From 36cc1447986401d29676b913b1bdd13e53cb9e0c Mon Sep 17 00:00:00 2001 From: maxim Date: Thu, 3 Apr 2025 18:44:34 +0300 Subject: [PATCH] upgrade `edit` for `/sight` route to create `articles` --- src/components/CreateSightArticle.tsx | 116 ++++++++++++++++++++++++++ src/pages/sight/edit.tsx | 14 +++- 2 files changed, 127 insertions(+), 3 deletions(-) create mode 100644 src/components/CreateSightArticle.tsx diff --git a/src/components/CreateSightArticle.tsx b/src/components/CreateSightArticle.tsx new file mode 100644 index 0000000..dece4bf --- /dev/null +++ b/src/components/CreateSightArticle.tsx @@ -0,0 +1,116 @@ +import {Typography, Button, Box, Accordion, AccordionSummary, AccordionDetails, useTheme, TextField} from '@mui/material' +import ExpandMoreIcon from '@mui/icons-material/ExpandMore' +import {axiosInstance} from '../providers/data' +import {BACKEND_URL} from '../lib/constants' +import {useForm, Controller} from 'react-hook-form' +import {MarkdownEditor} from './MarkdownEditor' +import React from 'react' + +const MemoizedSimpleMDE = React.memo(MarkdownEditor) + +type Props = { + parentId: string | number + parentResource: string + childResource: string + title: string +} + +export const CreateSightArticle = ({parentId, parentResource, childResource, title}: Props) => { + const theme = useTheme() + + const { + register: registerItem, + control: controlItem, + handleSubmit: handleSubmitItem, + reset: resetItem, + formState: {errors: itemErrors}, + } = useForm({ + defaultValues: { + heading: '', + body: '', + }, + }) + + const simpleMDEOptions = React.useMemo( + () => ({ + placeholder: 'Введите контент в формате Markdown...', + spellChecker: false, + }), + [], + ) + + const handleCreate = async (data: {heading: string; body: string}) => { + try { + const response = await axiosInstance.post(`${BACKEND_URL}/${childResource}`, data) + const itemId = response.data.id + + const existingItemsResponse = await axiosInstance.get(`${BACKEND_URL}/${parentResource}/${parentId}/${childResource}`) + const existingItems = existingItemsResponse.data || [] + + const nextPageNum = existingItems.length + 1 + + await axiosInstance.post(`${BACKEND_URL}/${parentResource}/${parentId}/${childResource}/`, { + [`${childResource}_id`]: itemId, + page_num: nextPageNum, + }) + + resetItem() + window.location.reload() + } catch (err: any) { + console.error('Error creating item:', err) + if (err?.response) { + console.error('Error response:', err.response.data) + console.error('Error status:', err.response.status) + } + } + } + + return ( + + } + sx={{ + marginTop: 2, + background: theme.palette.background.paper, + borderBottom: `1px solid ${theme.palette.divider}`, + }} + > + + Создать {title} + + + + + + + } /> + + + + + + + + + ) +} diff --git a/src/pages/sight/edit.tsx b/src/pages/sight/edit.tsx index 3357b7a..f0366fe 100644 --- a/src/pages/sight/edit.tsx +++ b/src/pages/sight/edit.tsx @@ -4,9 +4,12 @@ import {useForm} from '@refinedev/react-hook-form' import {Controller} from 'react-hook-form' import {useParams} from 'react-router' import {LinkedItems} from '../../components/LinkedItems' +import {CreateSightArticle} from '../../components/CreateSightArticle' import {ArticleItem, articleFields} from './types' export const SightEdit = () => { + const {id: sightId} = useParams<{id: string}>() + const { saveButtonProps, register, @@ -14,8 +17,6 @@ export const SightEdit = () => { formState: {errors}, } = useForm({}) - const {id: sightId} = useParams<{id: string}>() - const {autocompleteProps: cityAutocompleteProps} = useAutocomplete({ resource: 'city', onSearch: (value) => [ @@ -98,7 +99,14 @@ export const SightEdit = () => { )} /> - {sightId && type="edit" parentId={sightId} parentResource="sight" childResource="article" fields={articleFields} title="статьи" />} + + {sightId && ( + + type="edit" parentId={sightId} parentResource="sight" childResource="article" fields={articleFields} title="статьи" /> + + + + )} ) }