swa07016

'/api/delete' mypick user card 삭제 기능 구현

1 /*global kakao*/ 1 /*global kakao*/
2 -import React, { useEffect, useState, Fragment } from "react"; 2 +import React, { useEffect, useState } from "react";
3 -import { Input, Button, ButtonGroup } from "reactstrap"; 3 +import { Input, } from "reactstrap";
4 -import { Label, CustomInput, Row, Col, FormGroup } from "reactstrap"; 4 +import { Label, Row, Col, FormGroup } from "reactstrap";
5 import appKey from "../config/appKey.json"; 5 import appKey from "../config/appKey.json";
6 6
7 const LandingMap = (props) => { 7 const LandingMap = (props) => {
...@@ -23,7 +23,6 @@ const LandingMap = (props) => { ...@@ -23,7 +23,6 @@ const LandingMap = (props) => {
23 document.head.appendChild(script); 23 document.head.appendChild(script);
24 24
25 script.onload = () => { 25 script.onload = () => {
26 - console.log(FDatas);
27 kakao.maps.load(() => { 26 kakao.maps.load(() => {
28 let container = document.getElementById("map"); 27 let container = document.getElementById("map");
29 let options = { 28 let options = {
...@@ -111,12 +110,11 @@ const LandingMap = (props) => { ...@@ -111,12 +110,11 @@ const LandingMap = (props) => {
111 } 110 }
112 } 111 }
113 setFDatas(result); 112 setFDatas(result);
114 - }, [selectedOption]); 113 + }, [selectedOption, props]);
115 114
116 115
117 116
118 const handleOptionChange = (e) => { 117 const handleOptionChange = (e) => {
119 - console.log(e.target.value);
120 setselectedOption(e.target.value); 118 setselectedOption(e.target.value);
121 } 119 }
122 120
......
...@@ -10,12 +10,16 @@ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome" ...@@ -10,12 +10,16 @@ import { FontAwesomeIcon } from "@fortawesome/react-fontawesome"
10 const MealCard = (props) => { 10 const MealCard = (props) => {
11 11
12 const [modal, setModal] = useState(false); 12 const [modal, setModal] = useState(false);
13 - const [isLogin, setIsLogin] = useState(false);
14 const toggleModal = () => setModal(!modal); 13 const toggleModal = () => setModal(!modal);
15 14
16 15
17 const authApi = () => { 16 const authApi = () => {
18 const user = JSON.parse(localStorage.getItem('user')); 17 const user = JSON.parse(localStorage.getItem('user'));
18 + if(!user) {
19 + alert('로그인이 필요합니다.');
20 + window.location.href = "/mypick";
21 + return ;
22 + }
19 return fetch('/api/auth', { 23 return fetch('/api/auth', {
20 method: 'GET', 24 method: 'GET',
21 headers: { 25 headers: {
...@@ -45,7 +49,7 @@ const MealCard = (props) => { ...@@ -45,7 +49,7 @@ const MealCard = (props) => {
45 alert('error'); 49 alert('error');
46 } 50 }
47 }); 51 });
48 - setIsLogin(true); 52 +
49 } else { 53 } else {
50 alert('로그인이 필요합니다.'); 54 alert('로그인이 필요합니다.');
51 window.location.href = "/mypick"; 55 window.location.href = "/mypick";
...@@ -59,11 +63,6 @@ const MealCard = (props) => { ...@@ -59,11 +63,6 @@ const MealCard = (props) => {
59 authApi(); 63 authApi();
60 } 64 }
61 65
62 - // useEffect(() => {
63 - // authApi();
64 - // }, [isLogin]);
65 -
66 -
67 return ( 66 return (
68 <> 67 <>
69 <Card style={{ 68 <Card style={{
......
...@@ -35,7 +35,7 @@ const NavBar = (props) => { ...@@ -35,7 +35,7 @@ const NavBar = (props) => {
35 <NavLink href="/mypick" style={{'color':'#fff'}}>MyPick</NavLink> 35 <NavLink href="/mypick" style={{'color':'#fff'}}>MyPick</NavLink>
36 </NavItem> 36 </NavItem>
37 </Nav> 37 </Nav>
38 - <NavbarText><a href="http://khuhub.khu.ac.kr/2019102227/TermProject" target="_blank" style={{'color':'#fff', 'textDecoration':'none'}}>OSS Project</a></NavbarText> 38 + <NavbarText><a href="http://khuhub.khu.ac.kr/2019102227/TermProject" target="_blank" rel="noopener" style={{'color':'#fff', 'textDecoration':'none'}}>OSS Project</a></NavbarText>
39 </Collapse> 39 </Collapse>
40 </Container> 40 </Container>
41 </Navbar> 41 </Navbar>
......
...@@ -12,6 +12,56 @@ const MealCard = (props) => { ...@@ -12,6 +12,56 @@ const MealCard = (props) => {
12 const [modal, setModal] = useState(false); 12 const [modal, setModal] = useState(false);
13 const toggleModal = () => setModal(!modal); 13 const toggleModal = () => setModal(!modal);
14 14
15 + const authApi = () => {
16 + const user = JSON.parse(localStorage.getItem('user'));
17 + if(!user) {
18 + alert('토큰이 만료되었습니다.');
19 + window.location.href = "/mypick";
20 + return ;
21 + }
22 + return fetch('/api/auth', {
23 + method: 'GET',
24 + headers: {
25 + 'Content-Type': 'application/json',
26 + 'authorization': user
27 + }
28 + }).then(response => response.json())
29 + .then(result => {
30 + if(result.message === 'valid token') {
31 +
32 + return fetch('/api/delete', {
33 + method: 'POST',
34 + headers: {
35 + 'Content-Type': 'application/json',
36 + 'authorization': user
37 + },
38 + body: JSON.stringify({
39 + "cardid":props.id
40 + })
41 + }).then(response => response.json())
42 + .then(result => {
43 + if(result.message === 'delete success') {
44 + alert('delete success');
45 + window.location.href = "/mypick";
46 + } else if(result.message === 'delete error') {
47 + alert('delete error');
48 + } else {
49 + alert('error');
50 + }
51 + });
52 + } else {
53 + alert('토큰이 만료되었습니다.');
54 + window.location.href = "/mypick";
55 + }
56 + });
57 +}
58 +
59 +
60 + const deleteHandler = (e) => {
61 + e.preventDefault();
62 + authApi();
63 + }
64 +
15 return ( 65 return (
16 <> 66 <>
17 <Card style={{ 67 <Card style={{
...@@ -80,7 +130,7 @@ const MealCard = (props) => { ...@@ -80,7 +130,7 @@ const MealCard = (props) => {
80 <small> 130 <small>
81 썸네일 출처 131 썸네일 출처
82 <hr className="my-2"/> 132 <hr className="my-2"/>
83 - {props.img_source} <Button size="sm" className="float-right" color="danger">Delete</Button> 133 + {props.img_source} <Button size="sm" onClick={deleteHandler} className="float-right" color="danger">Delete</Button>
84 </small> 134 </small>
85 </div> 135 </div>
86 </ModalFooter> 136 </ModalFooter>
......
1 import React, { useState, useEffect } from 'react'; 1 import React, { useState, useEffect } from 'react';
2 import { Container, Row, Col, Button } from 'reactstrap'; 2 import { Container, Row, Col, Button } from 'reactstrap';
3 -import cookie from 'react-cookies';
4 -import axios from 'axios';
5 import PickedCard from '../components/PickedCard'; 3 import PickedCard from '../components/PickedCard';
6 4
7 const UserCards = (props) => { 5 const UserCards = (props) => {
...@@ -10,6 +8,7 @@ const UserCards = (props) => { ...@@ -10,6 +8,7 @@ const UserCards = (props) => {
10 8
11 const authApi = () => { 9 const authApi = () => {
12 const user = JSON.parse(localStorage.getItem('user')); 10 const user = JSON.parse(localStorage.getItem('user'));
11 +
13 return fetch('/api/mypicks', { 12 return fetch('/api/mypicks', {
14 method: 'GET', 13 method: 'GET',
15 headers: { 14 headers: {
...@@ -19,7 +18,6 @@ const UserCards = (props) => { ...@@ -19,7 +18,6 @@ const UserCards = (props) => {
19 }).then(response => response.json()) 18 }).then(response => response.json())
20 .then( result => { 19 .then( result => {
21 setPicks(result.datas); 20 setPicks(result.datas);
22 - console.log(result.datas);
23 } 21 }
24 ); 22 );
25 } 23 }
...@@ -27,7 +25,7 @@ const UserCards = (props) => { ...@@ -27,7 +25,7 @@ const UserCards = (props) => {
27 useEffect(() => { 25 useEffect(() => {
28 setUsername(props.username); 26 setUsername(props.username);
29 authApi(); 27 authApi();
30 - }, [username]); 28 + }, [username, props]);
31 29
32 30
33 31
...@@ -50,7 +48,7 @@ const UserCards = (props) => { ...@@ -50,7 +48,7 @@ const UserCards = (props) => {
50 <Container style={{'paddingTop':'1.2rem'}}> 48 <Container style={{'paddingTop':'1.2rem'}}>
51 <Row xs="2" sm="2" md="4"> 49 <Row xs="2" sm="2" md="4">
52 {picks.map((data, index) => 50 {picks.map((data, index) =>
53 - <Col> 51 + <Col key = {index}>
54 <PickedCard 52 <PickedCard
55 key = {index} 53 key = {index}
56 id = {data.id} 54 id = {data.id}
......
...@@ -9,12 +9,7 @@ import 'react-app-polyfill/ie11'; ...@@ -9,12 +9,7 @@ import 'react-app-polyfill/ie11';
9 // import 'bootstrap-css-only/css/bootstrap.min.css'; 9 // import 'bootstrap-css-only/css/bootstrap.min.css';
10 // import 'mdbreact/dist/css/mdb.css'; 10 // import 'mdbreact/dist/css/mdb.css';
11 11
12 -ReactDOM.render( 12 +ReactDOM.render( <App />, document.getElementById('root'));
13 - <React.StrictMode>
14 - <App />
15 - </React.StrictMode>,
16 - document.getElementById('root')
17 -);
18 13
19 // If you want your app to work offline and load faster, you can change 14 // If you want your app to work offline and load faster, you can change
20 // unregister() to register() below. Note this comes with some pitfalls. 15 // unregister() to register() below. Note this comes with some pitfalls.
......
1 -import React, { useState, useEffect, Fragment } from 'react'; 1 +import React, { useState, useEffect} from 'react';
2 import NavBar from '../components/NavBar'; 2 import NavBar from '../components/NavBar';
3 import LandingMap from '../components/LandingMap'; 3 import LandingMap from '../components/LandingMap';
4 import { CustomInput } from 'reactstrap'; 4 import { CustomInput } from 'reactstrap';
...@@ -55,7 +55,7 @@ const LandingPage = (props) => { ...@@ -55,7 +55,7 @@ const LandingPage = (props) => {
55 } 55 }
56 setFilteredDatas(result); 56 setFilteredDatas(result);
57 setIsLoading(true); 57 setIsLoading(true);
58 - }, [Kfood, Cfood, Jfood, meat, snackfood, pub, fastfood, cafe, etc]); 58 + }, [Kfood, Cfood, Jfood, meat, snackfood, pub, fastfood, cafe, etc, datas]);
59 59
60 useEffect(() => { 60 useEffect(() => {
61 if(all === true) { 61 if(all === true) {
...@@ -95,7 +95,7 @@ const LandingPage = (props) => { ...@@ -95,7 +95,7 @@ const LandingPage = (props) => {
95 let y = -1; 95 let y = -1;
96 while(1) { 96 while(1) {
97 y = getRandomInt(0, filteredDatas.length); 97 y = getRandomInt(0, filteredDatas.length);
98 - if(x!=y) break; 98 + if(x!==y) break;
99 } 99 }
100 setRandomCards([filteredDatas[x], filteredDatas[y]]); 100 setRandomCards([filteredDatas[x], filteredDatas[y]]);
101 setIsRandom(2); 101 setIsRandom(2);
......
...@@ -57,7 +57,7 @@ const MenuPage = (props) => { ...@@ -57,7 +57,7 @@ const MenuPage = (props) => {
57 } 57 }
58 setFilteredDatas(result); 58 setFilteredDatas(result);
59 setIsLoading(true); 59 setIsLoading(true);
60 - }, [Kfood, Cfood, Jfood, meat, snackfood, pub, fastfood, cafe, etc]); 60 + }, [Kfood, Cfood, Jfood, meat, snackfood, pub, fastfood, cafe, etc, datas]);
61 61
62 useEffect(() => { 62 useEffect(() => {
63 if(all === true) { 63 if(all === true) {
......
...@@ -59,7 +59,6 @@ app.post("/api/signup", (req, res) => { ...@@ -59,7 +59,6 @@ app.post("/api/signup", (req, res) => {
59 const params = [req.body.username, hash]; 59 const params = [req.body.username, hash];
60 connection.query(sql, params, (err, rows, fields) => { 60 connection.query(sql, params, (err, rows, fields) => {
61 if (err) { 61 if (err) {
62 - console.log(err);
63 res.send({ 62 res.send({
64 code: 400, 63 code: 400,
65 message: "error", 64 message: "error",
...@@ -202,9 +201,11 @@ app.post('/api/pick', (req, res) => { ...@@ -202,9 +201,11 @@ app.post('/api/pick', (req, res) => {
202 201
203 connection.query(`SELECT pick FROM USER WHERE NAME='${username}';`, (err, rows, fileds)=> { 202 connection.query(`SELECT pick FROM USER WHERE NAME='${username}';`, (err, rows, fileds)=> {
204 if(rows.length === 0) { 203 if(rows.length === 0) {
205 - // ?? 204 + return res.status(401).json({
205 + code: 401,
206 + message: 'card exist'
207 + });
206 } else { 208 } else {
207 - // ???? ??
208 let flag = true; 209 let flag = true;
209 let user_picks = rows[0].pick.split(','); 210 let user_picks = rows[0].pick.split(',');
210 user_picks.pop(); 211 user_picks.pop();
...@@ -233,4 +234,47 @@ app.post('/api/pick', (req, res) => { ...@@ -233,4 +234,47 @@ app.post('/api/pick', (req, res) => {
233 }) 234 })
234 }); 235 });
235 236
237 +app.post('/api/delete', (req, res) => {
238 + const user = jwt_decode(req.headers.authorization);
239 + const username = user.name;
240 + const cardid = req.body.cardid;
241 +
242 + connection.query(`SELECT pick FROM USER WHERE NAME='${username}';`, (err, rows, fileds)=> {
243 + if(rows.length === 0) {
244 + return res.status(401).json({
245 + code: 401,
246 + message: 'card exist'
247 + });
248 + } else {
249 + let flag = false;
250 + let user_picks = rows[0].pick.split(',');
251 + let newPick = '';
252 + user_picks.pop();
253 + for(let i=0; i<user_picks.length; i++) {
254 + if(user_picks[i] == cardid) {
255 + flag = true;
256 + continue;
257 + }
258 + else newPick = newPick + (user_picks[i] + ',');
259 + }
260 + if(flag) {
261 + console.log(user_picks, newPick);
262 + connection.query(`UPDATE USER SET pick='${newPick}' WHERE NAME='${username}';`, (err, rows, fields) => {
263 + return res.status(200).json({
264 + code: 200,
265 + message: 'delete success',
266 + });
267 + })
268 + } else {
269 + return res.status(401).json({
270 + code: 401,
271 + message: 'delete error'
272 + });
273 + }
274 +
275 + }
276 + })
277 +
278 +});
279 +
236 app.listen(port, () => console.log(`Listening on port ${port}`)); 280 app.listen(port, () => console.log(`Listening on port ${port}`));
...\ No newline at end of file ...\ No newline at end of file
......