Account.js 7.79 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
4
5
6
import { Link } from 'react-router-dom';
import axios from 'axios'
import catchErrors from '../utils/catchErrors';
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

박상호's avatar
박상호 committed
13

Jiwon Yoon's avatar
Jiwon Yoon committed
14
function Account() {
15
16
17
18
19
20
    const [account, setAccount] = useState(INIT_ACCOUNT)
    const [error, setError] = useState("")
    const userId = isAuthenticated()


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

    useEffect(() => {
        getUsername(userId)
    }, [userId])

박상호's avatar
박상호 committed
36
    const [show, setShow] = useState(false);
37

박상호's avatar
박상호 committed
38
    const handleClose = () => setShow(false)
39

박상호's avatar
박상호 committed
40
    const handleShow = () => setShow(true)
41
42


박상호's avatar
박상호 committed
43
44
45
46
47
48
49
50
51
52
53
54
    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 })
        }
    }
55

박상호's avatar
박상호 committed
56
57
58
59
60
61
62
63
64
65
66
67
68
69
    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) {
            catchErrors(error, setError)
        }
        setShow(false)
    }
70
71


박상호's avatar
박상호 committed
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
    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) {
                catchErrors(error, setError)
            }
        } else {
            alert("파일을 선택해주세요.")
박상호's avatar
123    
박상호 committed
88
        }
박상호's avatar
박상호 committed
89
    }
90

박상호's avatar
박상호 committed
91
    return (
92
93
94
95
96

        <Container className="px-3">
            <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
97
98
99
                    <Col md={5} className="d-flex align-content-center justify-content-center">
                        <Button variant="outline-light" onClick={handleShow}>
                            {account.avatarUrl ? (
박상호's avatar
213    
박상호 committed
100
                                <Image src={account.avatarUrl && `/images/${account.avatarUrl}`} className="img-thumbnail"
박상호's avatar
박상호 committed
101
102
                                    roundedCircle style={{ objectFit: "cover", width: "10rem", height: "10rem" }} />
                            ) : (
kusang96's avatar
0115    
kusang96 committed
103
                                    <Image src="/icon/person.svg" className="img-thumbnail"
박상호's avatar
박상호 committed
104
105
                                        roundedCircle style={{ objectFit: "cover", width: "10rem", height: "10rem" }} />
                                )}
106
                        </Button>
박상호's avatar
박상호 committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
                        <Modal show={show} onHide={handleClose}>
                            <Modal.Header closeButton>
                                <Modal.Title>이미지를 변경하시겠습니까?</Modal.Title>
                            </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>
                                    <Button variant="secondary" onClick={handleClose}>취소</Button>
                                    <Button variant="primary" type="submit" onClick={handleClose}>저장</Button>
                                </Modal.Footer>
                            </Form>
                        </Modal>
126
127
128
129
130
131
132
133
134
135
136
137
138
                    </Col>
                    <Col >
                        <Row className="mt-4 text-center">
                            <Col>
                                <h2>
                                    <strong>{account.name}</strong> <small>({account.id}){" "}님</small>
                                </h2>
                            </Col>
                        </Row>
                        <Row className="px-3">
                            <Card.Body className="p-2 text-center">
                                <h4><Link to="/" class="link-warning">
                                    <strong title="홈으로">
kusang96's avatar
0115    
kusang96 committed
139
                                        <Image src="/icon/mypagetiger.svg" width={"30rem"} roundedCircle className="img-thumbnail" >
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
                                        </Image>KU#
                  </strong>
                                </Link>
                                    {/* 홈페이지로 돌아가기 */}
                 방문해주신 <em>{account.name}</em> 님,<br></br>
                진심으로 환영합니다! 즐거운 쇼핑 되세요.</h4>
                            </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
187
    )
박상호's avatar
박상호 committed
188
}
Jiwon Yoon's avatar
Jiwon Yoon committed
189

kusang96's avatar
0115    
kusang96 committed
190
export default Account