1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
|
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import type { RootState } from '../../store';
import type { Guest, Name, GuestLogin } from '../../models';
export const guestSlice = createApi({
reducerPath: 'guestApi',
baseQuery: fetchBaseQuery({
baseUrl: `${import.meta.env.VITE_BASE_URL}guests/`,
prepareHeaders: (headers, { getState }) => {
const token = (getState() as RootState).guest.token;
if (token) {
headers.set('authorization', `${token}`);
}
return headers;
},
}),
endpoints: (builder) => ({
loginGuest: builder.mutation<GuestLogin, Name>({
query: (name) => ({
url: 'login',
method: 'POST',
body: name,
}),
}),
updateGuest: builder.mutation<Guest, Guest>({
query: (guest) => ({
url: `${guest?.id}`,
method: 'PUT',
body: guest,
}),
}),
}),
});
export const { useLoginGuestMutation, useUpdateGuestMutation } = guestSlice;
|