From 1fda43b20d8d857d12c10b1bd502f5cb20732c1e Mon Sep 17 00:00:00 2001 From: seb Date: Wed, 5 Aug 2026 18:17:48 +0200 Subject: [PATCH] u --- src/components/article/ArticleView.jsx | 9 -- .../article/boxes/ArticleViewName.jsx | 118 ++++++++---------- 2 files changed, 52 insertions(+), 75 deletions(-) diff --git a/src/components/article/ArticleView.jsx b/src/components/article/ArticleView.jsx index 84f39eb..5007bad 100644 --- a/src/components/article/ArticleView.jsx +++ b/src/components/article/ArticleView.jsx @@ -54,21 +54,14 @@ class ArticleView extends Component { } } - refreshSearchCaches = () => { - void this.context.updateArticleIndex(); - void this.context.updateCategoryTree(); - }; - handleArticleUpdated = (next) => { this.context.setArticle(next); this.setState({ article: next }); - this.refreshSearchCaches(); }; handleArticleDeleted = () => { const { navigate } = this.props; const { article, categories, rootCategoryId } = this.state; - this.refreshSearchCaches(); const categoryId = article?.categoryIds?.[0]; if (categories && rootCategoryId != null && categoryId != null) { navigate(buildCategoryPath(categories, rootCategoryId, categoryId)); @@ -96,8 +89,6 @@ class ArticleView extends Component { diff --git a/src/components/article/boxes/ArticleViewName.jsx b/src/components/article/boxes/ArticleViewName.jsx index f0bd05a..ef60b75 100644 --- a/src/components/article/boxes/ArticleViewName.jsx +++ b/src/components/article/boxes/ArticleViewName.jsx @@ -2,7 +2,6 @@ import { Component } from 'react'; import { EditablePanelTile } from '../../panels'; import SingleLineTextEdit from '../../SingleLineTextEdit'; import { StorageManagerContext } from '../../../api/storageManagerContext'; -import { requireObject } from '../../../utils/apiShape'; import { getCategoryBreadcrumbs } from '../../../store/categorySlice'; import { Box, Chip, IconButton } from '@mui/material'; import AddIcon from '@mui/icons-material/Add'; @@ -51,13 +50,18 @@ const columnSx = { alignSelf: 'stretch', }; +function categoryIdsChanged(a, b) { + const aa = a ?? []; + const bb = b ?? []; + return ( + aa.length !== bb.length || + [...aa].map(String).sort().join(',') !== [...bb].map(String).sort().join(',') + ); +} + function draftDirty(draft, saved) { const nameDiff = draft.name.trim() !== String(saved.name ?? '').trim(); - const a = draft.categoryIds ?? []; - const b = saved.categoryIds ?? []; - const catDiff = - a.length !== b.length || - [...a].map(String).sort().join(',') !== [...b].map(String).sort().join(','); + const catDiff = categoryIdsChanged(draft.categoryIds, saved.categoryIds); return nameDiff || catDiff; } @@ -83,6 +87,8 @@ function categoryLabels(categoryIds, categories, rootCategoryId) { } class CategoryPills extends Component { + static contextType = StorageManagerContext; + constructor(props) { super(props); this.state = { pickerOpen: false }; @@ -103,16 +109,10 @@ class CategoryPills extends Component { }; render() { - const { - categoryLabels: labels, - categoryIds, - categories, - rootCategoryId, - sku, - editMode, - onCategoryIdsChange, - } = this.props; + const { categoryIds, editMode, onCategoryIdsChange } = this.props; const { pickerOpen } = this.state; + const { categories, rootCategoryId } = this.context.getCategoryState() ?? {}; + const labels = categoryLabels(categoryIds, categories, rootCategoryId); return ( <> @@ -140,15 +140,16 @@ class CategoryPills extends Component { ) : null} - + {editMode ? ( + + ) : null} ); } @@ -156,8 +157,7 @@ class CategoryPills extends Component { class NameDisplay extends Component { render() { - const { name, categoryIds, categories, rootCategoryId, sku } = this.props; - const labels = categoryLabels(categoryIds, categories, rootCategoryId); + const { name, categoryIds } = this.props; return ( @@ -169,15 +169,8 @@ class NameDisplay extends Component { sx={articleNameFieldHiddenSx} /> - {labels.length > 0 ? ( - + {(categoryIds?.length ?? 0) > 0 ? ( + ) : null} ); @@ -186,34 +179,24 @@ class NameDisplay extends Component { class NameEdit extends Component { render() { - const { - draft, - onChange, - inputRef, - categories, - rootCategoryId, - sku, - } = this.props; - const labels = categoryLabels(draft.categoryIds, categories, rootCategoryId); + const { draft, onChange, inputRef, name, categoryIds } = this.props; + const currentName = draft?.name ?? name; + const currentCategoryIds = draft?.categoryIds ?? categoryIds; return ( onChange({ ...draft, name })} + value={currentName} + onChange={(nextName) => onChange({ ...draft, name: nextName })} sx={articleNameFieldSx} /> onChange({ ...draft, categoryIds })} + onCategoryIdsChange={(nextIds) => onChange({ ...draft, categoryIds: nextIds })} /> ); @@ -228,19 +211,26 @@ export default class ArticleViewName extends Component { const store = this.context; const name = draft.name.trim(); const categoryIds = draft.categoryIds ?? []; + const nameChanged = name !== String(articleData.name ?? '').trim(); + const categoriesChanged = categoryIdsChanged(categoryIds, articleData.categoryIds); - const [nameRes] = await Promise.all([ - store.article.update(articleData.sku, { name }), - store.article.setCategories(articleData.sku, { categoryIds }), - ]); + let article = null; - requireObject(nameRes, 'PATCH /api/article/:sku'); - requireObject(nameRes.article, 'article'); - onArticleUpdated?.(nameRes.article); + if (nameChanged) { + const nameRes = await store.article.update(articleData.sku, { name }); + article = nameRes.article; + } + + if (categoriesChanged) { + const catRes = await store.article.setCategories(articleData.sku, { categoryIds }); + article = catRes.article; + } + + if (article) onArticleUpdated?.(article); }; render() { - const { article: articleData, categories, rootCategoryId } = this.props; + const { article: articleData } = this.props; const savedCategoryIds = articleData.categoryIds ?? []; const value = { name: articleData.name ?? '', categoryIds: [...savedCategoryIds] }; @@ -257,16 +247,12 @@ export default class ArticleViewName extends Component { } edit={ } />