42 lines
1.3 KiB
JavaScript
42 lines
1.3 KiB
JavaScript
/**
|
|
* @file customergroupAutomat.js
|
|
* @description Manages cached customer groups (`GET /api/customergroup`) for
|
|
* the active company. Built via `createListAutomat`.
|
|
*
|
|
* Upstream Triggers:
|
|
* - `authAutomat`: Via `createListAutomat` — triggers `load()` on `'authed'` with
|
|
* a `selectedCompanyId`, and `reset()` on `'anon'`.
|
|
* - UI / Routes: Manual calls to `load()` or `reload()`.
|
|
*/
|
|
|
|
import { customergroup as customergroupApi } from '../api';
|
|
import { createListAutomat } from './createListAutomat';
|
|
|
|
/**
|
|
* @typedef {Object} CustomerGroup
|
|
* @property {string|number} id Customer group id.
|
|
* @property {string} name Group name.
|
|
* @property {number} sort Sort index.
|
|
* @property {boolean} editable False for reserved POS / Lieferanten groups.
|
|
*/
|
|
|
|
/**
|
|
* Company customer groups (`GET /api/customergroup`).
|
|
*
|
|
* Reserved groups (`POS`, `Lieferanten`) are flagged `editable: false` and must
|
|
* not be renamed or deleted by the UI.
|
|
*
|
|
* @type {import('react-automat').Automat<import('./createListAutomat').ListAutomatState>}
|
|
*/
|
|
export const customergroupAutomat = createListAutomat({
|
|
name: 'customergroup',
|
|
fetch: async () => {
|
|
const data = await customergroupApi.list();
|
|
return data.customergroups ?? [];
|
|
},
|
|
sort: (items) =>
|
|
[...items].sort((a, b) => (Number(a.sort) || 0) - (Number(b.sort) || 0)),
|
|
});
|
|
|
|
export default customergroupAutomat;
|