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