fix атрибутов создания шаблона
This commit is contained in:
@@ -2,6 +2,10 @@ export interface CreateTemplateInput {
|
||||
name: string
|
||||
description?: string
|
||||
elements: Record<string, any>
|
||||
attributes?: Array<{
|
||||
attribute_id: string
|
||||
value: string | null
|
||||
}>
|
||||
}
|
||||
|
||||
export interface CreateTemplateOutput {
|
||||
|
||||
@@ -28,7 +28,8 @@ interface TemplateContextType {
|
||||
error: Error | null
|
||||
refetchTemplates: () => void
|
||||
addTemplate: (
|
||||
data: Omit<Template, 'id' | 'createdAt' | 'updatedAt'>
|
||||
data: Omit<Template, 'id' | 'createdAt' | 'updatedAt'>,
|
||||
attributes?: Array<{ attributeId: string; value: string | null }>
|
||||
) => Promise<Template>
|
||||
updateTemplate: (
|
||||
template: Template,
|
||||
@@ -66,10 +67,20 @@ export const TemplateProvider: React.FC<{ children: ReactNode }> = ({
|
||||
})
|
||||
|
||||
const createMutation = useMutation({
|
||||
mutationFn: async (
|
||||
newTemplate: Omit<Template, 'id' | 'createdAt' | 'updatedAt'>
|
||||
) => {
|
||||
const input = templateToCreateInput(newTemplate)
|
||||
mutationFn: async ({
|
||||
template,
|
||||
attributes,
|
||||
}: {
|
||||
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 { template: apiTpl } = await getTemplateApi(id)
|
||||
if (!apiTpl) throw new Error('Template not found')
|
||||
@@ -172,7 +183,14 @@ export const TemplateProvider: React.FC<{ children: ReactNode }> = ({
|
||||
(copyMutation.error as Error) ||
|
||||
null,
|
||||
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 (
|
||||
template: Template,
|
||||
attributes?: Array<{ attributeId: string; value: string | null }>
|
||||
|
||||
@@ -118,7 +118,7 @@ interface TemplateFormProps {
|
||||
attributeValues: AttributeValue[]
|
||||
onAttributeValuesChange: (values: AttributeValue[]) => void
|
||||
attributesLoading: boolean
|
||||
onSubmit: () => void
|
||||
onSubmit: () => void | Promise<void>
|
||||
onCancel: () => void
|
||||
submitText: string
|
||||
submitIcon: React.ReactNode
|
||||
@@ -229,7 +229,14 @@ const TemplateForm: React.FC<TemplateFormProps> = ({
|
||||
<Button variant="outline">Отмена</Button>
|
||||
</DialogClose>
|
||||
<Button
|
||||
onClick={onSubmit}
|
||||
onClick={() => {
|
||||
const result = onSubmit()
|
||||
if (result instanceof Promise) {
|
||||
result.catch(error => {
|
||||
console.error('Ошибка при выполнении операции:', error)
|
||||
})
|
||||
}
|
||||
}}
|
||||
disabled={isSubmitDisabled}
|
||||
className="min-w-[130px]"
|
||||
>
|
||||
@@ -319,18 +326,27 @@ export const TemplatesOverviewPage = () => {
|
||||
setSelectionMode(false)
|
||||
}
|
||||
|
||||
const handleCreate = () => {
|
||||
const handleCreate = async () => {
|
||||
if (newName.trim()) {
|
||||
addTemplate({
|
||||
try {
|
||||
// Создаем шаблон с атрибутами сразу
|
||||
await addTemplate(
|
||||
{
|
||||
name: newName,
|
||||
elements: [],
|
||||
description: newDescription,
|
||||
mergedCells: [],
|
||||
})
|
||||
},
|
||||
newAttributeValues.length > 0 ? newAttributeValues : undefined
|
||||
)
|
||||
|
||||
setNewName('')
|
||||
setNewDescription('')
|
||||
setNewAttributeValues([])
|
||||
setIsDialogOpen(false)
|
||||
} catch (error) {
|
||||
console.error('Ошибка при создании шаблона:', error)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user