-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathApp.js
More file actions
102 lines (97 loc) · 2.83 KB
/
App.js
File metadata and controls
102 lines (97 loc) · 2.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
import React, { useReducer } from 'react'
import Quotes from './Quotes'
import QuoteForm from './QuoteForm'
const CREATE_QUOTE = 'CREATE_QUOTE'
const DELETE_QUOTE = 'DELETE_QUOTE'
const EDIT_QUOTE_AUTHENTICITY = 'EDIT_QUOTE_AUTHENTICITY'
const SET_HIGHLIGHTED_QUOTE = 'SET_HIGHLIGHTED_QUOTE'
const TOGGLE_VISIBILITY = 'TOGGLE_VISIBILITY'
let id = 1
const getNextId = () => id++
const initialState = {
displayAllQuotes: true,
highlightedQuote: null,
quotes: [
{
id: getNextId(),
quoteText: "Don't cry because it's over, smile because it happened.",
authorName: "Dr. Seuss",
apocryphal: false,
},
{
id: getNextId(),
quoteText: "So many books, so little time.",
authorName: "Frank Zappa",
apocryphal: false,
},
{
id: getNextId(),
quoteText: "Be yourself; everyone else is already taken.",
authorName: "Oscar Wilde",
apocryphal: false,
},
],
}
const reducer = (state, action) => {
switch (action.type) {
case CREATE_QUOTE:
return { ...state, quotes: [...state.quotes, action.payload] }
case DELETE_QUOTE:
return { ...state, quotes: state.quotes.filter(qt => qt.id !== action.payload) }
case EDIT_QUOTE_AUTHENTICITY:
return {
...state,
quotes: state.quotes.map(qt => {
if (qt.id != action.payload) return qt
return { ...qt, apocryphal: !qt.apocryphal }
})
}
case SET_HIGHLIGHTED_QUOTE:
return {
...state,
highlightedQuote: state.highlightedQuote !== action.payload
? action.payload
: null
}
case TOGGLE_VISIBILITY:
return { ...state, displayAllQuotes: !state.displayAllQuotes }
default:
return state
}
}
export default function App() {
const [state, dispatch] = useReducer(reducer, initialState)
const createQuote = ({ authorName, quoteText }) => {
const newQuote = { id: getNextId(), authorName, quoteText, apocryphal: false }
dispatch({ type: CREATE_QUOTE, payload: newQuote })
}
const deleteQuote = id => {
dispatch({ type: DELETE_QUOTE, payload: id })
}
const editQuoteAuthenticity = id => {
dispatch({ type: EDIT_QUOTE_AUTHENTICITY, payload: id })
}
const setHighlightedQuote = id => {
dispatch({ type: SET_HIGHLIGHTED_QUOTE, payload: id })
}
const toggleVisibility = () => {
dispatch({ type: TOGGLE_VISIBILITY })
}
return (
<div id="mp">
<h2>Module Project</h2>
<Quotes
deleteQuote={deleteQuote}
editQuoteAuthenticity={editQuoteAuthenticity}
setHighlightedQuote={setHighlightedQuote}
toggleVisibility={toggleVisibility}
displayAllQuotes={state.displayAllQuotes}
highlightedQuote={state.highlightedQuote}
quotes={state.quotes}
/>
<QuoteForm
createQuote={createQuote}
/>
</div>
)
}