diff options
Diffstat (limited to 'client/src/slices')
-rw-r--r-- | client/src/slices/api/adminSlice.ts | 53 | ||||
-rw-r--r-- | client/src/slices/api/guestSlice.ts (renamed from client/src/slices/apiSlice.ts) | 37 | ||||
-rw-r--r-- | client/src/slices/auth/adminSlice.ts | 29 | ||||
-rw-r--r-- | client/src/slices/auth/guestSlice.ts | 29 | ||||
-rw-r--r-- | client/src/slices/authSlice.ts | 31 |
5 files changed, 126 insertions, 53 deletions
diff --git a/client/src/slices/api/adminSlice.ts b/client/src/slices/api/adminSlice.ts new file mode 100644 index 0000000..cd1638d --- /dev/null +++ b/client/src/slices/api/adminSlice.ts @@ -0,0 +1,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; diff --git a/client/src/slices/apiSlice.ts b/client/src/slices/api/guestSlice.ts index 90cdc48..38deb9a 100644 --- a/client/src/slices/apiSlice.ts +++ b/client/src/slices/api/guestSlice.ts @@ -1,12 +1,12 @@ import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'; -import type { RootState } from '../store'; +import type { RootState } from '../../store'; -export interface LoginRequest { +export interface GuestLoginRequest { firstName: string; lastName: string; } -export interface LoginResponse { +export interface GuestLoginResponse { guest: Guest; token: string; } @@ -27,41 +27,34 @@ export interface PartyGuest { lastName: string; } -export const apiSlice = createApi({ - reducerPath: 'api', +export const guestSlice = createApi({ + reducerPath: 'guestApi', baseQuery: fetchBaseQuery({ - baseUrl: 'http://192.168.1.41:8080/', + baseUrl: import.meta.env.VITE_BASE_URL + 'guest/', prepareHeaders: (headers, { getState }) => { - const token = (getState() as RootState).auth.token; + const token = (getState() as RootState).guest.token; if (token) { headers.set('authorization', `${token}`); } return headers; }, }), - tagTypes: ['Guest'], endpoints: (builder) => ({ - getGuests: builder.query<void, void>({ - query: () => 'guest', - providesTags: ['Guest'], + loginGuest: builder.mutation<GuestLoginResponse, GuestLoginRequest>({ + query: (credentials) => ({ + url: 'login', + method: 'POST', + body: credentials, + }), }), updateGuest: builder.mutation<Guest, Guest>({ query: (guest) => ({ - url: `guest/${guest?.id}`, + url: `${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; +export const { useLoginGuestMutation, useUpdateGuestMutation } = guestSlice; diff --git a/client/src/slices/auth/adminSlice.ts b/client/src/slices/auth/adminSlice.ts new file mode 100644 index 0000000..8753b55 --- /dev/null +++ b/client/src/slices/auth/adminSlice.ts @@ -0,0 +1,29 @@ +import { createSlice } from '@reduxjs/toolkit'; +import type { PayloadAction } from '@reduxjs/toolkit'; +import type { RootState } from '../../store'; +import type { Guest } from '../api/guestSlice'; + +type AdminAuth = { + guests?: Guest[]; + token?: string; +}; + +const adminSlice = createSlice({ + name: 'admin', + initialState: { guest: undefined, token: undefined } as AdminAuth, + reducers: { + setAdmin: ( + state, + { payload: { guests, token } }: PayloadAction<AdminAuth> + ) => { + state.guests = guests; + state.token = token; + }, + }, +}); + +export const { setAdmin } = adminSlice.actions; + +export default adminSlice.reducer; + +export const selectGuests = (state: RootState) => state.admin.guests; diff --git a/client/src/slices/auth/guestSlice.ts b/client/src/slices/auth/guestSlice.ts new file mode 100644 index 0000000..701148e --- /dev/null +++ b/client/src/slices/auth/guestSlice.ts @@ -0,0 +1,29 @@ +import { createSlice } from '@reduxjs/toolkit'; +import type { PayloadAction } from '@reduxjs/toolkit'; +import type { RootState } from '../../store'; +import type { Guest } from '../api/guestSlice'; + +type GuestAuth = { + guest?: Guest; + token?: string; +}; + +const guestSlice = createSlice({ + name: 'guest', + initialState: { guest: undefined, token: undefined } as GuestAuth, + reducers: { + setGuest: ( + state, + { payload: { guest, token } }: PayloadAction<GuestAuth> + ) => { + state.guest = guest; + state.token = token; + }, + }, +}); + +export const { setGuest } = guestSlice.actions; + +export default guestSlice.reducer; + +export const selectGuest = (state: RootState) => state.guest.guest; diff --git a/client/src/slices/authSlice.ts b/client/src/slices/authSlice.ts deleted file mode 100644 index e1fec78..0000000 --- a/client/src/slices/authSlice.ts +++ /dev/null @@ -1,31 +0,0 @@ -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; |