diff --git a/src/App.tsx b/src/App.tsx index 26d6f00..5cd15ce 100644 --- a/src/App.tsx +++ b/src/App.tsx @@ -110,7 +110,6 @@ function App() { create: '/sight/create', edit: '/sight/edit/:id', show: '/sight/show/:id', - // добавить SHOW для sight->article (https://wn.krbl.ru/sight/2/article) meta: { canDelete: true, label: 'Виды', diff --git a/src/pages/sight/show.tsx b/src/pages/sight/show.tsx index bae82af..1beda1b 100644 --- a/src/pages/sight/show.tsx +++ b/src/pages/sight/show.tsx @@ -1,13 +1,101 @@ -import {Stack, Typography} from '@mui/material' +import {Stack, Typography, Box, Grid2 as Grid, Button, MenuItem, Select, FormControl, InputLabel, TextField} from '@mui/material' import {useShow} from '@refinedev/core' -import {Show, TextFieldComponent as TextField} from '@refinedev/mui' +import {Show, TextFieldComponent} from '@refinedev/mui' + +import {useEffect, useState} from 'react' +import axios from 'axios' + +import {BACKEND_URL} from '../../lib/constants' + +type ArticleItem = { + id: number + heading: string + body: string +} export const SightShow = () => { const {query} = useShow({}) const {data, isLoading} = query - const record = data?.data + const [articles, setArticles] = useState([]) + const [linkedArticles, setLinkedArticles] = useState([]) + const [selectedArticleId, setSelectedArticleId] = useState('') + const [pageNum, setPageNum] = useState(1) + const [articlesLoading, setArticlesLoading] = useState(true) + + useEffect(() => { + if (record?.id) { + axios + .get(`${BACKEND_URL}/sight/${record.id}/article`) + .then((response) => { + setLinkedArticles(response?.data || []) + }) + .catch(() => { + setLinkedArticles([]) + }) + } + }, [record?.id]) + + useEffect(() => { + axios + .get(`${BACKEND_URL}/article/`) + .then((response) => { + setArticles(response?.data || []) + setArticlesLoading(false) + }) + .catch(() => { + setArticles([]) + setArticlesLoading(false) + }) + }, []) + + const availableArticles = articles.filter((article) => !linkedArticles.some((linked) => linked.id === article.id)) + + const linkArticle = () => { + if (selectedArticleId) { + const requestData = { + article_id: selectedArticleId, + page_num: pageNum, + } + + axios + .post(`${BACKEND_URL}/sight/${record?.id}/article`, requestData, { + headers: { + accept: 'application/json', + 'Content-Type': 'application/json', + }, + }) + .then(() => { + axios + .get(`${BACKEND_URL}/sight/${record?.id}/article`) + .then((response) => { + setLinkedArticles(response?.data || []) + setPageNum(pageNum + 1) + }) + .catch(() => { + setLinkedArticles([]) + }) + }) + .catch((error) => { + console.error('Error linking article:', error) + }) + } + } + + const deleteArticle = (articleId: number) => { + axios + .delete(`${BACKEND_URL}/sight/${record?.id}/article`, { + data: {article_id: articleId}, + }) + .then(() => { + setLinkedArticles((prev) => prev.filter((item) => item.id !== articleId)) + }) + .catch((error) => { + console.error('Error unlinking article:', error) + }) + } + const fields = [ {label: 'ID', data: 'id'}, {label: 'Name', data: 'name'}, @@ -24,9 +112,70 @@ export const SightShow = () => { {label} - + ))} + + + + Linked Articles + + + + {articlesLoading ? ( + Loading articles... + ) : linkedArticles.length > 0 ? ( + linkedArticles.map((article) => ( + + + + ID: {article.id} + + + Heading: {article.heading} + + + Body: {article.body} + + + + + + )) + ) : ( + No articles linked + )} + + + + + Link Article + + + + + Article + + + + + setPageNum(Number(e.target.value))} fullWidth InputLabelProps={{shrink: true}} /> + + + + + + )