This commit is contained in:
seb
2026-10-04 21:04:40 +02:00
parent 669954b496
commit f765b87336
59 changed files with 1491 additions and 2045 deletions

View File

@@ -0,0 +1,44 @@
import { articleIndex as articleIndexApi } from '../api';
import { createListAutomat } from './createListAutomat';
/**
* @typedef {Object} ArticleIndexRecord
* @property {string|number} id Article id.
* @property {string} sku Article SKU.
* @property {string} name Article name.
* @property {Array<string>} eans Article EANs.
* @property {string|null} categories Comma-joined category ids.
* @property {boolean} isFather Whether the article has child variants.
*/
/**
* Company article search index (`GET /api/articleIndex`).
*
* Backs the article search page, the manifest article picker, and the
* optimistic name patches applied by `artikelAutomat`.
*
* @type {import('react-automat').Automat<import('./createListAutomat').ListAutomatState>}
*/
export const articleIndexAutomat = createListAutomat({
name: 'articleIndex',
fetch: () => articleIndexApi.listRecords(),
});
/**
* Patches a single record's name in the cached index (optimistic update).
*
* @param {string|number} sku Article SKU.
* @param {string} name New article name.
* @returns {void}
*/
export function setArticleIndexName(sku, name) {
const items = articleIndexAutomat.state.items;
if (!items.length) return;
articleIndexAutomat.actions.replace(
items.map((row) =>
String(row.sku) === String(sku) ? { ...row, name } : row,
),
);
}
export default articleIndexAutomat;