refactor: Api: Использование baseQueryWithAuth
This commit is contained in:
parent
6220c0e802
commit
f2c7f7c2da
@ -1,16 +1,10 @@
|
||||
import {createApi, fetchBaseQuery} from "@reduxjs/toolkit/query/react";
|
||||
import CONFIG from "../Core/сonfig.js";3
|
||||
import {createApi} from "@reduxjs/toolkit/query/react";
|
||||
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||
|
||||
|
||||
export const appointmentTypesApi = createApi({
|
||||
reducerPath: 'appointmentTypesApi',
|
||||
baseQuery: fetchBaseQuery({
|
||||
baseUrl: CONFIG.BASE_URL,
|
||||
prepareHeaders: (headers) => {
|
||||
const token = localStorage.getItem('access_token');
|
||||
if (token) headers.set('Authorization', `Bearer ${token}`);
|
||||
return headers;
|
||||
}
|
||||
}),
|
||||
baseQuery: baseQueryWithAuth,
|
||||
tagsTypes: ['AppointmentTypes'],
|
||||
endpoints: (builder) => ({
|
||||
getAppointmentTypes: builder.query({
|
||||
|
||||
@ -1,16 +1,9 @@
|
||||
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
|
||||
import CONFIG from "../Core/сonfig.js";
|
||||
import {createApi} from "@reduxjs/toolkit/query/react";
|
||||
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||
|
||||
export const appointmentsApi = createApi({
|
||||
reducerPath: 'appointmentsApi',
|
||||
baseQuery: fetchBaseQuery({
|
||||
baseUrl: CONFIG.BASE_URL,
|
||||
prepareHeaders: (headers) => {
|
||||
const token = localStorage.getItem('access_token');
|
||||
if (token) headers.set('Authorization', `Bearer ${token}`);
|
||||
return headers;
|
||||
},
|
||||
}),
|
||||
baseQuery: baseQueryWithAuth,
|
||||
tagTypes: ['Appointment'],
|
||||
endpoints: (builder) => ({
|
||||
getAppointments: builder.query({
|
||||
@ -32,7 +25,7 @@ export const appointmentsApi = createApi({
|
||||
invalidatesTags: ['Appointment'],
|
||||
}),
|
||||
updateAppointment: builder.mutation({
|
||||
query: ({ id, data }) => ({
|
||||
query: ({id, data}) => ({
|
||||
url: `/appointments/${id}/`,
|
||||
method: 'PUT',
|
||||
body: data,
|
||||
|
||||
@ -1,28 +1,6 @@
|
||||
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
|
||||
import { logout } from "../Redux/Slices/authSlice.js";
|
||||
import CONFIG from "../Core/сonfig.js";
|
||||
import {createApi} from "@reduxjs/toolkit/query/react";
|
||||
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||
|
||||
const baseQuery = fetchBaseQuery({
|
||||
baseUrl: CONFIG.BASE_URL,
|
||||
prepareHeaders: (headers) => {
|
||||
const token = localStorage.getItem("access_token");
|
||||
if (token) {
|
||||
headers.set("Authorization", `Bearer ${token}`);
|
||||
}
|
||||
headers.set("Content-Type", "application/json");
|
||||
return headers;
|
||||
},
|
||||
});
|
||||
|
||||
const baseQueryWithAuth = async (args, api, extraOptions) => {
|
||||
const result = await baseQuery(args, api, extraOptions);
|
||||
if (result.error && result.error.status === 401) {
|
||||
localStorage.removeItem("access_token");
|
||||
api.dispatch(logout());
|
||||
window.location.href = "/login";
|
||||
}
|
||||
return result;
|
||||
};
|
||||
|
||||
export const authApi = createApi({
|
||||
reducerPath: "authApi",
|
||||
@ -38,4 +16,4 @@ export const authApi = createApi({
|
||||
}),
|
||||
});
|
||||
|
||||
export const { useLoginMutation } = authApi;
|
||||
export const {useLoginMutation} = authApi;
|
||||
25
web-app/src/Api/baseQuery.js
Normal file
25
web-app/src/Api/baseQuery.js
Normal file
@ -0,0 +1,25 @@
|
||||
import { fetchBaseQuery } from '@reduxjs/toolkit/query/react';
|
||||
import { logout } from '../Redux/Slices/authSlice.js';
|
||||
import CONFIG from "../Core/сonfig.js";
|
||||
|
||||
export const baseQuery = fetchBaseQuery({
|
||||
baseUrl: CONFIG.BASE_URL,
|
||||
prepareHeaders: (headers) => {
|
||||
const token = localStorage.getItem('access_token');
|
||||
if (token) {
|
||||
headers.set('Authorization', `Bearer ${token}`);
|
||||
}
|
||||
headers.set('Content-Type', 'application/json');
|
||||
return headers;
|
||||
},
|
||||
});
|
||||
|
||||
export const baseQueryWithAuth = async (args, api, extraOptions) => {
|
||||
const result = await baseQuery(args, api, extraOptions);
|
||||
if (result.error && result.error.status === 401) {
|
||||
localStorage.removeItem('access_token');
|
||||
api.dispatch(logout());
|
||||
window.location.href = '/login';
|
||||
}
|
||||
return result;
|
||||
};
|
||||
@ -1,17 +1,10 @@
|
||||
import {createApi, fetchBaseQuery} from "@reduxjs/toolkit/query/react";
|
||||
import CONFIG from "../Core/сonfig.js";
|
||||
import {createApi} from "@reduxjs/toolkit/query/react";
|
||||
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||
|
||||
|
||||
export const lensIssuesApi = createApi({
|
||||
reducerPath: 'lensIssuesApi',
|
||||
baseQuery: fetchBaseQuery({
|
||||
baseUrl: CONFIG.BASE_URL,
|
||||
prepareHeaders: (headers) => {
|
||||
const token = localStorage.getItem('access_token');
|
||||
if (token) headers.set('Authorization', `Bearer ${token}`);
|
||||
return headers;
|
||||
}
|
||||
}),
|
||||
baseQuery: baseQueryWithAuth,
|
||||
tagTypes: ['LensIssues'],
|
||||
endpoints: (builder) => ({
|
||||
getLensIssues: builder.query({
|
||||
|
||||
@ -1,17 +1,10 @@
|
||||
import CONFIG from "../Core/сonfig.js";
|
||||
import {createApi, fetchBaseQuery} from "@reduxjs/toolkit/query/react";
|
||||
import {createApi} from "@reduxjs/toolkit/query/react";
|
||||
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||
|
||||
|
||||
export const lensTypesApi = createApi({
|
||||
reducerPath: 'lensTypesApi',
|
||||
baseQuery: fetchBaseQuery({
|
||||
baseUrl: CONFIG.BASE_URL,
|
||||
prepareHeaders: (headers) => {
|
||||
const token = localStorage.getItem('access_token');
|
||||
if (token) headers.set('Authorization', `Bearer ${token}`);
|
||||
return headers;
|
||||
}
|
||||
}),
|
||||
baseQuery: baseQueryWithAuth,
|
||||
endpoints: (builder) => ({
|
||||
getLensTypes: builder.query({
|
||||
query: () => `/lens_types/`,
|
||||
|
||||
@ -1,17 +1,10 @@
|
||||
import {createApi, fetchBaseQuery} from "@reduxjs/toolkit/query/react";
|
||||
import CONFIG from "../Core/сonfig.js";
|
||||
import {createApi} from "@reduxjs/toolkit/query/react";
|
||||
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||
|
||||
|
||||
export const lensesApi = createApi({
|
||||
reducerPath: 'lensesApi',
|
||||
baseQuery: fetchBaseQuery({
|
||||
baseUrl: CONFIG.BASE_URL,
|
||||
prepareHeaders: (headers) => {
|
||||
const token = localStorage.getItem('access_token');
|
||||
if (token) headers.set('Authorization', `Bearer ${token}`);
|
||||
return headers;
|
||||
}
|
||||
}),
|
||||
baseQuery: baseQueryWithAuth,
|
||||
tagTypes: ['Lens'],
|
||||
endpoints: (builder) => ({
|
||||
getLenses: builder.query({
|
||||
@ -33,7 +26,7 @@ export const lensesApi = createApi({
|
||||
invalidatesTags: ['Lens']
|
||||
}),
|
||||
updateLens: builder.mutation({
|
||||
query: ({ id, ...lens }) => ({
|
||||
query: ({id, ...lens}) => ({
|
||||
url: `/lenses/${id}/`,
|
||||
method: 'PUT',
|
||||
body: lens
|
||||
|
||||
@ -1,16 +1,9 @@
|
||||
import {createApi, fetchBaseQuery} from '@reduxjs/toolkit/query/react'
|
||||
import CONFIG from "../Core/сonfig.js";
|
||||
import {createApi} from '@reduxjs/toolkit/query/react'
|
||||
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||
|
||||
export const patientsApi = createApi({
|
||||
reducerPath: 'patientsApi',
|
||||
baseQuery: fetchBaseQuery({
|
||||
baseUrl: CONFIG.BASE_URL,
|
||||
prepareHeaders: (headers) => {
|
||||
const token = localStorage.getItem('access_token');
|
||||
if (token) headers.set('Authorization', `Bearer ${token}`);
|
||||
return headers;
|
||||
}
|
||||
}),
|
||||
baseQuery: baseQueryWithAuth,
|
||||
tagTypes: ['Patient'],
|
||||
endpoints: (builder) => ({
|
||||
getPatients: builder.query({
|
||||
|
||||
@ -1,16 +1,9 @@
|
||||
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
|
||||
import CONFIG from "../Core/сonfig.js";
|
||||
import {createApi} from "@reduxjs/toolkit/query/react";
|
||||
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||
|
||||
export const scheduledAppointmentsApi = createApi({
|
||||
reducerPath: 'scheduledAppointmentsApi',
|
||||
baseQuery: fetchBaseQuery({
|
||||
baseUrl: CONFIG.BASE_URL,
|
||||
prepareHeaders: (headers) => {
|
||||
const token = localStorage.getItem('access_token');
|
||||
if (token) headers.set('Authorization', `Bearer ${token}`);
|
||||
return headers;
|
||||
},
|
||||
}),
|
||||
baseQuery: baseQueryWithAuth,
|
||||
tagTypes: ['ScheduledAppointment'],
|
||||
endpoints: (builder) => ({
|
||||
getScheduledAppointments: builder.query({
|
||||
@ -26,7 +19,7 @@ export const scheduledAppointmentsApi = createApi({
|
||||
invalidatesTags: ['ScheduledAppointment'],
|
||||
}),
|
||||
updateScheduledAppointment: builder.mutation({
|
||||
query: ({ id, data }) => ({
|
||||
query: ({id, data}) => ({
|
||||
url: `/scheduled_appointments/${id}/`,
|
||||
method: 'PUT',
|
||||
body: data,
|
||||
|
||||
@ -1,16 +1,9 @@
|
||||
import {createApi, fetchBaseQuery} from '@reduxjs/toolkit/query/react';
|
||||
import CONFIG from "../Core/сonfig.js";
|
||||
import {createApi} from '@reduxjs/toolkit/query/react';
|
||||
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||
|
||||
export const setContentApi = createApi({
|
||||
reducerPath: 'setContentApi',
|
||||
baseQuery: fetchBaseQuery({
|
||||
baseUrl: CONFIG.BASE_URL,
|
||||
prepareHeaders: (headers) => {
|
||||
const token = localStorage.getItem('access_token');
|
||||
if (token) headers.set('Authorization', `Bearer ${token}`);
|
||||
return headers;
|
||||
}
|
||||
}),
|
||||
baseQuery: baseQueryWithAuth,
|
||||
tagTypes: ['SetContent'],
|
||||
endpoints: (builder) => ({
|
||||
getSetContent: builder.query({
|
||||
|
||||
@ -1,16 +1,9 @@
|
||||
import {createApi, fetchBaseQuery} from '@reduxjs/toolkit/query/react'
|
||||
import CONFIG from "../Core/сonfig.js";
|
||||
import {createApi} from '@reduxjs/toolkit/query/react'
|
||||
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||
|
||||
export const setsApi = createApi({
|
||||
reducerPath: 'setsApi',
|
||||
baseQuery: fetchBaseQuery({
|
||||
baseUrl: CONFIG.BASE_URL,
|
||||
prepareHeaders: (headers) => {
|
||||
const token = localStorage.getItem('access_token');
|
||||
if (token) headers.set('Authorization', `Bearer ${token}`);
|
||||
return headers;
|
||||
}
|
||||
}),
|
||||
baseQuery: baseQueryWithAuth,
|
||||
tagTypes: ['Set'],
|
||||
endpoints: (builder) => ({
|
||||
getSets: builder.query({
|
||||
|
||||
@ -1,17 +1,10 @@
|
||||
import {createApi, fetchBaseQuery} from "@reduxjs/toolkit/query/react";
|
||||
import CONFIG from "../Core/сonfig.js";
|
||||
import {createApi} from "@reduxjs/toolkit/query/react";
|
||||
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||
|
||||
|
||||
export const usersApi = createApi({
|
||||
reducerPath: 'usersApi',
|
||||
baseQuery: fetchBaseQuery({
|
||||
baseUrl: CONFIG.BASE_URL,
|
||||
prepareHeaders: (headers) => {
|
||||
const token = localStorage.getItem('access_token');
|
||||
if (token) headers.set('Authorization', `Bearer ${token}`);
|
||||
return headers;
|
||||
},
|
||||
}),
|
||||
baseQuery: baseQueryWithAuth,
|
||||
tagTypes: ['User'],
|
||||
endpoints: (builder) => ({
|
||||
getAuthenticatedUserData: builder.query({
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user