summaryrefslogtreecommitdiff
path: root/client/src/apiSlice.ts
blob: 514f8b18dbf370b9fbe883fa66d09d7ab3b4ffbf (plain)
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
61
62
63
64
65
66
67
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import type { RootState } from './store';

export interface LoginRequest {
  firstName: string;
  lastName: string;
}

export interface LoginResponse {
  guest: Guest;
  token: string;
}

export interface Guest {
  id: number;
  firstName: string;
  lastName: string;
  attendance: string;
  email: string;
  message: string;
  partySize: number;
  partyList: Array<PartyGuest>;
}

export interface PartyGuest {
  firstName: string;
  lastName: string;
}

export const apiSlice = createApi({
  reducerPath: 'api',
  baseQuery: fetchBaseQuery({
    baseUrl: 'http://192.168.1.41:8080/',
    prepareHeaders: (headers, { getState }) => {
      const token = (getState() as RootState).auth.token;
      if (token) {
        headers.set('authorization', `${token}`);
      }
      return headers;
    },
  }),
  tagTypes: ['Guests'],
  endpoints: (builder) => ({
    getGuests: builder.query<void, void>({
      query: () => 'guests',
      providesTags: ['Guests'],
    }),
    updateGuest: builder.mutation<Guest, Guest>({
      query: (guest) => ({
        url: `guests/${guest?.id}`,
        method: 'PUT',
        body: guest,
        providesTags: ['Guests'],
      }),
    }),
    login: builder.mutation<LoginResponse, LoginRequest>({
      query: (credentials) => ({
        url: 'guests/login',
        method: 'POST',
        body: credentials,
      }),
    }),
  }),
});

export const { useGetGuestsQuery, useUpdateGuestMutation, useLoginMutation } =
  apiSlice;