u
This commit is contained in:
@@ -24,7 +24,6 @@ import sseAutomat, { getSseClientId } from './sseAutomat';
|
||||
* @property {Array<object>} brands All brands (full list, decoupled from page UI).
|
||||
* @property {Array<object>} filteredBrands Filtered brands according to active filter query.
|
||||
* @property {string} filter Active search/filter string.
|
||||
* @property {number} total Unpaged brand count.
|
||||
* @property {'idle'|'loading'|'succeeded'|'failed'} status
|
||||
* @property {string|null} error
|
||||
*/
|
||||
@@ -34,7 +33,6 @@ const initialState = {
|
||||
brands: [],
|
||||
filteredBrands: [],
|
||||
filter: '',
|
||||
total: 0,
|
||||
status: 'idle',
|
||||
error: null,
|
||||
};
|
||||
@@ -50,7 +48,7 @@ const applyFilter = (brands, filter) => {
|
||||
let brandAutomat;
|
||||
let inflightLoad = null;
|
||||
const FETCH_PAGE_SIZE = 100;
|
||||
|
||||
// #region fetch
|
||||
async function fetchAllBrands() {
|
||||
let offset = 0, total = Infinity;
|
||||
const collected = [];
|
||||
@@ -62,12 +60,12 @@ async function fetchAllBrands() {
|
||||
offset += page.length;
|
||||
if (page.length === 0) break;
|
||||
}
|
||||
return { brands: sortBrands(collected), total: Number.isFinite(total) ? total : collected.length };
|
||||
return { brands: sortBrands(collected) };
|
||||
}
|
||||
|
||||
const updateStateWithBrands = (brands, extra = {}) => {
|
||||
const filteredBrands = applyFilter(brands, brandAutomat.state.filter);
|
||||
brandAutomat.setState({ brands, filteredBrands, total: brands.length, ...extra });
|
||||
brandAutomat.setState({ brands, filteredBrands, ...extra });
|
||||
return brands;
|
||||
};
|
||||
|
||||
@@ -96,47 +94,37 @@ const actions = {
|
||||
})
|
||||
.finally(() => { inflightLoad = null; }));
|
||||
},
|
||||
|
||||
/** Create a brand (insert). */
|
||||
// #region add
|
||||
async create(body) {
|
||||
const res = await brandApi.create(body);
|
||||
const row = res.brand;
|
||||
updateStateWithBrands(sortBrands([...brandAutomat.state.brands.filter((b) => b.id !== row.id), row]));
|
||||
return row;
|
||||
},
|
||||
|
||||
/** Rename a brand. */
|
||||
// #region rename
|
||||
async rename(id, body) {
|
||||
const res = await brandApi.rename(id, body);
|
||||
const updated = res.brand ?? { id: res.id, name: res.name, gsprpinfo: res.gsprpinfo };
|
||||
updateStateWithBrands(sortBrands(brandAutomat.state.brands.map((b) => (b.id === id ? { ...b, ...updated } : b))));
|
||||
return res;
|
||||
},
|
||||
|
||||
/** Delete a brand by id. */
|
||||
// #region del
|
||||
async remove(id) {
|
||||
await brandApi.delete(id);
|
||||
updateStateWithBrands(brandAutomat.state.brands.filter((b) => b.id !== id));
|
||||
},
|
||||
|
||||
/** Add / update brand image. */
|
||||
// #region addPic
|
||||
async addImage(id, file) {
|
||||
const res = await brandApi.addImage(id, file);
|
||||
await brandAutomat.actions.load(true);
|
||||
return res.brand;
|
||||
},
|
||||
|
||||
/** Delete brand image. */
|
||||
// #region delPic
|
||||
async deleteImage(id) {
|
||||
const res = await brandApi.deleteImage(id);
|
||||
await brandAutomat.actions.load(true);
|
||||
return res.brand;
|
||||
},
|
||||
|
||||
/** Reset to idle state (called on logout / company switch). */
|
||||
reset() {
|
||||
brandAutomat.setState(initialState);
|
||||
},
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
@@ -148,9 +136,10 @@ brandAutomat = new Automat(initialState, actions, { name: 'brand' });
|
||||
authAutomat.subscribe((auth) => {
|
||||
const companyId = auth.user?.selectedCompanyId;
|
||||
if (auth.status === 'authed' && companyId) void brandAutomat.actions.load();
|
||||
else if (auth.status === 'anon') brandAutomat.actions.reset();
|
||||
else if (auth.status === 'anon') brandAutomat.setState(initialState);
|
||||
});
|
||||
|
||||
// #region onSse
|
||||
sseAutomat.actions.on('brand', (event) => {
|
||||
try {
|
||||
const payload = JSON.parse(event?.data ?? '{}');
|
||||
|
||||
Reference in New Issue
Block a user