-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
127 lines (125 loc) · 3.9 KB
/
App.js
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
import React, { Component } from "react";
import { Route, Switch } from "react-router-dom";
import { TransitionGroup, CSSTransition } from "react-transition-group";
import Palette from "./Palette";
import PaletteList from "./PaletteList";
import SingleColorPalette from "./SingleColorPalette";
import Page from "./Page";
import NewPaletteForm from "./NewPaletteForm";
import seedColors from "./seedColors";
import { generatePalette } from "./colorHelpers";
class App extends Component {
constructor(props) {
super(props);
const savedPalettes = JSON.parse(window.localStorage.getItem("palettes"));
this.state = { palettes: savedPalettes || seedColors };
this.savePalette = this.savePalette.bind(this);
this.findPalette = this.findPalette.bind(this);
this.deletePalette = this.deletePalette.bind(this);
}
findPalette(id) {
return this.state.palettes.find(function (palette) {
return palette.id === id;
});
}
deletePalette(id) {
this.setState(
(st) => ({
palettes: st.palettes.filter((palette) => palette.id !== id),
}),
this.syncLocalStorage
);
}
savePalette(newPalette) {
this.setState(
{ palettes: [...this.state.palettes, newPalette] },
this.syncLocalStorage
);
}
syncLocalStorage() {
//save palettes to local storage
window.localStorage.setItem(
"palettes",
JSON.stringify(this.state.palettes)
);
}
render() {
return (
<Route
render={({ location }) => (
<TransitionGroup>
<CSSTransition key={location.key} classNames="page" timeout={500}>
<Switch location={location}>
<Route
exact
path="/palette/new"
render={(routeProps) => (
<Page>
<NewPaletteForm
savePalette={this.savePalette}
palettes={this.state.palettes}
{...routeProps}
/>
</Page>
)}
/>
<Route
exact
path="/palette/:paletteId/:colorId"
render={(routeProps) => (
<Page>
<SingleColorPalette
colorId={routeProps.match.params.colorId}
palette={generatePalette(
this.findPalette(routeProps.match.params.paletteId)
)}
/>
</Page>
)}
/>
<Route
exact
path="/"
render={(routeProps) => (
<Page>
<PaletteList
palettes={this.state.palettes}
deletePalette={this.deletePalette}
{...routeProps}
/>
</Page>
)}
/>
<Route
exact
path="/palette/:id"
render={(routeProps) => (
<Page>
<Palette
palette={generatePalette(
this.findPalette(routeProps.match.params.id)
)}
/>
</Page>
)}
/>
<Route
render={(routeProps) => (
<Page>
<PaletteList
palettes={this.state.palettes}
deletePalette={this.deletePalette}
{...routeProps}
/>
</Page>
)}
/>
</Switch>
</CSSTransition>
</TransitionGroup>
)}
/>
);
}
}
export default App;