-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy paththough.ts
132 lines (113 loc) · 2.97 KB
/
though.ts
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
export function getNewExpirationTime() {
return Date.now() + 15 * 1000;
}
let nextId = 0;
export function generateId() {
const result = nextId;
nextId += 1;
return result;
}
import React, { useEffect, useState } from "react";
export function Thought(props) {
const { thought, removeThought } = props;
const handleRemoveClick = (event) => {
event.preventDefault();
removeThought(thought.id);
};
useEffect(() => {
const timeRemaining = thought.expiresAt - Date.now();
const timeout = setTimeout(() => {
props.removeThought(thought.id);
}, timeRemaining);
return () => {
clearTimeout(timeout);
};
}, [thought]);
return (
<li className="Thought">
<button
aria-label="Remove thought"
className="remove-button"
onClick={handleRemoveClick}
>
×
</button>
<div className="text">{thought.text}</div>
</li>
);
}
import React, { useState } from "react";
import { generateId, getNewExpirationTime } from "./utilities";
export function AddThoughtForm(props) {
const [text, setText] = useState("");
const handleSubmit = (event) => {
event.preventDefault();
if (text.length > 0) {
const thought = {
id: generateId(),
text: text,
expiresAt: getNewExpirationTime(),
};
props.addThought(thought);
setText('');
};
};
const handleTextChange = (event) => setText(event.target.value);
return (
<form className="AddThoughtForm" onClick={handleSubmit}>
<input
type="text"
aria-label="What's on your mind?"
placeholder="What's on your mind?"
value={text}
onChange={handleTextChange}
/>
<input type="submit" value="Add" />
</form>
);
}
import React, { useState } from "react";
import { AddThoughtForm } from "./AddThoughtForm";
import { Thought } from "./Thought";
import { generateId, getNewExpirationTime } from "./utilities";
export default function App(props) {
const [thoughts, setThoughts] = useState([
{
id: generateId(),
text: "This is a place for your passing thoughts.",
expiresAt: getNewExpirationTime(),
},
{
id: generateId(),
text: "They'll be removed after 15 seconds.",
expiresAt: getNewExpirationTime(),
},
]);
const addThought = (thought) => {
setThoughts((thoughts) => [thought, ...thoughts]);
};
const removeThought = (thoughtIdToRemove) => {
setThoughts((thoughts) =>
thoughts.filter((thought) => thought.id !== thoughtIdToRemove)
);
};
return (
<div className="App">
<header>
<h1>Passing Thoughts</h1>
</header>
<main>
<AddThoughtForm addThought={addThought} />
<ul className="thoughts">
{thoughts.map((thought) => (
<Thought
key={thought.id}
thought={thought}
removeThought={removeThought}
/>
))}
</ul>
</main>
</div>
);
}