diff --git a/src/pages/theme/Values.tsx b/src/pages/theme/Values.tsx index 6253add..4d9f0ac 100644 --- a/src/pages/theme/Values.tsx +++ b/src/pages/theme/Values.tsx @@ -1,18 +1,25 @@ import { type FC, useState } from 'react' -import { useGetThemeValues } from './hooks/useThemeData' +import { useDeleteThemeValue, useGetThemeValues } from './hooks/useThemeData' import { useParams } from 'react-router-dom' import PageTitle from '@/components/PageTitle' import Td from '@/components/Td' -import { Eye, Add } from 'iconsax-react' +import { Edit, Add } from 'iconsax-react' import TrashWithConfrim from '@/components/TrashWithConfrim' import { toast } from 'react-toastify' import Button from '@/components/Button' import CreateThemeValueModal from './components/CreateThemeValueModal' +import UpdateThemeValueModal from './components/UpdateThemeValueModal' +import type { ThemeValueType } from './type/Types' +import { extractErrorMessage } from '@/helpers/utils' const Values: FC = () => { + const { id } = useParams() + const { mutate: deleteThemeValue, isPending: isDeletingThemeValue } = useDeleteThemeValue() const { data, isLoading } = useGetThemeValues(id!) const [isModalOpen, setIsModalOpen] = useState(false) + const [isUpdateModalOpen, setIsUpdateModalOpen] = useState(false) + const [selectedThemeValue, setSelectedThemeValue] = useState(null) if (isLoading) { return
در حال بارگذاری...
@@ -22,7 +29,7 @@ const Values: FC = () => { return (
-
+
{ - toast.error('این قابلیت در حال توسعه است') + deleteThemeValue(item._id, { + onSuccess: () => { + toast.success('مقدار تم با موفقیت حذف شد') + }, + onError: (error) => { + toast.error(extractErrorMessage(error)) + } + }) }} - isLoading={false} + isLoading={isDeletingThemeValue} />
@@ -100,6 +122,15 @@ const Values: FC = () => { themeId={id} /> )} + + { + setIsUpdateModalOpen(false) + setSelectedThemeValue(null) + }} + themeValue={selectedThemeValue} + />
) } diff --git a/src/pages/theme/components/UpdateThemeValueModal.tsx b/src/pages/theme/components/UpdateThemeValueModal.tsx new file mode 100644 index 0000000..393f013 --- /dev/null +++ b/src/pages/theme/components/UpdateThemeValueModal.tsx @@ -0,0 +1,127 @@ +import { type FC, useEffect } from 'react' +import { useFormik } from 'formik' +import * as Yup from 'yup' +import DefaulModal from '@/components/DefaulModal' +import Input from '@/components/Input' +import Button from '@/components/Button' +import { useUpdateThemeValue } from '../hooks/useThemeData' +import { extractErrorMessage } from '@/helpers/utils' +import { toast } from 'react-toastify' +import { TickCircle } from 'iconsax-react' +import type { UpdateThemeValueType, ThemeValueType } from '../type/Types' + +interface Props { + open: boolean + close: () => void + themeValue: ThemeValueType | null +} + +const UpdateThemeValueModal: FC = ({ open, close, themeValue }) => { + const { mutate: updateThemeValue, isPending } = useUpdateThemeValue() + + const formik = useFormik({ + initialValues: { + name: '', + value: '', + }, + validationSchema: Yup.object({ + name: Yup.string().required('نام الزامی است'), + value: Yup.string().required('مقدار الزامی است'), + }), + onSubmit: (values) => { + if (!themeValue?._id) return + + updateThemeValue( + { + id: themeValue._id, + params: { + name: values.name, + value: values.value, + }, + }, + { + onSuccess: () => { + toast.success('مقدار تم با موفقیت به‌روزرسانی شد') + formik.resetForm() + close() + }, + onError: (error) => { + toast.error(extractErrorMessage(error)) + }, + } + ) + }, + }) + + useEffect(() => { + if (themeValue && open) { + formik.setValues({ + name: themeValue.name, + value: themeValue.value.toString(), + }) + } + }, [themeValue, open]) + + const handleClose = () => { + formik.resetForm() + close() + } + + return ( + +
+ + + + +
+ + +
+
+
+ ) +} + +export default UpdateThemeValueModal + diff --git a/src/pages/theme/hooks/useThemeData.ts b/src/pages/theme/hooks/useThemeData.ts index 009cff6..d8ef837 100644 --- a/src/pages/theme/hooks/useThemeData.ts +++ b/src/pages/theme/hooks/useThemeData.ts @@ -1,6 +1,6 @@ import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; import * as api from "../service/ThemeService"; -import type { CreateThemeType } from "../type/Types"; +import type { CreateThemeType, UpdateThemeValueType } from "../type/Types"; export const useGetThemes = () => { return useQuery({ @@ -66,3 +66,29 @@ export const useCreateThemeValue = () => { }, }); }; + +export const useUpdateThemeValue = () => { + const queryClient = useQueryClient(); + return useMutation({ + mutationFn: ({ + id, + params, + }: { + id: string; + params: UpdateThemeValueType; + }) => api.updateThemeValue(id, params), + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: ["theme-values"] }); + }, + }); +}; + +export const useDeleteThemeValue = () => { + const queryClient = useQueryClient(); + return useMutation({ + mutationFn: api.deleteThemeValue, + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: ["theme-values"] }); + }, + }); +}; diff --git a/src/pages/theme/service/ThemeService.ts b/src/pages/theme/service/ThemeService.ts index 31fd37a..1d23089 100644 --- a/src/pages/theme/service/ThemeService.ts +++ b/src/pages/theme/service/ThemeService.ts @@ -5,6 +5,7 @@ import type { GetThemeByIdResponseType, GetThemeValuesResponseType, CreateThemeValueType, + UpdateThemeValueType, } from "../type/Types"; export const getThemes = async (): Promise => { @@ -57,18 +58,13 @@ export const createThemeValue = async (params: CreateThemeValueType) => { export const updateThemeValue = async ( id: string, - params: CreateThemeValueType + params: UpdateThemeValueType ) => { - const { data } = await axios.put( - `/admin/category/theme-value/${id}/value`, - params - ); + const { data } = await axios.put(`/admin/category/theme-value/${id}`, params); return data; }; export const deleteThemeValue = async (id: string) => { - const { data } = await axios.delete( - `/admin/category/theme-value/${id}/value` - ); + const { data } = await axios.delete(`/admin/category/theme-value/${id}`); return data; }; diff --git a/src/pages/theme/type/Types.ts b/src/pages/theme/type/Types.ts index 3ba0f53..0261329 100644 --- a/src/pages/theme/type/Types.ts +++ b/src/pages/theme/type/Types.ts @@ -35,6 +35,11 @@ export type CreateThemeValueType = { value: string; }; +export type UpdateThemeValueType = { + name: string; + value: string; +}; + export type ThemeValueType = { _id: string; theme: ThemeType;