mirror of
https://github.com/Tria-plc/edr-platform.git
synced 2026-08-29 09:30:59 +00:00
paginate wagons, locomotives and routes lists server-side
This commit is contained in:
@@ -1632,17 +1632,25 @@ export const api = {
|
||||
},
|
||||
|
||||
wagons: {
|
||||
/** Every match, page-walked — for pickers and yard views. Lists use `listPaged`. */
|
||||
list: endpoint<{ filters?: WagonListFilters }, Wagon[]>(
|
||||
"wagons",
|
||||
"list",
|
||||
({ filters }) => wagonService.getAll(filters ?? {}).then((r) => r.data),
|
||||
({ filters }) => wagonService.listAll(filters ?? {}),
|
||||
({ filters }) => ["wagons", "list", filters ?? {}],
|
||||
),
|
||||
|
||||
listPaged: endpoint<{ filters?: WagonListFilters }, PaginatedResponse<Wagon>>(
|
||||
"wagons",
|
||||
"listPaged",
|
||||
({ filters }) => wagonService.getAll(filters ?? {}).then((r) => r.data),
|
||||
({ filters }) => ["wagons", "listPaged", filters ?? {}],
|
||||
),
|
||||
|
||||
listByTrain: endpoint<{ trainId: string }, Wagon[]>(
|
||||
"wagons",
|
||||
"listByTrain",
|
||||
({ trainId }) => wagonService.getByTrain(trainId).then((r) => r.data),
|
||||
({ trainId }) => wagonService.getByTrain(trainId),
|
||||
({ trainId }) => ["wagons", "train", trainId],
|
||||
),
|
||||
|
||||
|
||||
@@ -23,7 +23,7 @@ const listHandlers: Record<
|
||||
> = {
|
||||
locomotives: (filters) => locomotivesService.getAll(filters ?? {}).then((r) => r.data),
|
||||
trains: () => trainService.getAll().then((r) => r.data),
|
||||
wagons: (filters) => wagonService.getAll(filters ?? {}).then((r) => r.data),
|
||||
wagons: (filters) => wagonService.listAll(filters ?? {}),
|
||||
containers: () => containerService.getAll().then((r) => r.data),
|
||||
cargoes: () => cargoService.getAll().then((r) => r.data),
|
||||
vehicles: (filters) => vehiclesService.getAll(filters ?? {}).then((r) => r.data),
|
||||
@@ -38,7 +38,7 @@ const pagedHandlers: Partial<
|
||||
Record<FleetResourceSlug, (filters: FleetListFilters) => Promise<PaginatedResponse<FleetRecord>>>
|
||||
> = {
|
||||
locomotives: (filters) => locomotivesService.getPaged(filters).then((r) => r.data),
|
||||
wagons: (filters) => wagonService.getPaged(filters).then((r) => r.data),
|
||||
wagons: (filters) => wagonService.getAll(filters).then((r) => r.data),
|
||||
};
|
||||
|
||||
export const isFleetServerPaginated = (slug: FleetResourceSlug): boolean =>
|
||||
|
||||
@@ -95,15 +95,28 @@ export interface WagonMovementRecord {
|
||||
}
|
||||
|
||||
export const wagonService = {
|
||||
/** One page ({items, meta}); 10 rows unless `pageSize` says otherwise. */
|
||||
getAll: (filters: WagonListFilters = {}) =>
|
||||
apiClient.get<Wagon[]>(`/wagons${wagonListQuery(filters)}`),
|
||||
/** Same filters as `getAll`, server-paginated ({items, meta}). */
|
||||
getPaged: (filters: WagonListFilters = {}) =>
|
||||
apiClient.get<PaginatedResponse<Wagon>>(`/wagons/paged${wagonListQuery(filters)}`),
|
||||
apiClient.get<PaginatedResponse<Wagon>>(`/wagons${wagonListQuery(filters)}`),
|
||||
/**
|
||||
* Every matching wagon, page-walked at the API's 100-row cap. For the pickers
|
||||
* and yard views that filter the whole fleet in the browser — a list page
|
||||
* should use `getAll` and show the real page controls instead.
|
||||
*/
|
||||
listAll: async (filters: WagonListFilters = {}): Promise<Wagon[]> => {
|
||||
const pageSize = 100;
|
||||
const first = await wagonService.getAll({ ...filters, page: 1, pageSize });
|
||||
const items = [...first.data.items];
|
||||
for (let page = 2; page <= (first.data.meta.totalPages ?? 1); page += 1) {
|
||||
const next = await wagonService.getAll({ ...filters, page, pageSize });
|
||||
items.push(...next.data.items);
|
||||
}
|
||||
return items;
|
||||
},
|
||||
getById: (id: string) => apiClient.get<Wagon>(`/wagons/${id}`),
|
||||
getMovements: (id: string) =>
|
||||
apiClient.get<WagonMovementRecord[]>(`/wagons/${id}/movements`),
|
||||
getByTrain: (trainId: string) => apiClient.get<Wagon[]>(`/wagons?trainId=${trainId}`),
|
||||
getByTrain: (trainId: string) => wagonService.listAll({ trainId }),
|
||||
assignToTrain: (wagonId: string, trainId: string, sequenceNumber?: number) =>
|
||||
apiClient.post(`/wagons/${wagonId}/assign-train`, { trainId, sequenceNumber }),
|
||||
unassign: (wagonId: string) => apiClient.post(`/wagons/${wagonId}/unassign-train`),
|
||||
|
||||
Reference in New Issue
Block a user