-
Notifications
You must be signed in to change notification settings - Fork 17
/
notes-semantic.js
166 lines (152 loc) · 4.1 KB
/
notes-semantic.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
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import React, { useState } from 'react'
import {
BrowserRouter as Router,
Route, Link, Redirect, withRouter
} from 'react-router-dom'
import { Container, Table, Form, Button, Message, Menu } from 'semantic-ui-react'
const Home = () => (
<div>
<h2>TKTL notes app</h2>
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.
</div>
)
const Note = ({ note }) => {
return (
<div>
<h2>{note.content}</h2>
<div>{note.user}</div>
<div><strong>{note.important ? 'tärkeä' : ''}</strong></div>
</div>
)
}
const Notes = (props) => (
<div>
<h2>Notes</h2>
<Table striped celled>
<Table.Body>
{props.notes.map(note =>
<Table.Row key={note.id}>
<Table.Cell>
<Link to={`/notes/${note.id}`}>
{note.content}
</Link>
</Table.Cell>
<Table.Cell>
{note.user}
</Table.Cell>
</Table.Row>
)}
</Table.Body>
</Table>
</div>
)
const Users = () => (
<div>
<h2>TKTL notes app</h2>
<ul>
<li>Matti Luukkainen</li>
<li>Juha Tauriainen</li>
<li>Arto Hellas</li>
</ul>
</div>
)
let Login = (props) => {
const onSubmit = (event) => {
event.preventDefault()
props.onLogin('mluukkai')
props.history.push('/')
}
return (
<Form onSubmit={onSubmit}>
<Form.Field>
<label>username</label>
<input name='username' />
</Form.Field>
<Form.Field>
<label>password</label>
<input type='password' />
</Form.Field>
<Button type='submit'>login</Button>
</Form>
)
}
Login = withRouter(Login)
const App = () => {
const [notes, setNotes] = useState([
{
id: 1,
content: 'HTML is easy',
important: true,
user: 'Matti Luukkainen'
},
{
id: 2,
content: 'Browser can execute only Javascript',
important: false,
user: 'Matti Luukkainen'
},
{
id: 3,
content: 'The most important methods of HTTP protocol are ovat GET are POST',
important: true,
user: 'Arto Hellas'
}
])
const [user, setUser] = useState(null)
const [message, setMessage] = useState(null)
const login = (user) => {
setUser(user)
setMessage(`welcome ${user}`)
setTimeout(() => {
setMessage(null)
}, 10000)
}
const noteById = (id) =>
notes.find(note => note.id === Number(id))
return (
<Container>
{(message &&
<Message success>
{message}
</Message>
)}
<Router>
<div>
<Menu inverted>
<Menu.Item link>
<Link to="/">home</Link>
</Menu.Item>
<Menu.Item link>
<Link to="/notes">notes</Link>
</Menu.Item>
<Menu.Item link>
<Link to="/users">users</Link>
</Menu.Item>
<Menu.Item link>
{user
? <em>{user} logged in</em>
: <Link to="/login">login</Link>
}
</Menu.Item>
</Menu>
<Route exact path="/" render={() => <Home />} />
<Route exact path="/notes" render={() => <Notes notes={notes} />} />
<Route exact path="/notes/:id" render={({ match }) =>
<Note note={noteById(match.params.id)} />}
/>
<Route path="/users" render={() =>
user ? <Users /> : <Redirect to="/login" />
} />
<Route path="/login" render={() =>
<Login onLogin={login} />}
/>
</div>
</Router>
<div>
<br />
<em>Note app, Department of Computer Science 2019</em>
</div>
</Container>
)
}
export default App