From 8855deda7ffa6c560d2591fea1d927c906c62801 Mon Sep 17 00:00:00 2001 From: Clayton Siby Date: Thu, 12 Oct 2023 18:41:59 +0200 Subject: [PATCH] Update: Redux store to include the Book object in the initialState and its reducer and actions --- src/features/book/bookSlice.ts | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/src/features/book/bookSlice.ts b/src/features/book/bookSlice.ts index 330b8cf..06c5e70 100644 --- a/src/features/book/bookSlice.ts +++ b/src/features/book/bookSlice.ts @@ -16,6 +16,7 @@ type initialState = { booksData: Book[] isLoading: 'idle' | 'loading' | 'succeeded' | 'failed' error: string | null + book: Book } export const fetchBooks = createAsyncThunk('books/fetchBooks', async () => { @@ -78,6 +79,15 @@ const initialState: initialState = { booksData: [], isLoading: 'idle', error: '', + book: { + id: '', + title: '', + author: '', + categories: [], + description: '', + created_at: '', + updated_at: '', + }, } const bookSlice = createSlice({ @@ -97,6 +107,17 @@ const bookSlice = createSlice({ state.isLoading = 'failed' state.error = action.error.message || 'Unknown error' }) + .addCase(fetchBook.pending, (state) => { + state.isLoading = 'loading' + }) + .addCase(fetchBook.fulfilled, (state, action) => { + state.isLoading = 'succeeded' + state.book = action.payload.book + }) + .addCase(fetchBook.rejected, (state, action) => { + state.isLoading = 'failed' + state.error = action.error.message || 'Unknown error' + }) }, })