fix атрибутов создания шаблона

This commit is contained in:
2025-07-27 08:25:21 +03:00
parent 4752b2dedc
commit de5b60907b
3 changed files with 57 additions and 19 deletions

View File

@@ -2,6 +2,10 @@ export interface CreateTemplateInput {
name: string name: string
description?: string description?: string
elements: Record<string, any> elements: Record<string, any>
attributes?: Array<{
attribute_id: string
value: string | null
}>
} }
export interface CreateTemplateOutput { export interface CreateTemplateOutput {

View File

@@ -28,7 +28,8 @@ interface TemplateContextType {
error: Error | null error: Error | null
refetchTemplates: () => void refetchTemplates: () => void
addTemplate: ( addTemplate: (
data: Omit<Template, 'id' | 'createdAt' | 'updatedAt'> data: Omit<Template, 'id' | 'createdAt' | 'updatedAt'>,
attributes?: Array<{ attributeId: string; value: string | null }>
) => Promise<Template> ) => Promise<Template>
updateTemplate: ( updateTemplate: (
template: Template, template: Template,
@@ -66,10 +67,20 @@ export const TemplateProvider: React.FC<{ children: ReactNode }> = ({
}) })
const createMutation = useMutation({ const createMutation = useMutation({
mutationFn: async ( mutationFn: async ({
newTemplate: Omit<Template, 'id' | 'createdAt' | 'updatedAt'> template,
) => { attributes,
const input = templateToCreateInput(newTemplate) }: {
template: Omit<Template, 'id' | 'createdAt' | 'updatedAt'>
attributes?: Array<{ attributeId: string; value: string | null }>
}) => {
const input = templateToCreateInput(template)
if (attributes && attributes.length > 0) {
input.attributes = attributes.map(attr => ({
attribute_id: attr.attributeId,
value: attr.value,
}))
}
const { id } = await createTemplateApi(input) const { id } = await createTemplateApi(input)
const { template: apiTpl } = await getTemplateApi(id) const { template: apiTpl } = await getTemplateApi(id)
if (!apiTpl) throw new Error('Template not found') if (!apiTpl) throw new Error('Template not found')
@@ -172,7 +183,14 @@ export const TemplateProvider: React.FC<{ children: ReactNode }> = ({
(copyMutation.error as Error) || (copyMutation.error as Error) ||
null, null,
refetchTemplates: refetch, refetchTemplates: refetch,
addTemplate: createMutation.mutateAsync, addTemplate: async (
template: Omit<Template, 'id' | 'createdAt' | 'updatedAt'>,
attributes?: Array<{ attributeId: string; value: string | null }>
) =>
createMutation.mutateAsync({
template,
attributes,
}),
updateTemplate: async ( updateTemplate: async (
template: Template, template: Template,
attributes?: Array<{ attributeId: string; value: string | null }> attributes?: Array<{ attributeId: string; value: string | null }>

View File

@@ -118,7 +118,7 @@ interface TemplateFormProps {
attributeValues: AttributeValue[] attributeValues: AttributeValue[]
onAttributeValuesChange: (values: AttributeValue[]) => void onAttributeValuesChange: (values: AttributeValue[]) => void
attributesLoading: boolean attributesLoading: boolean
onSubmit: () => void onSubmit: () => void | Promise<void>
onCancel: () => void onCancel: () => void
submitText: string submitText: string
submitIcon: React.ReactNode submitIcon: React.ReactNode
@@ -229,7 +229,14 @@ const TemplateForm: React.FC<TemplateFormProps> = ({
<Button variant="outline">Отмена</Button> <Button variant="outline">Отмена</Button>
</DialogClose> </DialogClose>
<Button <Button
onClick={onSubmit} onClick={() => {
const result = onSubmit()
if (result instanceof Promise) {
result.catch(error => {
console.error('Ошибка при выполнении операции:', error)
})
}
}}
disabled={isSubmitDisabled} disabled={isSubmitDisabled}
className="min-w-[130px]" className="min-w-[130px]"
> >
@@ -319,18 +326,27 @@ export const TemplatesOverviewPage = () => {
setSelectionMode(false) setSelectionMode(false)
} }
const handleCreate = () => { const handleCreate = async () => {
if (newName.trim()) { if (newName.trim()) {
addTemplate({ try {
name: newName, // Создаем шаблон с атрибутами сразу
elements: [], await addTemplate(
description: newDescription, {
mergedCells: [], name: newName,
}) elements: [],
setNewName('') description: newDescription,
setNewDescription('') mergedCells: [],
setNewAttributeValues([]) },
setIsDialogOpen(false) newAttributeValues.length > 0 ? newAttributeValues : undefined
)
setNewName('')
setNewDescription('')
setNewAttributeValues([])
setIsDialogOpen(false)
} catch (error) {
console.error('Ошибка при создании шаблона:', error)
}
} }
} }