This commit is contained in:
2025-02-10 19:34:29 +05:30
parent 3aa93b358f
commit 18c9ff8b78
16 changed files with 412 additions and 8 deletions

View File

@@ -6,9 +6,17 @@ import { RootState } from "../Store";
const baseQuery = fetchBaseQuery({
baseUrl: `${import.meta.env.VITE_API_URL}`,
prepareHeaders: (headers, { getState }) => {
const token = (getState() as RootState).auth.token; // Get token from Redux store
// Encode Basic Auth Credentials
const username = import.meta.env.VITE_USER_NAME||''; // Replace with actual username
const password = import.meta.env.VITE_PASSWORD||''; // Replace with actual password
const basicAuth = `${username} : ${password}`; // Encode to Base64
if (token) {
headers.set("Authorization", `Bearer ${token}`);
headers.set("Authorization", `Basic ${basicAuth}`);
headers.set("access-token", `Bearer ${token}`);
}
headers.set("Content-Type", "application/json");
return headers;
@@ -16,7 +24,7 @@ const baseQuery = fetchBaseQuery({
});
// ✅ Handle 401 Errors (Auto Logout)
const baseQueryWithReauth: BaseQueryFn<
export const baseQueryWithReauth: BaseQueryFn<
string | FetchArgs,
unknown,
FetchBaseQueryError
@@ -30,7 +38,10 @@ const baseQueryWithReauth: BaseQueryFn<
return result;
};
export const apiSlice = createApi({
export const dashboard = createApi({
reducerPath: "api",
baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
endpoints: (builder) => ({
@@ -46,7 +57,7 @@ export const apiSlice = createApi({
}),
});
export const { useGetPostsQuery } = apiSlice;
export const { useGetPostsQuery } = dashboard;
export type Post = {
id: number;

View File

@@ -0,0 +1,26 @@
import { createApi } from "@reduxjs/toolkit/query";
import { baseQueryWithReauth } from "./apiSlice";
export const deactivatedAccounts = createApi({
reducerPath: "api",
baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
endpoints: (builder) => ({
getPosts: builder.query<Post[], void>({ query: () => "/posts" }),
}),
});
export const { } = deactivatedAccounts;
export type Post = {
id: number;
title: string;
body: string;
};

View File

@@ -0,0 +1,26 @@
import { createApi } from "@reduxjs/toolkit/query";
import { baseQueryWithReauth } from "./apiSlice";
export const faqs = createApi({
reducerPath: "api",
baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
endpoints: (builder) => ({
getPosts: builder.query<Post[], void>({ query: () => "/posts" }),
}),
});
export const { } = faqs;
export type Post = {
id: number;
title: string;
body: string;
};

View File

@@ -0,0 +1,26 @@
import { createApi } from "@reduxjs/toolkit/query";
import { baseQueryWithReauth } from "./apiSlice";
export const registerUser = createApi({
reducerPath: "api",
baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
endpoints: (builder) => ({
getPosts: builder.query<Post[], void>({ query: () => "/posts" }),
}),
});
export const { } = registerUser;
export type Post = {
id: number;
title: string;
body: string;
};

View File

@@ -0,0 +1,26 @@
import { createApi } from "@reduxjs/toolkit/query";
import { baseQueryWithReauth } from "./apiSlice";
export const registerUser = createApi({
reducerPath: "api",
baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
endpoints: (builder) => ({
getPosts: builder.query<Post[], void>({ query: () => "/posts" }),
}),
});
export const { } = registerUser;
export type Post = {
id: number;
title: string;
body: string;
};

View File

@@ -0,0 +1,26 @@
import { createApi } from "@reduxjs/toolkit/query";
import { baseQueryWithReauth } from "./apiSlice";
export const registerUser = createApi({
reducerPath: "api",
baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
endpoints: (builder) => ({
getPosts: builder.query<Post[], void>({ query: () => "/posts" }),
}),
});
export const { } = registerUser;
export type Post = {
id: number;
title: string;
body: string;
};

View File

@@ -0,0 +1,26 @@
import { createApi } from "@reduxjs/toolkit/query";
import { baseQueryWithReauth } from "./apiSlice";
export const registerUser = createApi({
reducerPath: "api",
baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
endpoints: (builder) => ({
getPosts: builder.query<Post[], void>({ query: () => "/posts" }),
}),
});
export const { } = registerUser;
export type Post = {
id: number;
title: string;
body: string;
};

View File

@@ -0,0 +1,26 @@
import { createApi } from "@reduxjs/toolkit/query";
import { baseQueryWithReauth } from "./apiSlice";
export const managePosts = createApi({
reducerPath: "api",
baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
endpoints: (builder) => ({
getPosts: builder.query<Post[], void>({ query: () => "/posts" }),
}),
});
export const { } = managePosts;
export type Post = {
id: number;
title: string;
body: string;
};

View File

@@ -0,0 +1,26 @@
import { createApi } from "@reduxjs/toolkit/query";
import { baseQueryWithReauth } from "./apiSlice";
export const manageSubAdmin = createApi({
reducerPath: "api",
baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
endpoints: (builder) => ({
getPosts: builder.query<Post[], void>({ query: () => "/posts" }),
}),
});
export const { } = manageSubAdmin;
export type Post = {
id: number;
title: string;
body: string;
};

View File

@@ -0,0 +1,26 @@
import { createApi } from "@reduxjs/toolkit/query";
import { baseQueryWithReauth } from "./apiSlice";
export const registerUser = createApi({
reducerPath: "api",
baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
endpoints: (builder) => ({
getPosts: builder.query<Post[], void>({ query: () => "/posts" }),
}),
});
export const { } = registerUser;
export type Post = {
id: number;
title: string;
body: string;
};

View File

@@ -0,0 +1,26 @@
import { createApi } from "@reduxjs/toolkit/query";
import { baseQueryWithReauth } from "./apiSlice";
export const registerUser = createApi({
reducerPath: "api",
baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
endpoints: (builder) => ({
getPosts: builder.query<Post[], void>({ query: () => "/posts" }),
}),
});
export const { } = registerUser;
export type Post = {
id: number;
title: string;
body: string;
};

View File

@@ -0,0 +1,26 @@
import { createApi } from "@reduxjs/toolkit/query";
import { baseQueryWithReauth } from "./apiSlice";
export const registerUser = createApi({
reducerPath: "api",
baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
endpoints: (builder) => ({
getPosts: builder.query<Post[], void>({ query: () => "/posts" }),
}),
});
export const { } = registerUser;
export type Post = {
id: number;
title: string;
body: string;
};

View File

@@ -0,0 +1,26 @@
import { createApi } from "@reduxjs/toolkit/query";
import { baseQueryWithReauth } from "./apiSlice";
export const registerUser = createApi({
reducerPath: "api",
baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
endpoints: (builder) => ({
getPosts: builder.query<Post[], void>({ query: () => "/posts" }),
}),
});
export const { } = registerUser;
export type Post = {
id: number;
title: string;
body: string;
};

View File

@@ -0,0 +1,26 @@
import { createApi } from "@reduxjs/toolkit/query";
import { baseQueryWithReauth } from "./apiSlice";
export const registerUser = createApi({
reducerPath: "api",
baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
endpoints: (builder) => ({
getPosts: builder.query<Post[], void>({ query: () => "/posts" }),
}),
});
export const { } = registerUser;
export type Post = {
id: number;
title: string;
body: string;
};

View File

@@ -0,0 +1,26 @@
import { createApi } from "@reduxjs/toolkit/query";
import { baseQueryWithReauth } from "./apiSlice";
export const registerUser = createApi({
reducerPath: "api",
baseQuery: baseQueryWithReauth, // Use enhanced baseQuery with error handling
endpoints: (builder) => ({
getPosts: builder.query<Post[], void>({ query: () => "/posts" }),
}),
});
export const { } = registerUser;
export type Post = {
id: number;
title: string;
body: string;
};

View File

@@ -1,14 +1,43 @@
import { configureStore } from "@reduxjs/toolkit";
import { apiSlice } from "./Service/apiSlice";
import authReducer from "./Service/authSlice"
import { dashboard } from "./Service/apiSlice";
import authReducer from "./Service/authSlice";
import { registerUser } from "./Service/register.user.service";
import { deactivatedAccounts } from "./Service/deactivated.account.service";
import { faqs } from "./Service/faqs.service";
import { managePosts } from "./Service/manage.posts.service";
import { manageSubAdmin } from "./Service/manage.subadmin.service";
export const store = configureStore({
reducer: {
[apiSlice.reducerPath]: apiSlice.reducer,
[dashboard.reducerPath]: dashboard.reducer,
[registerUser.reducerPath]: registerUser.reducer,
[deactivatedAccounts.reducerPath]: deactivatedAccounts.reducer,
[faqs.reducerPath]: faqs.reducer,
[managePosts.reducerPath]: managePosts.reducer,
[manageSubAdmin.reducerPath]: manageSubAdmin.reducer,
[registerUser.reducerPath]: registerUser.reducer,
[registerUser.reducerPath]: registerUser.reducer,
[registerUser.reducerPath]: registerUser.reducer,
[registerUser.reducerPath]: registerUser.reducer,
[registerUser.reducerPath]: registerUser.reducer,
[registerUser.reducerPath]: registerUser.reducer,
auth: authReducer,
},
middleware: (getDefaultMiddleware) =>
getDefaultMiddleware().concat(apiSlice.middleware),
getDefaultMiddleware().concat(
dashboard.middleware,
registerUser.middleware,
deactivatedAccounts.middleware,
managePosts.middleware,
faqs.middleware,
manageSubAdmin.middleware,
),
});
export type RootState = ReturnType<typeof store.getState>;