import React, { useCallback, useMemo } from "react"; import { useParams } from "react-router-dom"; import CancelIcon from "@mui/icons-material/Cancel"; import CheckCircleIcon from "@mui/icons-material/CheckCircle"; import Card from "../../../components/Card"; import CardActions from "../../../components/Card/CardActions"; import CardCancelButton from "../../../components/Card/CardCancelButton"; import CardContent from "../../../components/Card/CardContent"; import CardFieldCheckbox from "../../../components/Card/CardFieldCheckbox"; import CardFieldText from "../../../components/Card/CardFieldText"; import CardHeader from "../../../components/Card/CardHeader"; import CardRow from "../../../components/Card/CardRow"; import CardSaveButton from "../../../components/Card/CardSaveButton"; import { useApi } from "../../../contexts/ApiContext"; import { useI18n } from "../../../contexts/I18nContext"; import { useUser } from "../../../contexts/UserContext"; import { hasPermission } from "../../../util/has_permission"; import { ArticleCreated, ArticleDetail } from "../types/article"; import { PHYSICAL_ITEM_TYPES } from "../utils"; export type ArticleCardProps = | { create: true; article?: ArticleDetail; onCreated: (article: ArticleCreated) => void; onUpdated?: never; } | { create?: false; article: ArticleDetail; onCreated?: never; onUpdated: (article: ArticleDetail) => void; }; export interface ArticleCardFormValues { code: string; item_type: string; is_active: string; name: string; description: string; product_number: string; model_number: string; tax_code: string; gtin: string; variant: string; } export const ArticleCard: React.FC = ({ article, create, onCreated, onUpdated, }) => { const params = useParams(); const api = useApi(); const { t } = useI18n(); const { user } = useUser(); const canCreate = useMemo(() => hasPermission(user, "catalog.add_article"), [user]); const canChange = useMemo(() => hasPermission(user, "catalog.change_article"), [user]); article ??= { id: 0, name: "", variant: "", item_type: "", code: "", model_number: "", product_number: "", gtin: "", tax_code: "", is_active: true, variants: [], }; const handleSave = useCallback( async ({ is_active, ...values }: ArticleCardFormValues) => { if (create != null) { const action = api.operations["catalog.article:create"]; if (!action) { throw new Error('Invalid action "catalog.article:create".'); } const response = await action.call({ params, body: { is_active: is_active == "on", ...values, }, }); if (response.ok) { const createdArticle = await response.json(); onCreated?.(createdArticle); return t("Article created successfully."); } else { console.error("[ARTICLE_CARD]", response); throw new Error("creating article."); } } else { const action = api.operations["catalog.article:update"]; if (!action) { throw new Error('Invalid action "catalog.article:update".'); } const response = await action.call({ params, body: { is_active: is_active == "on", ...values, }, }); if (response.ok) { const updatedArticle = await response.json(); onUpdated?.(updatedArticle); return t("Article updated successfully."); } else { console.error("[ARTICLE_CARD]", response); throw new Error("updating article fields."); } } }, [api, params, create, onCreated, onUpdated, t], ); return ( {(PHYSICAL_ITEM_TYPES.includes(article.item_type) || create) && ( <> } size={1} value={article.is_active} yesValue={} /> )} {(canChange || canCreate) && ( {!create && } )} ); };