Skip to content

Commit

Permalink
Update: Redux store to include the Book object in the initialState an…
Browse files Browse the repository at this point in the history
…d its reducer and actions
  • Loading branch information
ClaytonSiby committed Oct 12, 2023
1 parent 8d673c3 commit 8855ded
Showing 1 changed file with 21 additions and 0 deletions.
21 changes: 21 additions & 0 deletions src/features/book/bookSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 () => {
Expand Down Expand Up @@ -78,6 +79,15 @@ const initialState: initialState = {
booksData: [],
isLoading: 'idle',
error: '',
book: {
id: '',
title: '',
author: '',
categories: [],
description: '',
created_at: '',
updated_at: '',
},
}

const bookSlice = createSlice({
Expand All @@ -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'
})
},
})

Expand Down

0 comments on commit 8855ded

Please sign in to comment.