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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import { setCredentials, logOut } from './features/auth/authSlice';
export interface User {
firstName: string
lastName: string
}
export interface UserResponse {
user: User
token: string
}
export interface LoginRequest {
firstName: string
lastName: string
}
export const apiSlice = createApi({
reducerPath: 'api',
baseQuery: fetchBaseQuery({
// baseUrl: 'http://localhost:3000',
baseUrl: '/',
prepareHeaders: (headers, { getState }) => {
const token = (getState() as RootState).auth.token;
if (token) {
headers.set('authorization', `Bearer ${token}`);
}
return headers;
}
}),
tagTypes: ['Guests'],
endpoints: builder => ({
getGuests: builder.query({
query: () => '/guests',
providesTags: ['Guests']
}),
updateGuest: builder.mutation({
query: guest => ({
url: `/guests/${guest.id}`,
method: 'PATCH',
body: guest,
providesTags: ['Guests']
})
}),
login: builder.mutation<UserResponse, LoginRequest>({
query: credentials => ({
url: '/guest-login',
method: 'POST',
body: credentials
})
})
})
});
export const {
useGetGuestsQuery,
useUpdateGuestMutation,
useLoginMutation
} = apiSlice;
|