feat: add Settings page with hijri seasons CRUD
- Server: seasons CRUD routes + generic NocoDB helpers - Client: Settings page at /settings with inline add/edit/delete - Seasons stored in NocoDB Seasons table - Vite proxy: /api/seasons routed to Express server - Nav links added (desktop + mobile) - Locale keys for EN + AR - Seasons loaded non-blocking on app mount Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
This commit is contained in:
@@ -3,6 +3,7 @@ import cors from 'cors';
|
||||
import { server, erp, nocodb } from './config';
|
||||
import erpRoutes from './routes/erp';
|
||||
import etlRoutes from './routes/etl';
|
||||
import seasonsRoutes from './routes/seasons';
|
||||
|
||||
const app = express();
|
||||
app.use(cors());
|
||||
@@ -11,6 +12,7 @@ app.use(express.json());
|
||||
// Mount routes
|
||||
app.use('/api/erp', erpRoutes);
|
||||
app.use('/api/etl', etlRoutes);
|
||||
app.use('/api/seasons', seasonsRoutes);
|
||||
|
||||
app.listen(server.port, () => {
|
||||
console.log(`\nServer running on http://localhost:${server.port}`);
|
||||
|
||||
63
server/src/routes/seasons.ts
Normal file
63
server/src/routes/seasons.ts
Normal file
@@ -0,0 +1,63 @@
|
||||
import { Router, Request, Response } from 'express';
|
||||
import { discoverTableIds, fetchAllRecords, createRecord, updateRecord, deleteRecord } from '../services/nocodbClient';
|
||||
|
||||
const router = Router();
|
||||
|
||||
async function getSeasonsTableId(): Promise<string> {
|
||||
const tables = await discoverTableIds();
|
||||
const id = tables['Seasons'];
|
||||
if (!id) throw new Error("NocoDB table 'Seasons' not found");
|
||||
return id;
|
||||
}
|
||||
|
||||
// GET /api/seasons
|
||||
router.get('/', async (_req: Request, res: Response) => {
|
||||
try {
|
||||
const tableId = await getSeasonsTableId();
|
||||
const records = await fetchAllRecords(tableId);
|
||||
res.json(records);
|
||||
} catch (err) {
|
||||
console.error('Failed to fetch seasons:', (err as Error).message);
|
||||
res.status(500).json({ error: (err as Error).message });
|
||||
}
|
||||
});
|
||||
|
||||
// POST /api/seasons
|
||||
router.post('/', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const tableId = await getSeasonsTableId();
|
||||
const result = await createRecord(tableId, req.body);
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
console.error('Failed to create season:', (err as Error).message);
|
||||
res.status(500).json({ error: (err as Error).message });
|
||||
}
|
||||
});
|
||||
|
||||
// PUT /api/seasons/:id
|
||||
router.put('/:id', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const tableId = await getSeasonsTableId();
|
||||
const id = parseInt(req.params.id);
|
||||
const result = await updateRecord(tableId, id, req.body);
|
||||
res.json(result);
|
||||
} catch (err) {
|
||||
console.error('Failed to update season:', (err as Error).message);
|
||||
res.status(500).json({ error: (err as Error).message });
|
||||
}
|
||||
});
|
||||
|
||||
// DELETE /api/seasons/:id
|
||||
router.delete('/:id', async (req: Request, res: Response) => {
|
||||
try {
|
||||
const tableId = await getSeasonsTableId();
|
||||
const id = parseInt(req.params.id);
|
||||
await deleteRecord(tableId, id);
|
||||
res.json({ ok: true });
|
||||
} catch (err) {
|
||||
console.error('Failed to delete season:', (err as Error).message);
|
||||
res.status(500).json({ error: (err as Error).message });
|
||||
}
|
||||
});
|
||||
|
||||
export default router;
|
||||
@@ -1,5 +1,4 @@
|
||||
import { nocodb } from '../config';
|
||||
import type { AggregatedRecord } from '../types';
|
||||
|
||||
let discoveredTables: Record<string, string> | null = null;
|
||||
|
||||
@@ -91,8 +90,7 @@ export async function deleteAllRows(tableId: string): Promise<number> {
|
||||
return deleted;
|
||||
}
|
||||
|
||||
export async function insertRecords(tableId: string, records: AggregatedRecord[]): Promise<number> {
|
||||
// NocoDB bulk insert accepts max 100 records at a time
|
||||
export async function insertRecords<T extends Record<string, unknown>>(tableId: string, records: T[]): Promise<number> {
|
||||
const batchSize = 100;
|
||||
let inserted = 0;
|
||||
|
||||
@@ -107,3 +105,43 @@ export async function insertRecords(tableId: string, records: AggregatedRecord[]
|
||||
|
||||
return inserted;
|
||||
}
|
||||
|
||||
// Generic CRUD helpers
|
||||
|
||||
export async function fetchAllRecords<T>(tableId: string): Promise<T[]> {
|
||||
let all: T[] = [];
|
||||
let offset = 0;
|
||||
|
||||
while (true) {
|
||||
const json = await fetchJson(
|
||||
`${nocodb.url}/api/v2/tables/${tableId}/records?limit=1000&offset=${offset}`
|
||||
) as { list: T[] };
|
||||
|
||||
all = all.concat(json.list);
|
||||
if (json.list.length < 1000) break;
|
||||
offset += 1000;
|
||||
}
|
||||
|
||||
return all;
|
||||
}
|
||||
|
||||
export async function createRecord<T extends Record<string, unknown>>(tableId: string, record: T): Promise<T> {
|
||||
return await fetchJson(`${nocodb.url}/api/v2/tables/${tableId}/records`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify(record),
|
||||
}) as T;
|
||||
}
|
||||
|
||||
export async function updateRecord<T extends Record<string, unknown>>(tableId: string, id: number, record: T): Promise<T> {
|
||||
return await fetchJson(`${nocodb.url}/api/v2/tables/${tableId}/records`, {
|
||||
method: 'PATCH',
|
||||
body: JSON.stringify({ Id: id, ...record }),
|
||||
}) as T;
|
||||
}
|
||||
|
||||
export async function deleteRecord(tableId: string, id: number): Promise<void> {
|
||||
await fetchJson(`${nocodb.url}/api/v2/tables/${tableId}/records`, {
|
||||
method: 'DELETE',
|
||||
body: JSON.stringify([{ Id: id }]),
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user