From 7d864ccf6bf7cfd676a6279e51f5e9efaad70565 Mon Sep 17 00:00:00 2001 From: Amirhossein Mahmoodi Date: Sat, 13 Jul 2024 14:02:55 +0330 Subject: [PATCH] add form create inquiryPrivacyFencing --- .../Create/Form/index.jsx | 5 ++-- .../inquiryPrivacyFencing/Create/index.jsx | 4 +-- src/core/utils/routes.js | 2 ++ src/lib/hooks/useRequest.js | 27 ++++++++++++++++++- 4 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/components/dashboard/inquiryPrivacyFencing/Create/Form/index.jsx b/src/components/dashboard/inquiryPrivacyFencing/Create/Form/index.jsx index 057995e..9fff4da 100644 --- a/src/components/dashboard/inquiryPrivacyFencing/Create/Form/index.jsx +++ b/src/components/dashboard/inquiryPrivacyFencing/Create/Form/index.jsx @@ -9,7 +9,7 @@ import moment from "jalali-moment"; import { Controller, useForm } from "react-hook-form"; import LocationOnMap from "./LocationOnMap"; -const CreateForm = ({ handleClose }) => { +const CreateForm = ({ handleClose, mutate }) => { const { control, watch, register, handleSubmit, setValue } = useForm({ defaultValues: { dabirkhaneh_number: '', @@ -130,6 +130,7 @@ const CreateForm = ({ handleClose }) => { try { await request(SET_INQUIRE_PRIVACY_FENCING, 'post', { data: formData }) handleClose() + mutate() } catch (error) { } @@ -441,7 +442,7 @@ const CreateForm = ({ handleClose }) => { value.map((option, index) => { const { key, ...tagProps } = getTagProps({ index }); return ( - + ); }) } diff --git a/src/components/dashboard/inquiryPrivacyFencing/Create/index.jsx b/src/components/dashboard/inquiryPrivacyFencing/Create/index.jsx index 42bb2af..9010277 100644 --- a/src/components/dashboard/inquiryPrivacyFencing/Create/index.jsx +++ b/src/components/dashboard/inquiryPrivacyFencing/Create/index.jsx @@ -4,7 +4,7 @@ import { Close } from "@mui/icons-material"; import { Dialog, DialogTitle, IconButton } from "@mui/material"; import CreateForm from "./Form"; -const InquiryPrivacyFencingCreate = ({ open, handleClose }) => { +const InquiryPrivacyFencingCreate = ({ open, handleClose, mutate }) => { return ( <> @@ -24,7 +24,7 @@ const InquiryPrivacyFencingCreate = ({ open, handleClose }) => { > - + ); diff --git a/src/core/utils/routes.js b/src/core/utils/routes.js index de863af..29a17a8 100644 --- a/src/core/utils/routes.js +++ b/src/core/utils/routes.js @@ -1,5 +1,7 @@ const api = process.env.NEXT_PUBLIC_API_URL; +export const GET_CSRF = api + "/csrf"; + export const GET_USER_ROUTE = api + "/webapi/user/get-permission"; export const GET_LOGIN_ROUTE = api + "/login_dev"; diff --git a/src/lib/hooks/useRequest.js b/src/lib/hooks/useRequest.js index 2e35512..8805a56 100644 --- a/src/lib/hooks/useRequest.js +++ b/src/lib/hooks/useRequest.js @@ -1,7 +1,18 @@ import { errorResponse } from "@/core/utils/errorResponse"; +import { GET_CSRF } from "@/core/utils/routes"; import { successRequest } from "@/core/utils/successRequest"; import axios from "axios"; +const getCsrfToken = async () => { + try { + const response = await axios.get(GET_CSRF); + return response.data.data; + } catch (error) { + console.error("Error fetching CSRF token:", error); + throw error; + } +}; + const defaultOptions = { data: {}, requestOptions: { @@ -15,13 +26,27 @@ const defaultOptions = { }; const useRequest = (initOptions) => { + const instance = axios.create(); + instance.interceptors.request.use( + async function (config) { + if (config.method !== 'get') { + const csrfToken = await getCsrfToken(); + config.headers['X-CSRF-TOKEN'] = csrfToken; + } + return config; + }, + function (error) { + return Promise.reject(error); + } + ); + const _options = Object.assign({}, defaultOptions, initOptions); return async (url = "", method = "get", options) => { const mergedOptions = Object.assign({}, _options, options); try { - const response = await axios({ + const response = await instance({ url, method, data: method === "get" ? null : mergedOptions.data,