u
This commit is contained in:
44
src/automats/articleIndexAutomat.js
Normal file
44
src/automats/articleIndexAutomat.js
Normal 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;
|
||||
Reference in New Issue
Block a user