mirror of
https://github.com/s-frick/effigenix.git
synced 2026-03-28 17:39:57 +01:00
feat(tui): Produktionschargen und Bestandsverwaltung in TUI einbauen
Chargen: Liste mit Statusfilter, Planen, Starten, Verbrauch erfassen, Abschließen und Stornieren. Bestände: Liste, Anlegen, Detailansicht mit Chargen sperren/entsperren/entfernen. Types, API-Client, Hooks, Navigation und Screens für beide Bounded Contexts vollständig ergänzt.
This commit is contained in:
parent
b2b3b59ce9
commit
5fe0dfc139
21 changed files with 2385 additions and 31 deletions
120
frontend/apps/cli/src/hooks/useBatches.ts
Normal file
120
frontend/apps/cli/src/hooks/useBatches.ts
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
import { useState, useCallback } from 'react';
|
||||
import type { BatchSummaryDTO, BatchDTO, PlanBatchRequest, BatchStatus } from '@effigenix/api-client';
|
||||
import { client } from '../utils/api-client.js';
|
||||
|
||||
interface BatchesState {
|
||||
batches: BatchSummaryDTO[];
|
||||
batch: BatchDTO | null;
|
||||
loading: boolean;
|
||||
error: string | null;
|
||||
}
|
||||
|
||||
function errorMessage(err: unknown): string {
|
||||
return err instanceof Error ? err.message : 'Unbekannter Fehler';
|
||||
}
|
||||
|
||||
export function useBatches() {
|
||||
const [state, setState] = useState<BatchesState>({
|
||||
batches: [],
|
||||
batch: null,
|
||||
loading: false,
|
||||
error: null,
|
||||
});
|
||||
|
||||
const fetchBatches = useCallback(async (status?: BatchStatus) => {
|
||||
setState((s) => ({ ...s, loading: true, error: null }));
|
||||
try {
|
||||
const batches = await client.batches.list(status);
|
||||
setState((s) => ({ ...s, batches, loading: false, error: null }));
|
||||
} catch (err) {
|
||||
setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
|
||||
}
|
||||
}, []);
|
||||
|
||||
const fetchBatch = useCallback(async (id: string) => {
|
||||
setState((s) => ({ ...s, loading: true, error: null }));
|
||||
try {
|
||||
const batch = await client.batches.getById(id);
|
||||
setState((s) => ({ ...s, batch, loading: false, error: null }));
|
||||
} catch (err) {
|
||||
setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
|
||||
}
|
||||
}, []);
|
||||
|
||||
const planBatch = useCallback(async (request: PlanBatchRequest) => {
|
||||
setState((s) => ({ ...s, loading: true, error: null }));
|
||||
try {
|
||||
const batch = await client.batches.plan(request);
|
||||
setState((s) => ({ ...s, loading: false, error: null }));
|
||||
return batch;
|
||||
} catch (err) {
|
||||
setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
|
||||
return null;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const startBatch = useCallback(async (id: string) => {
|
||||
setState((s) => ({ ...s, loading: true, error: null }));
|
||||
try {
|
||||
const batch = await client.batches.start(id);
|
||||
setState((s) => ({ ...s, batch, loading: false, error: null }));
|
||||
return batch;
|
||||
} catch (err) {
|
||||
setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
|
||||
return null;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const recordConsumption = useCallback(async (id: string, request: Parameters<typeof client.batches.recordConsumption>[1]) => {
|
||||
setState((s) => ({ ...s, loading: true, error: null }));
|
||||
try {
|
||||
await client.batches.recordConsumption(id, request);
|
||||
const batch = await client.batches.getById(id);
|
||||
setState((s) => ({ ...s, batch, loading: false, error: null }));
|
||||
return true;
|
||||
} catch (err) {
|
||||
setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
|
||||
return false;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const completeBatch = useCallback(async (id: string, request: Parameters<typeof client.batches.complete>[1]) => {
|
||||
setState((s) => ({ ...s, loading: true, error: null }));
|
||||
try {
|
||||
const batch = await client.batches.complete(id, request);
|
||||
setState((s) => ({ ...s, batch, loading: false, error: null }));
|
||||
return batch;
|
||||
} catch (err) {
|
||||
setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
|
||||
return null;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const cancelBatch = useCallback(async (id: string, reason: string) => {
|
||||
setState((s) => ({ ...s, loading: true, error: null }));
|
||||
try {
|
||||
const batch = await client.batches.cancel(id, { reason });
|
||||
setState((s) => ({ ...s, batch, loading: false, error: null }));
|
||||
return batch;
|
||||
} catch (err) {
|
||||
setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
|
||||
return null;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const clearError = useCallback(() => {
|
||||
setState((s) => ({ ...s, error: null }));
|
||||
}, []);
|
||||
|
||||
return {
|
||||
...state,
|
||||
fetchBatches,
|
||||
fetchBatch,
|
||||
planBatch,
|
||||
startBatch,
|
||||
recordConsumption,
|
||||
completeBatch,
|
||||
cancelBatch,
|
||||
clearError,
|
||||
};
|
||||
}
|
||||
|
|
@ -1,8 +1,17 @@
|
|||
import { useState, useCallback } from 'react';
|
||||
import type { StockBatchDTO, AddStockBatchRequest } from '@effigenix/api-client';
|
||||
import type {
|
||||
StockDTO,
|
||||
StockBatchDTO,
|
||||
AddStockBatchRequest,
|
||||
CreateStockRequest,
|
||||
UpdateStockRequest,
|
||||
StockFilter,
|
||||
} from '@effigenix/api-client';
|
||||
import { client } from '../utils/api-client.js';
|
||||
|
||||
interface StocksState {
|
||||
stocks: StockDTO[];
|
||||
stock: StockDTO | null;
|
||||
loading: boolean;
|
||||
error: string | null;
|
||||
}
|
||||
|
|
@ -13,29 +22,121 @@ function errorMessage(err: unknown): string {
|
|||
|
||||
export function useStocks() {
|
||||
const [state, setState] = useState<StocksState>({
|
||||
stocks: [],
|
||||
stock: null,
|
||||
loading: false,
|
||||
error: null,
|
||||
});
|
||||
|
||||
const fetchStocks = useCallback(async (filter?: StockFilter) => {
|
||||
setState((s) => ({ ...s, loading: true, error: null }));
|
||||
try {
|
||||
const stocks = await client.stocks.list(filter);
|
||||
setState((s) => ({ ...s, stocks, loading: false, error: null }));
|
||||
} catch (err) {
|
||||
setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
|
||||
}
|
||||
}, []);
|
||||
|
||||
const fetchStock = useCallback(async (id: string) => {
|
||||
setState((s) => ({ ...s, loading: true, error: null }));
|
||||
try {
|
||||
const stock = await client.stocks.getById(id);
|
||||
setState((s) => ({ ...s, stock, loading: false, error: null }));
|
||||
} catch (err) {
|
||||
setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
|
||||
}
|
||||
}, []);
|
||||
|
||||
const createStock = useCallback(async (request: CreateStockRequest) => {
|
||||
setState((s) => ({ ...s, loading: true, error: null }));
|
||||
try {
|
||||
const result = await client.stocks.create(request);
|
||||
setState((s) => ({ ...s, loading: false, error: null }));
|
||||
return result;
|
||||
} catch (err) {
|
||||
setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
|
||||
return null;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const updateStock = useCallback(async (id: string, request: UpdateStockRequest) => {
|
||||
setState((s) => ({ ...s, loading: true, error: null }));
|
||||
try {
|
||||
const stock = await client.stocks.update(id, request);
|
||||
setState((s) => ({ ...s, stock, loading: false, error: null }));
|
||||
return stock;
|
||||
} catch (err) {
|
||||
setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
|
||||
return null;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const addBatch = useCallback(async (stockId: string, request: AddStockBatchRequest): Promise<StockBatchDTO | null> => {
|
||||
setState({ loading: true, error: null });
|
||||
setState((s) => ({ ...s, loading: true, error: null }));
|
||||
try {
|
||||
const batch = await client.stocks.addBatch(stockId, request);
|
||||
setState({ loading: false, error: null });
|
||||
setState((s) => ({ ...s, loading: false, error: null }));
|
||||
return batch;
|
||||
} catch (err) {
|
||||
setState({ loading: false, error: errorMessage(err) });
|
||||
setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
|
||||
return null;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const removeBatch = useCallback(async (stockId: string, batchId: string, quantityAmount: string, quantityUnit: string) => {
|
||||
setState((s) => ({ ...s, loading: true, error: null }));
|
||||
try {
|
||||
await client.stocks.removeBatch(stockId, batchId, { quantityAmount, quantityUnit });
|
||||
const stock = await client.stocks.getById(stockId);
|
||||
setState((s) => ({ ...s, stock, loading: false, error: null }));
|
||||
return true;
|
||||
} catch (err) {
|
||||
setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
|
||||
return false;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const blockBatch = useCallback(async (stockId: string, batchId: string, reason: string) => {
|
||||
setState((s) => ({ ...s, loading: true, error: null }));
|
||||
try {
|
||||
await client.stocks.blockBatch(stockId, batchId, { reason });
|
||||
const stock = await client.stocks.getById(stockId);
|
||||
setState((s) => ({ ...s, stock, loading: false, error: null }));
|
||||
return true;
|
||||
} catch (err) {
|
||||
setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
|
||||
return false;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const unblockBatch = useCallback(async (stockId: string, batchId: string) => {
|
||||
setState((s) => ({ ...s, loading: true, error: null }));
|
||||
try {
|
||||
await client.stocks.unblockBatch(stockId, batchId);
|
||||
const stock = await client.stocks.getById(stockId);
|
||||
setState((s) => ({ ...s, stock, loading: false, error: null }));
|
||||
return true;
|
||||
} catch (err) {
|
||||
setState((s) => ({ ...s, loading: false, error: errorMessage(err) }));
|
||||
return false;
|
||||
}
|
||||
}, []);
|
||||
|
||||
const clearError = useCallback(() => {
|
||||
setState((s) => ({ ...s, error: null }));
|
||||
}, []);
|
||||
|
||||
return {
|
||||
...state,
|
||||
fetchStocks,
|
||||
fetchStock,
|
||||
createStock,
|
||||
updateStock,
|
||||
addBatch,
|
||||
removeBatch,
|
||||
blockBatch,
|
||||
unblockBatch,
|
||||
clearError,
|
||||
};
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue