From 096a08708e2310becba56a237ef63b5cf6e3c4c4 Mon Sep 17 00:00:00 2001 From: Michael Hunteman Date: Sun, 25 Aug 2024 12:44:32 -0700 Subject: Add admin dashboard --- client/src/slices/api/adminSlice.ts | 53 ++++++++++++++++++++++++++++ client/src/slices/api/guestSlice.ts | 60 ++++++++++++++++++++++++++++++++ client/src/slices/apiSlice.ts | 67 ------------------------------------ client/src/slices/auth/adminSlice.ts | 29 ++++++++++++++++ client/src/slices/auth/guestSlice.ts | 29 ++++++++++++++++ client/src/slices/authSlice.ts | 31 ----------------- 6 files changed, 171 insertions(+), 98 deletions(-) create mode 100644 client/src/slices/api/adminSlice.ts create mode 100644 client/src/slices/api/guestSlice.ts delete mode 100644 client/src/slices/apiSlice.ts create mode 100644 client/src/slices/auth/adminSlice.ts create mode 100644 client/src/slices/auth/guestSlice.ts delete mode 100644 client/src/slices/authSlice.ts (limited to 'client/src/slices') 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; +} + +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({ + query: (credentials) => ({ + url: 'admin/login', + method: 'POST', + body: credentials, + }), + }), + }), +}); + +export const { useLoginAdminMutation } = adminSlice; diff --git a/client/src/slices/api/guestSlice.ts b/client/src/slices/api/guestSlice.ts new file mode 100644 index 0000000..38deb9a --- /dev/null +++ b/client/src/slices/api/guestSlice.ts @@ -0,0 +1,60 @@ +import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react'; +import type { RootState } from '../../store'; + +export interface GuestLoginRequest { + firstName: string; + lastName: string; +} + +export interface GuestLoginResponse { + guest: Guest; + token: string; +} + +export interface Guest { + id: number; + firstName: string; + lastName: string; + attendance: string; + email: string; + message: string; + partySize: number; + partyList: Array; +} + +export interface PartyGuest { + firstName: string; + lastName: string; +} + +export const guestSlice = createApi({ + reducerPath: 'guestApi', + baseQuery: fetchBaseQuery({ + baseUrl: import.meta.env.VITE_BASE_URL + 'guest/', + prepareHeaders: (headers, { getState }) => { + const token = (getState() as RootState).guest.token; + if (token) { + headers.set('authorization', `${token}`); + } + return headers; + }, + }), + endpoints: (builder) => ({ + loginGuest: builder.mutation({ + query: (credentials) => ({ + url: 'login', + method: 'POST', + body: credentials, + }), + }), + updateGuest: builder.mutation({ + query: (guest) => ({ + url: `${guest?.id}`, + method: 'PUT', + body: guest, + }), + }), + }), +}); + +export const { useLoginGuestMutation, useUpdateGuestMutation } = guestSlice; diff --git a/client/src/slices/apiSlice.ts b/client/src/slices/apiSlice.ts deleted file mode 100644 index 90cdc48..0000000 --- a/client/src/slices/apiSlice.ts +++ /dev/null @@ -1,67 +0,0 @@ -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; -} - -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({ - query: () => 'guest', - providesTags: ['Guest'], - }), - updateGuest: builder.mutation({ - query: (guest) => ({ - url: `guest/${guest?.id}`, - method: 'PUT', - body: guest, - providesTags: ['Guest'], - }), - }), - login: builder.mutation({ - query: (credentials) => ({ - url: 'guest/login', - method: 'POST', - body: credentials, - }), - }), - }), -}); - -export const { useGetGuestsQuery, useUpdateGuestMutation, useLoginMutation } = - apiSlice; 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 + ) => { + 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 + ) => { + 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; -- cgit v1.2.3