u
This commit is contained in:
@@ -1,4 +1,3 @@
|
||||
import { useDispatch, useSelector } from 'react-redux';
|
||||
import { useEffect, useState } from 'react';
|
||||
import Box from '@mui/material/Box';
|
||||
import InputAdornment from '@mui/material/InputAdornment';
|
||||
@@ -7,11 +6,8 @@ import Typography from '@mui/material/Typography';
|
||||
import { EditablePanelTile } from '../../panels';
|
||||
import ArticlePriceTiersDialog from '../ArticlePriceTiersDialog';
|
||||
import ArticleUnitEditorDialog from '../ArticleUnitEditorDialog';
|
||||
import { article } from '../../../api';
|
||||
import { useStorageManager } from '../../../api/storageManager';
|
||||
import { requireDefined, requireObject } from '../../../utils/apiShape';
|
||||
import { loadUnits, selectUnits, selectUnitsStatus } from '../../../store/unitSlice';
|
||||
import { selectCurrentCompany } from '../../../store/companySlice';
|
||||
import { patchArticleInLists } from '../../../store/categorySlice';
|
||||
import {
|
||||
buildPosPricesPutBody,
|
||||
companyTaxRateOptions,
|
||||
@@ -219,12 +215,11 @@ function PriceAmountField({
|
||||
/* ── main component ────────────────────────────────────────────────── */
|
||||
|
||||
export default function ArticleViewPrice({ article: articleData, onArticleUpdated }) {
|
||||
const dispatch = useDispatch();
|
||||
const store = useStorageManager();
|
||||
const [tiersDialogOpen, setTiersDialogOpen] = useState(false);
|
||||
const [unitEditorOpen, setUnitEditorOpen] = useState(false);
|
||||
const units = useSelector(selectUnits);
|
||||
const unitsStatus = useSelector(selectUnitsStatus);
|
||||
const company = useSelector(selectCurrentCompany);
|
||||
const [units, setUnits] = useState([]);
|
||||
const [company, setCompany] = useState(null);
|
||||
const basePrice = primaryPrice(articleData);
|
||||
const taxRates = companyTaxRateOptions(company, basePrice?.taxRate);
|
||||
const priceValue = priceDraftValue(articleData, company);
|
||||
@@ -234,20 +229,32 @@ export default function ArticleViewPrice({ article: articleData, onArticleUpdate
|
||||
: units;
|
||||
|
||||
useEffect(() => {
|
||||
if (unitsStatus !== 'idle') return;
|
||||
void dispatch(loadUnits()).unwrap().catch((err) => notifyError(err));
|
||||
}, [dispatch, unitsStatus]);
|
||||
|
||||
const syncCategoryListPrice = (next) => {
|
||||
const price = primaryPrice(next);
|
||||
dispatch(
|
||||
patchArticleInLists({
|
||||
sku: next.sku,
|
||||
net: price?.net ?? null,
|
||||
taxRate: price?.taxRate ?? null,
|
||||
}),
|
||||
);
|
||||
};
|
||||
let cancelled = false;
|
||||
async function load() {
|
||||
try {
|
||||
const [unitList, companies, session] = await Promise.all([
|
||||
store.unitPromise,
|
||||
store.companyPromise,
|
||||
store.authMePromise,
|
||||
]);
|
||||
const companyId = requireDefined(session.user?.selectedCompanyId, 'selectedCompanyId');
|
||||
const current = requireDefined(
|
||||
companies.find((c) => c.id === companyId),
|
||||
'company',
|
||||
);
|
||||
if (!cancelled) {
|
||||
setUnits(unitList);
|
||||
setCompany(current);
|
||||
}
|
||||
} catch (err) {
|
||||
if (!cancelled) notifyError(err);
|
||||
}
|
||||
}
|
||||
void load();
|
||||
return () => {
|
||||
cancelled = true;
|
||||
};
|
||||
}, [store]);
|
||||
|
||||
const handleSave = async (draft) => {
|
||||
const saved = priceDraftValue(articleData, company);
|
||||
@@ -269,12 +276,12 @@ export default function ArticleViewPrice({ article: articleData, onArticleUpdate
|
||||
if (isNoPackUnit(unitSi, unitamount)) {
|
||||
throw new Error('Einheit pc × 1 ist keine Packungseinheit');
|
||||
}
|
||||
const res = await article.update(articleData.sku, { unitamount, unitSi });
|
||||
const res = await store.article.update(articleData.sku, { unitamount, unitSi });
|
||||
requireObject(res, 'PATCH /api/article/:sku');
|
||||
requireObject(res.article, 'article');
|
||||
nextArticle = res.article;
|
||||
} else {
|
||||
const res = await article.update(articleData.sku, {
|
||||
const res = await store.article.update(articleData.sku, {
|
||||
unitamount: 1,
|
||||
unitSi: 'pc',
|
||||
});
|
||||
@@ -299,11 +306,10 @@ export default function ArticleViewPrice({ article: articleData, onArticleUpdate
|
||||
taxRate,
|
||||
tiers: draft.tiers ?? [],
|
||||
});
|
||||
const res = await article.setPrices(articleData.sku, prices);
|
||||
const res = await store.article.setPrices(articleData.sku, { prices });
|
||||
requireObject(res, 'PUT /api/article/:sku/prices');
|
||||
requireObject(res.article, 'article');
|
||||
nextArticle = res.article;
|
||||
syncCategoryListPrice(res.article);
|
||||
}
|
||||
|
||||
onArticleUpdated?.(nextArticle);
|
||||
@@ -421,12 +427,14 @@ export default function ArticleViewPrice({ article: articleData, onArticleUpdate
|
||||
<ArticleUnitEditorDialog
|
||||
open={unitEditorOpen}
|
||||
onClose={() => setUnitEditorOpen(false)}
|
||||
onUnitUpserted={(row) => {
|
||||
onUnitUpserted={async (row) => {
|
||||
setUnits(await store.unitPromise);
|
||||
if (!draft.unitSi) {
|
||||
onChange({ ...draft, unitSi: row.si });
|
||||
}
|
||||
}}
|
||||
onUnitDeleted={(si) => {
|
||||
onUnitDeleted={async (si) => {
|
||||
setUnits(await store.unitPromise);
|
||||
if (draft.unitSi === si) {
|
||||
onChange({ ...draft, unitSi: '' });
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user