-
Notifications
You must be signed in to change notification settings - Fork 3
Add Redux #18
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add Redux #18
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
const localStorageMock = { | ||
store: {}, | ||
getItem(key) { | ||
return this.store[key]; | ||
}, | ||
setItem(key, value) { | ||
this.store[key] = value.toString(); | ||
}, | ||
clear() { | ||
this.store = {}; | ||
}, | ||
removeItem(key) { | ||
delete this.store[key]; | ||
}, | ||
}; | ||
|
||
Object.defineProperty(global, 'localStorage', { value: localStorageMock }); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,6 +9,8 @@ import ListItem from "./ListItem"; | |
import Popup from "./Popup"; | ||
import { getCsrfToken } from "./utils"; | ||
import * as fd from "./fetchData"; | ||
import { useDispatch } from "react-redux"; | ||
import { librarySlice } from "./reducers/librarySlice"; | ||
|
||
async function deleteBook(bookid: string, onDelete) { | ||
const res = await fd.deleteBook(bookid); | ||
|
@@ -36,7 +38,7 @@ async function newBook(dispatch) { | |
} else { | ||
const book = res.payload; | ||
console.log("new book", book); | ||
dispatch({ type: "ADD_BOOK", payload: book }); | ||
dispatch(librarySlice.actions.addBook(book)); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you look at the quick start we can make this even easier: by doing something like: The problem is we have A LOT of actions. I think this is something we can reevaluate and clean up later |
||
} | ||
} | ||
|
||
|
@@ -49,17 +51,16 @@ export default function BookList({ | |
onChange, | ||
onDelete, | ||
saveBook, | ||
dispatch, | ||
canCloseSidebar = true, | ||
}: { | ||
books: t.Book[]; | ||
selectedBookId: string; | ||
onChange: () => void; | ||
onDelete: (bookid: string) => void; | ||
saveBook: (book: t.Book) => void; | ||
dispatch: React.Dispatch<any>; | ||
canCloseSidebar?: boolean; | ||
}) { | ||
const dispatch = useDispatch(); | ||
const [showPopup, setShowPopup] = React.useState(false); | ||
const [currentBook, setCurrentBook] = React.useState(books[0]); | ||
|
||
|
@@ -92,7 +93,7 @@ export default function BookList({ | |
const rightMenuItem = canCloseSidebar && { | ||
label: "Close", | ||
icon: <XMarkIcon className="w-4 h-4 xl:w-5 xl:h-5" />, | ||
onClick: () => dispatch({ type: "CLOSE_BOOK_LIST" }), | ||
onClick: () => dispatch(librarySlice.actions.closeBookList()), | ||
className: buttonStyles, | ||
}; | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since the initial state calls local storage, and the utils file needs a dispatcher, this sadly forces Jest to call local storage which does not exist. This mock is put in place to fix it.