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 {createApi} from "@reduxjs/toolkit/query/react";
|
||||||
import CONFIG from "../Core/сonfig.js";3
|
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||||
|
|
||||||
|
|
||||||
export const appointmentTypesApi = createApi({
|
export const appointmentTypesApi = createApi({
|
||||||
reducerPath: 'appointmentTypesApi',
|
reducerPath: 'appointmentTypesApi',
|
||||||
baseQuery: fetchBaseQuery({
|
baseQuery: baseQueryWithAuth,
|
||||||
baseUrl: CONFIG.BASE_URL,
|
|
||||||
prepareHeaders: (headers) => {
|
|
||||||
const token = localStorage.getItem('access_token');
|
|
||||||
if (token) headers.set('Authorization', `Bearer ${token}`);
|
|
||||||
return headers;
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
tagsTypes: ['AppointmentTypes'],
|
tagsTypes: ['AppointmentTypes'],
|
||||||
endpoints: (builder) => ({
|
endpoints: (builder) => ({
|
||||||
getAppointmentTypes: builder.query({
|
getAppointmentTypes: builder.query({
|
||||||
|
|||||||
@ -1,16 +1,9 @@
|
|||||||
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
|
import {createApi} from "@reduxjs/toolkit/query/react";
|
||||||
import CONFIG from "../Core/сonfig.js";
|
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||||
|
|
||||||
export const appointmentsApi = createApi({
|
export const appointmentsApi = createApi({
|
||||||
reducerPath: 'appointmentsApi',
|
reducerPath: 'appointmentsApi',
|
||||||
baseQuery: fetchBaseQuery({
|
baseQuery: baseQueryWithAuth,
|
||||||
baseUrl: CONFIG.BASE_URL,
|
|
||||||
prepareHeaders: (headers) => {
|
|
||||||
const token = localStorage.getItem('access_token');
|
|
||||||
if (token) headers.set('Authorization', `Bearer ${token}`);
|
|
||||||
return headers;
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
tagTypes: ['Appointment'],
|
tagTypes: ['Appointment'],
|
||||||
endpoints: (builder) => ({
|
endpoints: (builder) => ({
|
||||||
getAppointments: builder.query({
|
getAppointments: builder.query({
|
||||||
@ -32,7 +25,7 @@ export const appointmentsApi = createApi({
|
|||||||
invalidatesTags: ['Appointment'],
|
invalidatesTags: ['Appointment'],
|
||||||
}),
|
}),
|
||||||
updateAppointment: builder.mutation({
|
updateAppointment: builder.mutation({
|
||||||
query: ({ id, data }) => ({
|
query: ({id, data}) => ({
|
||||||
url: `/appointments/${id}/`,
|
url: `/appointments/${id}/`,
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
body: data,
|
body: data,
|
||||||
|
|||||||
@ -1,28 +1,6 @@
|
|||||||
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
|
import {createApi} from "@reduxjs/toolkit/query/react";
|
||||||
import { logout } from "../Redux/Slices/authSlice.js";
|
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||||
import CONFIG from "../Core/сonfig.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({
|
export const authApi = createApi({
|
||||||
reducerPath: "authApi",
|
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 {createApi} from "@reduxjs/toolkit/query/react";
|
||||||
import CONFIG from "../Core/сonfig.js";
|
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||||
|
|
||||||
|
|
||||||
export const lensIssuesApi = createApi({
|
export const lensIssuesApi = createApi({
|
||||||
reducerPath: 'lensIssuesApi',
|
reducerPath: 'lensIssuesApi',
|
||||||
baseQuery: fetchBaseQuery({
|
baseQuery: baseQueryWithAuth,
|
||||||
baseUrl: CONFIG.BASE_URL,
|
|
||||||
prepareHeaders: (headers) => {
|
|
||||||
const token = localStorage.getItem('access_token');
|
|
||||||
if (token) headers.set('Authorization', `Bearer ${token}`);
|
|
||||||
return headers;
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
tagTypes: ['LensIssues'],
|
tagTypes: ['LensIssues'],
|
||||||
endpoints: (builder) => ({
|
endpoints: (builder) => ({
|
||||||
getLensIssues: builder.query({
|
getLensIssues: builder.query({
|
||||||
|
|||||||
@ -1,17 +1,10 @@
|
|||||||
import CONFIG from "../Core/сonfig.js";
|
import {createApi} from "@reduxjs/toolkit/query/react";
|
||||||
import {createApi, fetchBaseQuery} from "@reduxjs/toolkit/query/react";
|
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||||
|
|
||||||
|
|
||||||
export const lensTypesApi = createApi({
|
export const lensTypesApi = createApi({
|
||||||
reducerPath: 'lensTypesApi',
|
reducerPath: 'lensTypesApi',
|
||||||
baseQuery: fetchBaseQuery({
|
baseQuery: baseQueryWithAuth,
|
||||||
baseUrl: CONFIG.BASE_URL,
|
|
||||||
prepareHeaders: (headers) => {
|
|
||||||
const token = localStorage.getItem('access_token');
|
|
||||||
if (token) headers.set('Authorization', `Bearer ${token}`);
|
|
||||||
return headers;
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
endpoints: (builder) => ({
|
endpoints: (builder) => ({
|
||||||
getLensTypes: builder.query({
|
getLensTypes: builder.query({
|
||||||
query: () => `/lens_types/`,
|
query: () => `/lens_types/`,
|
||||||
|
|||||||
@ -1,17 +1,10 @@
|
|||||||
import {createApi, fetchBaseQuery} from "@reduxjs/toolkit/query/react";
|
import {createApi} from "@reduxjs/toolkit/query/react";
|
||||||
import CONFIG from "../Core/сonfig.js";
|
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||||
|
|
||||||
|
|
||||||
export const lensesApi = createApi({
|
export const lensesApi = createApi({
|
||||||
reducerPath: 'lensesApi',
|
reducerPath: 'lensesApi',
|
||||||
baseQuery: fetchBaseQuery({
|
baseQuery: baseQueryWithAuth,
|
||||||
baseUrl: CONFIG.BASE_URL,
|
|
||||||
prepareHeaders: (headers) => {
|
|
||||||
const token = localStorage.getItem('access_token');
|
|
||||||
if (token) headers.set('Authorization', `Bearer ${token}`);
|
|
||||||
return headers;
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
tagTypes: ['Lens'],
|
tagTypes: ['Lens'],
|
||||||
endpoints: (builder) => ({
|
endpoints: (builder) => ({
|
||||||
getLenses: builder.query({
|
getLenses: builder.query({
|
||||||
@ -33,7 +26,7 @@ export const lensesApi = createApi({
|
|||||||
invalidatesTags: ['Lens']
|
invalidatesTags: ['Lens']
|
||||||
}),
|
}),
|
||||||
updateLens: builder.mutation({
|
updateLens: builder.mutation({
|
||||||
query: ({ id, ...lens }) => ({
|
query: ({id, ...lens}) => ({
|
||||||
url: `/lenses/${id}/`,
|
url: `/lenses/${id}/`,
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
body: lens
|
body: lens
|
||||||
|
|||||||
@ -1,16 +1,9 @@
|
|||||||
import {createApi, fetchBaseQuery} from '@reduxjs/toolkit/query/react'
|
import {createApi} from '@reduxjs/toolkit/query/react'
|
||||||
import CONFIG from "../Core/сonfig.js";
|
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||||
|
|
||||||
export const patientsApi = createApi({
|
export const patientsApi = createApi({
|
||||||
reducerPath: 'patientsApi',
|
reducerPath: 'patientsApi',
|
||||||
baseQuery: fetchBaseQuery({
|
baseQuery: baseQueryWithAuth,
|
||||||
baseUrl: CONFIG.BASE_URL,
|
|
||||||
prepareHeaders: (headers) => {
|
|
||||||
const token = localStorage.getItem('access_token');
|
|
||||||
if (token) headers.set('Authorization', `Bearer ${token}`);
|
|
||||||
return headers;
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
tagTypes: ['Patient'],
|
tagTypes: ['Patient'],
|
||||||
endpoints: (builder) => ({
|
endpoints: (builder) => ({
|
||||||
getPatients: builder.query({
|
getPatients: builder.query({
|
||||||
|
|||||||
@ -1,16 +1,9 @@
|
|||||||
import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";
|
import {createApi} from "@reduxjs/toolkit/query/react";
|
||||||
import CONFIG from "../Core/сonfig.js";
|
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||||
|
|
||||||
export const scheduledAppointmentsApi = createApi({
|
export const scheduledAppointmentsApi = createApi({
|
||||||
reducerPath: 'scheduledAppointmentsApi',
|
reducerPath: 'scheduledAppointmentsApi',
|
||||||
baseQuery: fetchBaseQuery({
|
baseQuery: baseQueryWithAuth,
|
||||||
baseUrl: CONFIG.BASE_URL,
|
|
||||||
prepareHeaders: (headers) => {
|
|
||||||
const token = localStorage.getItem('access_token');
|
|
||||||
if (token) headers.set('Authorization', `Bearer ${token}`);
|
|
||||||
return headers;
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
tagTypes: ['ScheduledAppointment'],
|
tagTypes: ['ScheduledAppointment'],
|
||||||
endpoints: (builder) => ({
|
endpoints: (builder) => ({
|
||||||
getScheduledAppointments: builder.query({
|
getScheduledAppointments: builder.query({
|
||||||
@ -26,7 +19,7 @@ export const scheduledAppointmentsApi = createApi({
|
|||||||
invalidatesTags: ['ScheduledAppointment'],
|
invalidatesTags: ['ScheduledAppointment'],
|
||||||
}),
|
}),
|
||||||
updateScheduledAppointment: builder.mutation({
|
updateScheduledAppointment: builder.mutation({
|
||||||
query: ({ id, data }) => ({
|
query: ({id, data}) => ({
|
||||||
url: `/scheduled_appointments/${id}/`,
|
url: `/scheduled_appointments/${id}/`,
|
||||||
method: 'PUT',
|
method: 'PUT',
|
||||||
body: data,
|
body: data,
|
||||||
|
|||||||
@ -1,16 +1,9 @@
|
|||||||
import {createApi, fetchBaseQuery} from '@reduxjs/toolkit/query/react';
|
import {createApi} from '@reduxjs/toolkit/query/react';
|
||||||
import CONFIG from "../Core/сonfig.js";
|
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||||
|
|
||||||
export const setContentApi = createApi({
|
export const setContentApi = createApi({
|
||||||
reducerPath: 'setContentApi',
|
reducerPath: 'setContentApi',
|
||||||
baseQuery: fetchBaseQuery({
|
baseQuery: baseQueryWithAuth,
|
||||||
baseUrl: CONFIG.BASE_URL,
|
|
||||||
prepareHeaders: (headers) => {
|
|
||||||
const token = localStorage.getItem('access_token');
|
|
||||||
if (token) headers.set('Authorization', `Bearer ${token}`);
|
|
||||||
return headers;
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
tagTypes: ['SetContent'],
|
tagTypes: ['SetContent'],
|
||||||
endpoints: (builder) => ({
|
endpoints: (builder) => ({
|
||||||
getSetContent: builder.query({
|
getSetContent: builder.query({
|
||||||
|
|||||||
@ -1,16 +1,9 @@
|
|||||||
import {createApi, fetchBaseQuery} from '@reduxjs/toolkit/query/react'
|
import {createApi} from '@reduxjs/toolkit/query/react'
|
||||||
import CONFIG from "../Core/сonfig.js";
|
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||||
|
|
||||||
export const setsApi = createApi({
|
export const setsApi = createApi({
|
||||||
reducerPath: 'setsApi',
|
reducerPath: 'setsApi',
|
||||||
baseQuery: fetchBaseQuery({
|
baseQuery: baseQueryWithAuth,
|
||||||
baseUrl: CONFIG.BASE_URL,
|
|
||||||
prepareHeaders: (headers) => {
|
|
||||||
const token = localStorage.getItem('access_token');
|
|
||||||
if (token) headers.set('Authorization', `Bearer ${token}`);
|
|
||||||
return headers;
|
|
||||||
}
|
|
||||||
}),
|
|
||||||
tagTypes: ['Set'],
|
tagTypes: ['Set'],
|
||||||
endpoints: (builder) => ({
|
endpoints: (builder) => ({
|
||||||
getSets: builder.query({
|
getSets: builder.query({
|
||||||
|
|||||||
@ -1,17 +1,10 @@
|
|||||||
import {createApi, fetchBaseQuery} from "@reduxjs/toolkit/query/react";
|
import {createApi} from "@reduxjs/toolkit/query/react";
|
||||||
import CONFIG from "../Core/сonfig.js";
|
import {baseQueryWithAuth} from "./baseQuery.js";
|
||||||
|
|
||||||
|
|
||||||
export const usersApi = createApi({
|
export const usersApi = createApi({
|
||||||
reducerPath: 'usersApi',
|
reducerPath: 'usersApi',
|
||||||
baseQuery: fetchBaseQuery({
|
baseQuery: baseQueryWithAuth,
|
||||||
baseUrl: CONFIG.BASE_URL,
|
|
||||||
prepareHeaders: (headers) => {
|
|
||||||
const token = localStorage.getItem('access_token');
|
|
||||||
if (token) headers.set('Authorization', `Bearer ${token}`);
|
|
||||||
return headers;
|
|
||||||
},
|
|
||||||
}),
|
|
||||||
tagTypes: ['User'],
|
tagTypes: ['User'],
|
||||||
endpoints: (builder) => ({
|
endpoints: (builder) => ({
|
||||||
getAuthenticatedUserData: builder.query({
|
getAuthenticatedUserData: builder.query({
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user