diff --git a/src/pages/theme/List.tsx b/src/pages/theme/List.tsx
index 98a1e21..2cf69fc 100644
--- a/src/pages/theme/List.tsx
+++ b/src/pages/theme/List.tsx
@@ -50,7 +50,7 @@ const ThemeList: FC = () => {
|
-
+
{
+ const { id } = useParams<{ id: string }>()
+ const navigate = useNavigate()
+ const { data } = useGetThemeById(id!)
+ const { mutate: updateTheme, isPending } = useUpdateTheme()
+
+ const formik = useFormik({
+ initialValues: {
+ title: '',
+ inputType: ThemeInputType.Color,
+ },
+ validationSchema: Yup.object({
+ title: Yup.string().required('عنوان تم الزامی است'),
+ inputType: Yup.string().required('نوع ورودی تم الزامی است'),
+ }),
+ onSubmit: (values) => {
+ updateTheme({ id: id!, params: values }, {
+ onSuccess: () => {
+ toast.success('تم با موفقیت ویرایش شد')
+ navigate(Pages.products.theme.list)
+ },
+ onError: (error) => {
+ toast.error(extractErrorMessage(error))
+ }
+ })
+ },
+ })
+
+ useEffect(() => {
+ if (data?.results?.data) {
+ formik.setValues({
+ title: data?.results?.data?.title,
+ inputType: data?.results?.data?.inputType,
+ })
+ }
+ // eslint-disable-next-line react-hooks/exhaustive-deps
+ }, [data])
+
+ return (
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+
+ )
+}
+
+export default UpdateTheme
\ No newline at end of file
diff --git a/src/pages/theme/hooks/useThemeData.ts b/src/pages/theme/hooks/useThemeData.ts
index cd892fe..496e16b 100644
--- a/src/pages/theme/hooks/useThemeData.ts
+++ b/src/pages/theme/hooks/useThemeData.ts
@@ -1,5 +1,6 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import * as api from "../service/ThemeService";
+import type { CreateThemeType } from "../type/Types";
export const useGetThemes = () => {
return useQuery({
@@ -17,3 +18,33 @@ export const useCreateTheme = () => {
},
});
};
+
+export const useGetThemeById = (id: string) => {
+ return useQuery({
+ queryKey: ["theme", id],
+ queryFn: () => api.getThemeById(id),
+ enabled: !!id,
+ });
+};
+
+export const useUpdateTheme = () => {
+ const queryClient = useQueryClient();
+ return useMutation({
+ mutationFn: ({ id, params }: { id: string; params: CreateThemeType }) =>
+ api.updateTheme(id, params),
+ onSuccess: () => {
+ queryClient.invalidateQueries({ queryKey: ["theme"] });
+ queryClient.invalidateQueries({ queryKey: ["themes"] });
+ },
+ });
+};
+
+export const useDeleteTheme = () => {
+ const queryClient = useQueryClient();
+ return useMutation({
+ mutationFn: api.deleteTheme,
+ onSuccess: () => {
+ queryClient.invalidateQueries({ queryKey: ["themes"] });
+ },
+ });
+};
diff --git a/src/pages/theme/service/ThemeService.ts b/src/pages/theme/service/ThemeService.ts
index 517c091..d431177 100644
--- a/src/pages/theme/service/ThemeService.ts
+++ b/src/pages/theme/service/ThemeService.ts
@@ -1,5 +1,9 @@
import axios from "@/config/axios";
-import type { CreateThemeType, GetThemesResponseType } from "../type/Types";
+import type {
+ CreateThemeType,
+ GetThemesResponseType,
+ GetThemeByIdResponseType,
+} from "../type/Types";
export const getThemes = async (): Promise => {
const { data } = await axios.get(
@@ -12,3 +16,25 @@ export const createTheme = async (params: CreateThemeType) => {
const { data } = await axios.post("/admin/category/theme", params);
return data;
};
+
+export const getThemeById = async (
+ id: string
+): Promise => {
+ const { data } = await axios.get(
+ `/admin/category/theme/${id}`
+ );
+ return data;
+};
+
+export const updateTheme = async (id: string, params: CreateThemeType) => {
+ const { data } = await axios.put(`/admin/category/theme`, {
+ themeId: id,
+ ...params,
+ });
+ return data;
+};
+
+export const deleteTheme = async (id: string) => {
+ const { data } = await axios.delete(`/admin/category/theme/${id}`);
+ return data;
+};
diff --git a/src/pages/theme/type/Types.ts b/src/pages/theme/type/Types.ts
index 7755277..69aaf7e 100644
--- a/src/pages/theme/type/Types.ts
+++ b/src/pages/theme/type/Types.ts
@@ -20,3 +20,11 @@ export type GetThemesResponseType = {
data: ThemeType[];
};
};
+
+export type GetThemeByIdResponseType = {
+ status: number;
+ success: boolean;
+ results: {
+ data: ThemeType;
+ };
+};
diff --git a/src/router/Main.tsx b/src/router/Main.tsx
index 80dfd12..944f5aa 100644
--- a/src/router/Main.tsx
+++ b/src/router/Main.tsx
@@ -85,6 +85,7 @@ import ReturnDetails from '@/pages/orders/ReturnDetails'
import Cancels from '@/pages/orders/Cancels'
import ThemeList from '@/pages/theme/List'
import CreateTheme from '@/pages/theme/Create'
+import UpdateTheme from '@/pages/theme/Update'
const MainRouter: FC = () => {
const { hasSubMenu } = useSharedStore()
@@ -208,6 +209,7 @@ const MainRouter: FC = () => {
} />
} />
+ } />
|