Skip to content

Commit

Permalink
Update: deleteBook action and dispatch it on click
Browse files Browse the repository at this point in the history
  • Loading branch information
ClaytonSiby committed Oct 11, 2023
1 parent c18b9d3 commit 3c418fb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 8 deletions.
18 changes: 16 additions & 2 deletions src/components/BookList/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,19 @@ import { useAppDispatch, useAppSelector } from '../../app/hooks'
import { fetchBooks } from '../../features/book/bookSlice'
import LoadingSpinner from '../LoadingSpinner'
import BookModal from '../BookModal'
import Book from '../Book'

const BookList: React.FC = () => {
const dispatch = useAppDispatch()
const books = useAppSelector((state) => state.books.booksData)
const isLoading = useAppSelector((state) => state.books.isLoading)

useEffect(() => {
dispatch(fetchBooks() as any)
const fetchBooksData = async () => {
await dispatch(fetchBooks())
}

fetchBooksData()
}, [dispatch])

return (
Expand All @@ -21,7 +26,16 @@ const BookList: React.FC = () => {
<div>
<BookModal />
<h2>Book List</h2>
{books?.map((book) => <div key={book.id}>{book.title}</div>)}
{books?.map((book) => (
<Book
key={book.id}
bookAuthor={book.author}
bookTitle={book.title}
bookCategory={book.category}
bookUpdatedAt={book.updatedAt}
bookId={book.id}
/>
))}
</div>
)}
</div>
Expand Down
20 changes: 14 additions & 6 deletions src/features/book/bookSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,12 +40,20 @@ export const addBook = createAsyncThunk('books/addBook', async (book) => {
return response.data
})

export const deleteBook = createAsyncThunk('books/deleteBook', async (id) => {
const response = await axios.delete(
`https://helm-bookstore-api.onrender.com/api/books/${id}`
)
return response.data
})
export const deleteBook = createAsyncThunk(
'books/deleteBook',
async (id: string) => {
try {
await axios.delete(
`https://helm-bookstore-api.onrender.com/api/books/${id}/`
)

alert('Book deleted successfully')
} catch (error) {
alert(error)
}
}
)

export const updateBook = createAsyncThunk(
'books/updateBook',
Expand Down

0 comments on commit 3c418fb

Please sign in to comment.