[update] - token service
This commit is contained in:
@@ -1,24 +1,22 @@
|
||||
import React, { useContext, useEffect, useState } from "react";
|
||||
import { Box, Button, Text, useToast } from "@chakra-ui/react";
|
||||
import { useForm, Controller } from "react-hook-form";
|
||||
import { yupResolver } from "@hookform/resolvers/yup";
|
||||
import * as yup from "yup";
|
||||
import { useNavigate, useParams } from "react-router-dom";
|
||||
import { v4 as uuidv4 } from "uuid";
|
||||
import { ArrowBackIcon } from "@chakra-ui/icons";
|
||||
import { OPACITY_ON_LOAD } from "../../Layout/animations";
|
||||
import FormInputMain from "../../Components/FormInputMain";
|
||||
import ToastBox from "../../Components/ToastBox";
|
||||
import FullscreenLoaders from "../../Components/Loaders/FullscreenLoaders";
|
||||
import { Box, Text, useToast } from "@chakra-ui/react";
|
||||
import { yupResolver } from "@hookform/resolvers/yup";
|
||||
import React, { useEffect, useState } from "react";
|
||||
import { useForm } from "react-hook-form";
|
||||
import { useNavigate, useParams } from "react-router-dom";
|
||||
import * as yup from "yup";
|
||||
import CustomAlertDialog from "../../Components/CustomAlertDialog";
|
||||
import FormInputMain from "../../Components/FormInputMain";
|
||||
import FullscreenLoaders from "../../Components/Loaders/FullscreenLoaders";
|
||||
import RoleSwitchButton from "../../Components/RoleSwitchButton";
|
||||
import ToastBox from "../../Components/ToastBox";
|
||||
import { encryptString } from "../../Constants/Constants";
|
||||
import { OPACITY_ON_LOAD } from "../../Layout/animations";
|
||||
import {
|
||||
useCreateSubAdminMutation,
|
||||
useGetSubAdminByIdQuery,
|
||||
useUpdateSubAdminMutation,
|
||||
} from "../../Services/subadmin.service";
|
||||
import { useGetSponserByIdQuery } from "../../Services/io.service";
|
||||
import { encryptString } from "../../Constants/Constants";
|
||||
// ======================= [validation] =========================
|
||||
|
||||
export const addSubAdmin = yup.object().shape({
|
||||
@@ -60,17 +58,15 @@ const SubAdminUpdateCreate = () => {
|
||||
const [isLoadingBtn, setIsLoadingBtn] = useState(false);
|
||||
const [alert, setAlert] = useState(false);
|
||||
const [form, setForm] = useState();
|
||||
const [isSwitchOn, setIsSwitchOn] = useState(true);
|
||||
const [isSwitchOn, setIsSwitchOn] = useState(false);
|
||||
|
||||
const [createSubAdmin] = useCreateSubAdminMutation();
|
||||
const [updateSubAdmin] = useUpdateSubAdminMutation();
|
||||
|
||||
// Fetch sponsor data only if id exists
|
||||
const {
|
||||
data: subAdminByIdData,
|
||||
error,
|
||||
isLoading,
|
||||
} = useGetSubAdminByIdQuery(id, { skip: !id });
|
||||
const { data: subAdminByIdData, isLoading } = useGetSubAdminByIdQuery(id, {
|
||||
skip: !id,
|
||||
});
|
||||
|
||||
// ======================== [validators] ===========================
|
||||
|
||||
@@ -97,11 +93,10 @@ const SubAdminUpdateCreate = () => {
|
||||
subAdminByIdData?.data?.role[0]?.role ===
|
||||
encryptString(import.meta.env.VITE_VITE_MAKER)
|
||||
);
|
||||
console.log(subAdminByIdData?.data?.role);
|
||||
}
|
||||
}, [subAdminByIdData, reset]);
|
||||
|
||||
if (false) {
|
||||
if (isLoading) {
|
||||
return <FullscreenLoaders />;
|
||||
}
|
||||
|
||||
@@ -281,7 +276,7 @@ const SubAdminUpdateCreate = () => {
|
||||
}, {});
|
||||
|
||||
// ==================== [On Submit] ========================
|
||||
console.log(errors);
|
||||
// console.log(errors);
|
||||
|
||||
const onSubmit = async (data) => {
|
||||
console.log("Hit");
|
||||
|
||||
@@ -13,9 +13,9 @@ export const baseQuery = async (args, api, extraOptions) => {
|
||||
})(args, api, extraOptions);
|
||||
|
||||
if (result.error) {
|
||||
if (error.status === 403) {
|
||||
if (result.error.status === 403) {
|
||||
try {
|
||||
const { data } = await fetchBaseQuery({
|
||||
const { data, error } = await fetchBaseQuery({
|
||||
baseUrl: import.meta.env.VITE_BAS_URL,
|
||||
credentials: 'include',
|
||||
prepareHeaders: (headers) => {
|
||||
@@ -41,17 +41,18 @@ export const baseQuery = async (args, api, extraOptions) => {
|
||||
}
|
||||
})(args, api, extraOptions);
|
||||
}
|
||||
throw error;
|
||||
} catch (err) {
|
||||
console.error("Failed to refresh token:", err);
|
||||
localStorage.clear();
|
||||
window.location.href = '/login'; // Redirect to login page
|
||||
// window.location.href = '/login'; // Redirect to login page
|
||||
}
|
||||
} else if (error.status === 401) {
|
||||
} else if (result.error.status === 401) {
|
||||
localStorage.clear();
|
||||
window.location.href = '/login';
|
||||
// window.location.href = '/login';
|
||||
}
|
||||
}
|
||||
console.log(result);
|
||||
// console.log(result);
|
||||
|
||||
return result;
|
||||
};
|
||||
@@ -94,6 +95,6 @@ export const apiSlice = createApi({
|
||||
}),
|
||||
|
||||
}),
|
||||
});
|
||||
});
|
||||
|
||||
export const { useLoginMutation, useRefreshTokenMutation, useLogoutMutation } = apiSlice;
|
||||
|
||||
Reference in New Issue
Block a user