summaryrefslogtreecommitdiff
path: root/client/src/slices/api/adminSlice.ts
blob: cd1638daa5d41b4c0c0dfb1d4b468d1c79bf04d3 (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
import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import type { RootState } from '../../store';

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 interface AdminLoginRequest {
  username: string;
  password: string;
}

export interface AdminLoginResponse {
  guests: Guest[];
  token: string;
}

export const adminSlice = createApi({
  reducerPath: 'adminApi',
  baseQuery: fetchBaseQuery({
    baseUrl: import.meta.env.VITE_BASE_URL,
    prepareHeaders: (headers, { getState }) => {
      const token = (getState() as RootState).admin.token;
      if (token) {
        headers.set('authorization', `${token}`);
      }
      return headers;
    },
  }),
  endpoints: (builder) => ({
    loginAdmin: builder.mutation<AdminLoginResponse, AdminLoginRequest>({
      query: (credentials) => ({
        url: 'admin/login',
        method: 'POST',
        body: credentials,
      }),
    }),
  }),
});

export const { useLoginAdminMutation } = adminSlice;