import { DashboardRouteDefinition, detailPageRouteLoader, useDetailPage, Page, PageTitle, PageActionBar, Button, PageLayout, PageBlock, FormFieldWrapper, DetailFormGrid, Switch, Input, RichTextInput, CustomFieldsPageBlock, ActionBarItem, useNavigate, toast,} from '@vendure/dashboard';import type { AnyRoute } from '@vendure/dashboard';import { graphql } from '@/gql';const articleDetailDocument = graphql(` query GetArticleDetail($id: ID!) { article(id: $id) { id createdAt updatedAt isPublished title slug body customFields } }`);const createArticleDocument = graphql(` mutation CreateArticle($input: CreateArticleInput!) { createArticle(input: $input) { id } }`);const updateArticleDocument = graphql(` mutation UpdateArticle($input: UpdateArticleInput!) { updateArticle(input: $input) { id } }`);export const articleDetail: DashboardRouteDefinition = { path: '/articles/$id', loader: detailPageRouteLoader({ queryDocument: articleDetailDocument, breadcrumb: (isNew, entity) => [ { path: '/articles', label: 'Articles' }, isNew ? 'New article' : entity?.title, ], }), component: route => { return <ArticleDetailPage route={route} />; },};function ArticleDetailPage({ route }: { route: AnyRoute }) { const params = route.useParams(); const navigate = useNavigate(); const creatingNewEntity = params.id === 'new'; const { form, submitHandler, entity, isPending, resetForm } = useDetailPage({ queryDocument: articleDetailDocument, createDocument: createArticleDocument, updateDocument: updateArticleDocument, setValuesForUpdate: article => { return { id: article?.id ?? '', isPublished: article?.isPublished ?? false, title: article?.title ?? '', slug: article?.slug ?? '', body: article?.body ?? '', }; }, params: { id: params.id }, onSuccess: async data => { toast('Successfully updated article'); resetForm(); if (creatingNewEntity) { await navigate({ to: `../$id`, params: { id: data.id } }); } }, onError: err => { toast('Failed to update article', { description: err instanceof Error ? err.message : 'Unknown error', }); }, }); return ( <Page pageId="article-detail" form={form} submitHandler={submitHandler}> <PageTitle>{creatingNewEntity ? 'New article' : (entity?.title ?? '')}</PageTitle> <PageActionBar> <ActionBarItem requires={['UpdateProduct', 'UpdateCatalog']} itemId="save-button"> <Button type="submit" disabled={!form.formState.isDirty || !form.formState.isValid || isPending} > Update </Button> </ActionBarItem> </PageActionBar> <PageLayout> <PageBlock column="side" blockId="publish-status"> <FormFieldWrapper control={form.control} name="isPublished" label="Is Published" render={({ field }) => ( <Switch checked={field.value} onCheckedChange={field.onChange} /> )} /> </PageBlock> <PageBlock column="main" blockId="main-form"> <DetailFormGrid> <FormFieldWrapper control={form.control} name="title" label="Title" render={({ field }) => <Input {...field} />} /> <FormFieldWrapper control={form.control} name="slug" label="Slug" render={({ field }) => <Input {...field} />} /> </DetailFormGrid> <FormFieldWrapper control={form.control} name="body" label="Content" render={({ field }) => ( <RichTextInput value={field.value ?? ''} onChange={field.onChange} /> )} /> </PageBlock> <CustomFieldsPageBlock column="main" entityType="Article" control={form.control} /> </PageLayout> </Page> );}