diff options
author | Michael Hunteman <michael@huntm.net> | 2024-07-09 18:24:15 -0700 |
---|---|---|
committer | Michael Hunteman <michael@huntm.net> | 2024-07-09 18:24:44 -0700 |
commit | a6b1fa31e890b51b043c4211b14cd4be29ed6fba (patch) | |
tree | 66a1fbc182fc3f9ed746958e2854d8a337782983 /client/src/slices | |
parent | 03ca04fdb738cc151d775b76e1bc38aec792521a (diff) |
Add success and error alerts
Diffstat (limited to 'client/src/slices')
-rw-r--r-- | client/src/slices/apiSlice.ts | 67 | ||||
-rw-r--r-- | client/src/slices/authSlice.ts | 31 |
2 files changed, 98 insertions, 0 deletions
diff --git a/client/src/slices/apiSlice.ts b/client/src/slices/apiSlice.ts new file mode 100644 index 0000000..90cdc48 --- /dev/null +++ b/client/src/slices/apiSlice.ts @@ -0,0 +1,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: ['Guest'], + endpoints: (builder) => ({ + getGuests: builder.query<void, void>({ + query: () => 'guest', + providesTags: ['Guest'], + }), + updateGuest: builder.mutation<Guest, Guest>({ + query: (guest) => ({ + url: `guest/${guest?.id}`, + method: 'PUT', + body: guest, + providesTags: ['Guest'], + }), + }), + login: builder.mutation<LoginResponse, LoginRequest>({ + query: (credentials) => ({ + url: 'guest/login', + method: 'POST', + body: credentials, + }), + }), + }), +}); + +export const { useGetGuestsQuery, useUpdateGuestMutation, useLoginMutation } = + apiSlice; diff --git a/client/src/slices/authSlice.ts b/client/src/slices/authSlice.ts new file mode 100644 index 0000000..e1fec78 --- /dev/null +++ b/client/src/slices/authSlice.ts @@ -0,0 +1,31 @@ +import { createSlice } from '@reduxjs/toolkit'; +import type { PayloadAction } from '@reduxjs/toolkit'; +import type { RootState } from '../store'; +import type { Guest } from './apiSlice'; + +type AuthState = { + guest?: Guest; + token?: string; +}; + +const authSlice = createSlice({ + name: 'auth', + initialState: { guest: undefined, token: undefined } as AuthState, + reducers: { + setCredentials: ( + state, + { + payload: { guest, token }, + }: PayloadAction<{ guest: Guest; token: string }> + ) => { + state.guest = guest; + state.token = token; + }, + }, +}); + +export const { setCredentials } = authSlice.actions; + +export default authSlice.reducer; + +export const selectCurrentGuest = (state: RootState) => state.auth.guest; |