Account.js 9.78 KB
Newer Older
1
import React, { useEffect, useState } from 'react'
박상호's avatar
박상호 committed
2
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('')
20
21

    async function getUsername(user) {
박상호's avatar
박상호 committed
22
        // console.log("tlg")
23
24
25
        try {
            const response = await axios.get(`/api/users/account/${user}`)
            setAccount(response.data)
박상호's avatar
0120    
박상호 committed
26
            console.log('555555555', response.data);
27
        } catch (error) {
kusang96's avatar
kusang96 committed
28
            catchError(error, setError)
박상호's avatar
박상호 committed
29
            // console.log('error2222', error)
30
31
32
33
34
        }
    }

    useEffect(() => {
        getUsername(userId)
35
        getOrdered(userId)
36
37
    }, [userId])

박상호's avatar
박상호 committed
38
39
40
41
42
43
44
45
46
47
48
49
    const handleChange = (event) => {
        const { name, value, files } = event.target
        if (files) {
            for (const file of files) {
                // console.log("name=", name, "value=", value, 'file=', file);
            }
            setAccount({ ...account, [name]: files })
        } else {
            console.log("name=", name, "value=", value);
            setAccount({ ...account, [name]: value })
        }
    }
50

박상호's avatar
박상호 committed
51
52
53
54
55
56
57
58
59
60
    const handleBasic = async (event) => {
        const formData = new FormData()
        formData.append('avatar', '')
        try {
            if (userId) {
                const response = await axios.put(`/api/users/account/${userId}`, formData)
                console.log(response.data)
                window.location.reload()
            }
        } catch (error) {
kusang96's avatar
kusang96 committed
61
            catchError(error, setError)
박상호's avatar
박상호 committed
62
63
64
        }
        setShow(false)
    }
65
66


박상호's avatar
박상호 committed
67
68
69
70
71
72
73
74
75
76
77
78
    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)
                    console.log(response.data)
                    window.location.reload()
                }
            } catch (error) {
kusang96's avatar
kusang96 committed
79
                catchError(error, setError)
박상호's avatar
박상호 committed
80
81
82
            }
        } else {
            alert("파일을 선택해주세요.")
kusang96's avatar
kusang96 committed
83
        }
박상호's avatar
박상호 committed
84
    }
85

86
87
88
89
90
91
92
93
94
95
96
    async function getOrdered({}) {
        console.log("object")
        try {
            const response = await axios.get(`/api/users/addorder`)
            setOrdered(response.data)
            console.log('@@@@', response.data);
        } catch (error) {
            catchError(error, setError)
        }
    }

박상호's avatar
박상호 committed
97
    return (
98
        <Container className="px-3">
kusang96's avatar
kusang96 committed
99
100
101
102
103
104
105
106
            <style type="text/css">
                {`
                a, a:hover, a:active {
                    color: #91877F;
                    text-decoration-color: #91877F;
                }
                `}
            </style>
107
108
109
            <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
110
                    <Col md={5} className="d-flex align-content-center justify-content-center">
kusang96's avatar
kusang96 committed
111
                        <Button variant="outline-light" onClick={() => setShow(true)}>
박상호's avatar
박상호 committed
112
                            {account.avatarUrl ? (
kusang96's avatar
kusang96 committed
113
                                <Image src={account.avatarUrl && `/images/${account.avatarUrl}`} className="img-thumbnail"
박상호's avatar
박상호 committed
114
115
                                    roundedCircle style={{ objectFit: "cover", width: "10rem", height: "10rem" }} />
                            ) : (
kusang96's avatar
0115    
kusang96 committed
116
                                    <Image src="/icon/person.svg" className="img-thumbnail"
박상호's avatar
박상호 committed
117
118
                                        roundedCircle style={{ objectFit: "cover", width: "10rem", height: "10rem" }} />
                                )}
119
                        </Button>
kusang96's avatar
kusang96 committed
120
                        <Modal show={show} onHide={() => setShow(false)}>
121
                            <Modal.Header closeButton style={{ background: "#F7F3F3" }}>
박상호's avatar
0120    
박상호 committed
122
                                <Modal.Title >이미지를 변경하시겠습니까?</Modal.Title>
박상호's avatar
박상호 committed
123
124
125
126
127
128
129
130
131
132
133
                            </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
134
135
                                    <Button variant="secondary" onClick={() => setShow(false)}>취소</Button>
                                    <Button variant="primary" type="submit" onClick={() => setShow(false)}>저장</Button>
박상호's avatar
박상호 committed
136
137
138
                                </Modal.Footer>
                            </Form>
                        </Modal>
139
140
141
142
143
                    </Col>
                    <Col >
                        <Row className="mt-4 text-center">
                            <Col>
                                <h2>
박상호's avatar
0120    
박상호 committed
144
145
146
147
                                    <strong title='회원정보' style={{ cursor: "pointer", textDecoration: 'underline' }} onClick={() => setProshow(true)}>
                                        {account.name}
                                    </strong>
                                    <Modal
148

박상호's avatar
0120    
박상호 committed
149
150
151
                                        size="sm"
                                        show={proshow}
                                        onHide={() => setProshow(false)}>
152
                                        <Modal.Header closeButton style={{ background: "#F7F3F3" }}>
박상호's avatar
0120    
박상호 committed
153
154
155
156
157
158
159
160
161
162
163
164
165
                                            <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>
166
167
168
169
170
                                </h2>
                            </Col>
                        </Row>
                        <Row className="px-3">
                            <Card.Body className="p-2 text-center">
kusang96's avatar
kusang96 committed
171
                                <h4><Link to="/">
172
                                    <strong title="홈으로">
kusang96's avatar
0115    
kusang96 committed
173
                                        <Image src="/icon/mypagetiger.svg" width={"30rem"} roundedCircle className="img-thumbnail" >
174
                                        </Image>KU#
kusang96's avatar
kusang96 committed
175
                                    </strong>
176
                                </Link>
kusang96's avatar
kusang96 committed
177
178
179
                 방문해주신 <em>{account.name}</em> 님,<br />
                진심으로 환영합니다! 즐거운 쇼핑 되세요.
                </h4>
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
211
212
213
214
215
216
217
218
219
220
                            </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
221
    )
박상호's avatar
박상호 committed
222
}
Jiwon Yoon's avatar
Jiwon Yoon committed
223

kusang96's avatar
0115    
kusang96 committed
224
export default Account