-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.html
144 lines (136 loc) · 3.86 KB
/
index.html
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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
<html>
<head>
<script
src="https://unpkg.com/react@16/umd/react.development.js"
crossorigin
></script>
<script
src="https://unpkg.com/react-dom@16/umd/react-dom.development.js"
crossorigin
></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.0/axios.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/4.0.4/redux.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/axios/0.19.0/axios.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/react-router-dom/5.0.1/react-router-dom.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/moment.js/2.24.0/moment.js"></script>
<style>
body {
font-family: verdana;
}
nav {
display: flex;
justify-content: space-around;
}
a {
padding: 0.5rem;
}
.active {
background-color: tomato;
color: white;
}
</style>
</head>
<body>
<div id="root"></div>
<script type="text/babel">
const { createStore, combineReducers } = Redux;
const { HashRouter, Link, Route } = ReactRouterDOM;
const initialState = {
events: [],
title: "",
description: "",
date: "",
};
const reducer = (state = initialState, action) => {
switch (action.type) {
case "GOT.EVENTS":
return {
...state,
events: action.events,
};
default:
return state;
}
};
const store = createStore(reducer);
class Events extends React.Component {
render() {
return null;
}
}
class Nav extends React.Component {
render() {
const { events } = store.getState();
return (
<div>
<Link to="/">Home </Link>
<Link to="/events">{`Events (${events.length})`} </Link>
</div>
);
}
}
class Home extends React.Component {
render() {
const { events } = store.getState();
return <h1>You have {events.length} events!</h1>;
}
}
class App extends React.Component {
state = {
events: [],
};
componentDidMount() {
this.unsubscribe = store.subscribe(() =>
this.setState(store.getState())
);
axios.get("/api/events").then((res) => {
console.log(res.data);
// this.setState({
// events: res.data,
// });
store.dispatch({
type: "GOT.EVENTS",
events: res.data,
});
});
}
fetchEvents() {
store.getState();
}
render() {
console.log("rendering state ", this.state);
return (
<HashRouter>
<h1> Acme Events with Redux </h1>
<Route component={Nav} />
<Route path="/" component={Home} exact />
<Route path="/events" component={Events} />
</HashRouter>
);
}
}
const connect = (Component) => {
class Connected extends React.Component {
constructor() {
super();
this.state = store.getState();
}
componentWillUnmount() {
this.unsubscribe();
}
componentDidMount() {
this.unsubscribe = store.subscribe(() =>
this.setState(store.getState())
);
}
render() {
return <Component {...this.state} {...this.props} />;
}
}
return Connected;
};
ReactDOM.render(<App />, document.querySelector("#root"));
</script>
</body>
</html>