-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
executable file
·161 lines (145 loc) · 4.56 KB
/
index.html
File metadata and controls
executable file
·161 lines (145 loc) · 4.56 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
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
<!DOCTYPE html>
<html lang="ko">
<head>
<meta charset="UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>고양이 가라사대</title>
</head>
<style>
body {
text-align: center;
}
.main-card button {
position: relative;
left: -45px;
bottom: 15px;
}
.favorites {
list-style: none;
display: flex;
justify-content: center;
flex-wrap: wrap;
gap: 15px;
}
</style>
<body>
<div id="app"></div>
<script
src="https://unpkg.com/react@17/umd/react.development.js"
crossorigin
></script>
<script
src="https://unpkg.com/react-dom@17/umd/react-dom.development.js"
crossorigin
></script>
<script src="https://unpkg.com/babel-standalone@6/babel.min.js"></script>
<script type="text/babel">
const jsonLocalStorage = {
setItem: (key, value) => {
localStorage.setItem(key, JSON.stringify(value));
},
getItem: (key) => {
return JSON.parse(localStorage.getItem(key));
},
}; //localStorage의 데이터 값을 string type으로만 받아오는 불편함 방지
const Title = (props) => {
return <h1>{props.children}</h1>;
};
const Form = ({updateMainCat}) => {
const includesHangul = (text) => /[ㄱ-ㅎ|ㅏ-ㅣ|가-힣]/i.test(text);
const [value, setValue] = React.useState("");
const [errorMessage, setErrorMessage] = React.useState('');
function handleInputChange(e) {
const userValue = e.target.value;
setValue(userValue.toUpperCase());
setErrorMessage('');
if (includesHangul(userValue)) {
setErrorMessage('한글은 입력할 수 없습니다.')
}
}
function handleFormSubmit(e) {
e.preventDefault();
setErrorMessage('');
if (value == '') {
setErrorMessage('빈 값으로 만들 수 없습니다.');
return;
}
updateMainCat();
}
return (
<form onSubmit={handleFormSubmit}>
<input
type="text"
name="name"
placeholder="영어 대사를 입력해주세요"
value={value}
onChange={handleInputChange}
/>
<button type="submit">생성</button>
<p style={{color: 'red'}}>{errorMessage}</p>
</form>
);
};
function CatItem(props) {
return (
<li>
<img src={props.img} style={{ width: "150px" }} />
</li>
);
}
function Favorites({ favorites }) {
return (
<ul className="favorites">
{favorites.map((cat) => (
<CatItem img={cat} key={cat} />
))}
</ul>
);
}
const MainCard = ({ img, onHeartClick }) => {
return (
<div className="main-card">
<img src={img} alt="고양이" width="400" />
<button onClick={onHeartClick}>🤍</button>
</div>
);
};
const App = () => {
const CAT1 =
"https://cataas.com/cat/60b73094e04e18001194a309/says/react";
const CAT2 =
"https://cataas.com//cat/5e9970351b7a400011744233/says/inflearn";
const CAT3 =
"https://cataas.com/cat/595f280b557291a9750ebf65/says/JavaScript";
const [counter, setCounter] = React.useState(
jsonLocalStorage.getItem('counter')
);
const [mainCat, setMainCat] = React.useState(CAT1);
const [favorites, setFavorites] = React.useState(jsonLocalStorage
.getItem(favorites) || [] );
function updateMainCat() {
const nextCounter = counter + 1;
setCounter(nextCounter);
setMainCat(CAT2);
jsonLocalStorage.setItem('counter', nextCounter)
}
function handleHeartClick() {
const nextFavorites = [...favorites, mainCat]
setFavorites(nextFavorites);
jsonLocalStorage.setItem('favorites', nextFavorites)
}
return (
<div>
<Title>{counter}번째 고양이 가라사대</Title>
<Form updateMainCat={updateMainCat} />
<MainCard img={mainCat} onHeartClick={handleHeartClick} />
<Favorites favorites={favorites} />
</div>
);
};
const 여기다가그려 = document.querySelector("#app");
ReactDOM.render(<App />, 여기다가그려);
</script>
</body>
</html>