Account.js 9.24 KB
Newer Older
kusang96's avatar
kusang96 committed
1
2
import React, { useEffect, useState } from 'react';
import { Card, Image, Container, Row, Col, Table, Accordion, Button, Form, Modal, Alert } from 'react-bootstrap';
3
import { Link } from 'react-router-dom';
kusang96's avatar
kusang96 committed
4
5
import axios from 'axios';
import catchError from '../utils/catchErrors';
6
import { isAuthenticated } from '../utils/auth';
Jiwon Yoon's avatar
Jiwon Yoon committed
7

8
9
const INIT_ACCOUNT = {
    name: "",
박상호's avatar
박상호 committed
10
    avatarUrl: ''
11
}
Jiwon Yoon's avatar
Jiwon Yoon committed
12
13

function Account() {
14
    const [account, setAccount] = useState(INIT_ACCOUNT)
kusang96's avatar
kusang96 committed
15
    const [show, setShow] = useState(false);
박상호's avatar
0120    
박상호 committed
16
    const [proshow, setProshow] = useState(false)
17
18
    const [error, setError] = useState("")
    const userId = isAuthenticated()
19
    const [ordered, setOrdered] = useState('')
kusang96's avatar
kusang96 committed
20

21
22
23
24
25
26

    async function getUsername(user) {
        try {
            const response = await axios.get(`/api/users/account/${user}`)
            setAccount(response.data)
        } catch (error) {
kusang96's avatar
kusang96 committed
27
            catchError(error, setError)
28
29
30
31
32
        }
    }

    useEffect(() => {
        getUsername(userId)
33
        getOrdered(userId)
34
35
    }, [userId])

박상호's avatar
박상호 committed
36
37
38
39
40
41
42
43
    const handleChange = (event) => {
        const { name, value, files } = event.target
        if (files) {
            setAccount({ ...account, [name]: files })
        } else {
            setAccount({ ...account, [name]: value })
        }
    }
44

kusang96's avatar
kusang96 committed
45
    const handleBasic = async () => {
박상호's avatar
박상호 committed
46
47
48
49
50
51
52
53
        const formData = new FormData()
        formData.append('avatar', '')
        try {
            if (userId) {
                const response = await axios.put(`/api/users/account/${userId}`, formData)
                window.location.reload()
            }
        } catch (error) {
kusang96's avatar
kusang96 committed
54
            catchError(error, setError)
박상호's avatar
박상호 committed
55
56
57
        }
        setShow(false)
    }
58
59


박상호's avatar
박상호 committed
60
61
62
63
64
65
66
67
68
69
70
    const handleSubmit = async (event) => {
        event.preventDefault()
        if (account.avatar) {
            const formData = new FormData()
            formData.append('avatar', account.avatar[0])
            try {
                if (userId) {
                    const response = await axios.put(`/api/users/account/${userId}`, formData)
                    window.location.reload()
                }
            } catch (error) {
kusang96's avatar
kusang96 committed
71
                catchError(error, setError)
박상호's avatar
박상호 committed
72
73
74
            }
        } else {
            alert("파일을 선택해주세요.")
kusang96's avatar
kusang96 committed
75
        }
박상호's avatar
박상호 committed
76
    }
77

kusang96's avatar
kusang96 committed
78
    async function getOrdered({ }) {
79
80
81
82
83
84
85
86
        try {
            const response = await axios.get(`/api/users/addorder`)
            setOrdered(response.data)
        } catch (error) {
            catchError(error, setError)
        }
    }

kusang96's avatar
kusang96 committed
87

박상호's avatar
박상호 committed
88
    return (
89
        <Container className="px-3">
kusang96's avatar
kusang96 committed
90
91
92
93
94
95
96
97
            <style type="text/css">
                {`
                a, a:hover, a:active {
                    color: #91877F;
                    text-decoration-color: #91877F;
                }
                `}
            </style>
98
99
100
            <h3 className="my-4 mx-3 font-weight-bold">My Page</h3>
            <Card md={3} className="p-1 mb-4" style={{ background: '#F7F3F3' }}>
                <Row className="p-2">
박상호's avatar
박상호 committed
101
                    <Col md={5} className="d-flex align-content-center justify-content-center">
kusang96's avatar
kusang96 committed
102
                        <Button variant="outline-light" onClick={() => setShow(true)}>
박상호's avatar
박상호 committed
103
                            {account.avatarUrl ? (
kusang96's avatar
kusang96 committed
104
                                <Image src={account.avatarUrl && `/images/${account.avatarUrl}`} className="img-thumbnail"
박상호's avatar
박상호 committed
105
106
                                    roundedCircle style={{ objectFit: "cover", width: "10rem", height: "10rem" }} />
                            ) : (
kusang96's avatar
0115    
kusang96 committed
107
                                    <Image src="/icon/person.svg" className="img-thumbnail"
박상호's avatar
박상호 committed
108
109
                                        roundedCircle style={{ objectFit: "cover", width: "10rem", height: "10rem" }} />
                                )}
110
                        </Button>
kusang96's avatar
kusang96 committed
111
                        <Modal show={show} onHide={() => setShow(false)}>
112
                            <Modal.Header closeButton style={{ background: "#F7F3F3" }}>
박상호's avatar
0120    
박상호 committed
113
                                <Modal.Title >이미지를 변경하시겠습니까?</Modal.Title>
박상호's avatar
박상호 committed
114
115
116
117
118
119
120
121
122
123
124
                            </Modal.Header>
                            <Form onSubmit={handleSubmit}>
                                <Modal.Body>
                                    <Form.Control type="file" name="avatar" onChange={handleChange} />
                                </Modal.Body>
                                <Modal.Footer>
                                    <Col className="px-0">
                                        <Button variant="outline-secondary" onClick={handleBasic}
                                            className="d-flex justify-content-start"><small>기본이미지로</small></Button>
                                        {/* 기본이미지로 보내기 */}
                                    </Col>
kusang96's avatar
kusang96 committed
125
126
                                    <Button variant="secondary" onClick={() => setShow(false)}>취소</Button>
                                    <Button variant="primary" type="submit" onClick={() => setShow(false)}>저장</Button>
박상호's avatar
박상호 committed
127
128
129
                                </Modal.Footer>
                            </Form>
                        </Modal>
130
131
132
133
134
                    </Col>
                    <Col >
                        <Row className="mt-4 text-center">
                            <Col>
                                <h2>
박상호's avatar
0120    
박상호 committed
135
136
137
138
                                    <strong title='회원정보' style={{ cursor: "pointer", textDecoration: 'underline' }} onClick={() => setProshow(true)}>
                                        {account.name}
                                    </strong>
                                    <Modal
139

박상호's avatar
0120    
박상호 committed
140
141
142
                                        size="sm"
                                        show={proshow}
                                        onHide={() => setProshow(false)}>
143
                                        <Modal.Header closeButton style={{ background: "#F7F3F3" }}>
박상호's avatar
0120    
박상호 committed
144
145
146
147
148
149
150
151
152
153
154
155
156
                                            <Modal.Title>회원정보</Modal.Title>
                                        </Modal.Header>
                                        <Modal.Body>
                                            <Col className="p-1">
                                                <li><strong>Role :</strong> {account.role}</li>
                                                <li><strong>ID :</strong> {account.id}</li>
                                                <li><strong>Username :</strong> {account.name}</li>
                                                <li><strong>Email :</strong> {account.email}</li>
                                                <li><strong>Tel :</strong> {account.tel}</li>
                                            </Col>
                                        </Modal.Body>
                                    </Modal>
                                    <small>{' '}({account.id}){" "}</small>
157
158
159
160
161
                                </h2>
                            </Col>
                        </Row>
                        <Row className="px-3">
                            <Card.Body className="p-2 text-center">
kusang96's avatar
kusang96 committed
162
                                <h4><Link to="/">
163
                                    <strong title="홈으로">
kusang96's avatar
0115    
kusang96 committed
164
                                        <Image src="/icon/mypagetiger.svg" width={"30rem"} roundedCircle className="img-thumbnail" >
165
                                        </Image>KU#
kusang96's avatar
kusang96 committed
166
                                    </strong>
167
                                </Link>
kusang96's avatar
kusang96 committed
168
169
170
                 방문해주신 <em>{account.name}</em> 님,<br />
                진심으로 환영합니다! 즐거운 쇼핑 되세요.
                </h4>
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
                            </Card.Body>
                        </Row>
                        <Row className="mr-1 text-muted d-flex justify-content-end">
                            <a href="mailto:shoppingmall_KU@korea.ac.kr">
                                <small title="메일보내기"> * 문의 : shoppingmall_KU@korea.ac.kr </small>
                            </a>
                        </Row>
                    </Col>
                </Row>
            </Card>
            <Accordion>
                <Row className="my-3 px-3">
                    <Table>
                        <thead className="text-center" style={{ background: '#F7F3F3' }}>
                            <tr>
                                <th scope="col">주문현황</th>
                                <th scope="col">배송중</th>
                                <th scope="col">배송완료</th>
                            </tr>
                        </thead>
                        <tbody>
                            <tr>
                                <th scope="row">케이시앵글부츠(SH)</th>
                                <td>Mark</td>
                                <td>Otto</td>
                            </tr>
                            <tr>
                                <th scope="row">2</th>
                                <td>Jacob</td>
                                <td>Thornton</td>
                            </tr>
                            <tr>
                                <th scope="row">3</th>
                                <td colspan="2">Larry the Bird</td>
                            </tr>
                        </tbody>
                    </Table>
                </Row>
            </Accordion>
        </Container >
Jiwon Yoon's avatar
Jiwon Yoon committed
211
    )
박상호's avatar
박상호 committed
212
}
Jiwon Yoon's avatar
Jiwon Yoon committed
213

kusang96's avatar
0115    
kusang96 committed
214
export default Account