Skip to content

Commit

Permalink
refactor: rename context and provider for a more meaningful name
Browse files Browse the repository at this point in the history
  • Loading branch information
giovanizanetti committed Dec 30, 2020
1 parent 75270b6 commit c52a96b
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 16 deletions.
6 changes: 3 additions & 3 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { useState, useEffect } from 'react'
import './App.css'
import Navigation from './components/Navigation/Navigation'
import Main from './components/Main/Main'
import { BooksProvider } from './BooksProvider'
import { StoreProvider } from './Store'
import { getAll, update } from './BooksAPI'

const BooksApp = () => {
Expand Down Expand Up @@ -53,7 +53,7 @@ const BooksApp = () => {
<Navigation />

{/* Pass books down to make it available for any consumer component within the provider */}
<BooksProvider
<StoreProvider
value={{
books,
handleShelf,
Expand All @@ -65,7 +65,7 @@ const BooksApp = () => {
}}
>
<Main />
</BooksProvider>
</StoreProvider>
</div>
)
}
Expand Down
7 changes: 0 additions & 7 deletions src/BooksProvider.js

This file was deleted.

7 changes: 7 additions & 0 deletions src/Store.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { createContext } from 'react'

export const StoreContext = createContext([])

export const StoreProvider = StoreContext.Provider

export default StoreContext
4 changes: 2 additions & 2 deletions src/components/Book/Book.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { useContext } from 'react'
import BooksProvider from '../../BooksProvider'
import StoreContext from '../../Store'
import BookShelfChanger from '../BookShelfChanger/BookShelfChanger'
import placeholder from '../../assets/book-placeholder.jpeg'

const Book = ({ book, search }) => {
const { title, authors, shelf, id, imageLinks } = book

const { books } = useContext(BooksProvider)
const { books } = useContext(StoreContext)

// if book list coming from search results, check if book is already is in any shelf mybooks
const isBook = () => {
Expand Down
15 changes: 15 additions & 0 deletions src/components/BookList/books.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import React from 'react'
import ReactDOM from 'react-dom'
import Book from '../Book/Book'
import BooksProvider from '../../BooksProvider'

/**
This course is not designed to teach Test Driven Development.
Feel free to use this file to test your application, but it
is not required.
**/

it('renders Book without crashing', () => {
const div = document.createElement('div')
ReactDOM.render(<Book />, div)
})
4 changes: 2 additions & 2 deletions src/components/BookShelfChanger/BookShelfChanger.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import { useContext, useState } from 'react'
import BooksContext from '../../BooksProvider'
import StoreContext from '../../Store'

const BookShelfChanger = ({ book }) => {
const bookShelf = book.shelf ? book.shelf : 'none'
const { handleShelf } = useContext(BooksContext)
const { handleShelf } = useContext(StoreContext)
const [value, setValue] = useState(bookShelf)

const handleChange = (e) => {
Expand Down
4 changes: 2 additions & 2 deletions src/components/Main/Main.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { useContext } from 'react'
import BookShelf from '../BookShelf/BookShelf'
import BooksContext from '../../BooksProvider'
import StoreContext from '../../Store'
import { BrowserRouter as Router, Switch, Route, Link } from 'react-router-dom'
import Search from '../Search/Search'

const Main = () => {
const { currentlyReading, wantToRead, read } = useContext(BooksContext)
const { currentlyReading, wantToRead, read } = useContext(StoreContext)

return (
<Router>
Expand Down

0 comments on commit c52a96b

Please sign in to comment.