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

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

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

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

    useEffect(() => {
        getUsername(userId)
이재연's avatar
이재연 committed
34
        getOrdered()
35
36
    }, [userId])

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

kusang96's avatar
최종    
kusang96 committed
46
    const handleBasic = async () => {
박상호's avatar
박상호 committed
47
48
49
50
51
52
53
54
        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
55
            catchError(error, setError)
박상호's avatar
박상호 committed
56
57
58
        }
        setShow(false)
    }
59
60


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

박상호's avatar
12    
박상호 committed
80
    async function getOrdered() {
81
        try {
kusang96's avatar
kusang96 committed
82
83
84
85
            setError('')
            const response = await axios.post(`/api/users/addorder`, { userId: userId })
            const res = response.data
            setOrdered(res)
86
87
88
89
90
        } catch (error) {
            catchError(error, setError)
        }
    }

박상호's avatar
박상호 committed
91
    return (
92
        <Container className="px-3">
kusang96's avatar
kusang96 committed
93
94
            <style type="text/css">
                {`
박상호's avatar
css    
박상호 committed
95
96
97
98
99
100
101
                @font-face {
                    font-family: 'Jal_Onuel';
                    src: url('https://cdn.jsdelivr.net/gh/projectnoonnu/noonfonts_20-10-21@1.0/Jal_Onuel.woff') format('woff');
                    font-weight: normal;
                    font-style: normal;
                }
                body{font-family:'Jal_Onuel'}
kusang96's avatar
kusang96 committed
102
103
104
105
106
107
                a, a:hover, a:active {
                    color: #91877F;
                    text-decoration-color: #91877F;
                }
                `}
            </style>
108
109
110
            <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
111
                    <Col md={5} className="d-flex align-content-center justify-content-center">
kusang96's avatar
kusang96 committed
112
                        <Button variant="outline-light" onClick={() => setShow(true)}>
박상호's avatar
박상호 committed
113
                            {account.avatarUrl ? (
kusang96's avatar
kusang96 committed
114
                                <Image src={account.avatarUrl && `/images/${account.avatarUrl}`} className="img-thumbnail"
박상호's avatar
박상호 committed
115
116
                                    roundedCircle style={{ objectFit: "cover", width: "10rem", height: "10rem" }} />
                            ) : (
kusang96's avatar
0115    
kusang96 committed
117
                                    <Image src="/icon/person.svg" className="img-thumbnail"
박상호's avatar
박상호 committed
118
119
                                        roundedCircle style={{ objectFit: "cover", width: "10rem", height: "10rem" }} />
                                )}
120
                        </Button>
kusang96's avatar
kusang96 committed
121
                        <Modal show={show} onHide={() => setShow(false)}>
122
                            <Modal.Header closeButton style={{ background: "#F7F3F3" }}>
박상호's avatar
0120    
박상호 committed
123
                                <Modal.Title >이미지를 변경하시겠습니까?</Modal.Title>
박상호's avatar
박상호 committed
124
125
126
127
128
129
130
                            </Modal.Header>
                            <Form onSubmit={handleSubmit}>
                                <Modal.Body>
                                    <Form.Control type="file" name="avatar" onChange={handleChange} />
                                </Modal.Body>
                                <Modal.Footer>
                                    <Col className="px-0">
kusang96's avatar
kusang96 committed
131
                                        <Button variant="outline-secondary" onClick={handleBasic} className="d-flex justify-content-start"><small>기본이미지로</small></Button>
박상호's avatar
박상호 committed
132
                                    </Col>
kusang96's avatar
kusang96 committed
133
134
                                    <Button variant="secondary" onClick={() => setShow(false)}>취소</Button>
                                    <Button variant="primary" type="submit" onClick={() => setShow(false)}>저장</Button>
박상호's avatar
박상호 committed
135
136
137
                                </Modal.Footer>
                            </Form>
                        </Modal>
138
139
140
141
142
                    </Col>
                    <Col >
                        <Row className="mt-4 text-center">
                            <Col>
                                <h2>
박상호's avatar
0120    
박상호 committed
143
144
145
146
147
148
149
                                    <strong title='회원정보' style={{ cursor: "pointer", textDecoration: 'underline' }} onClick={() => setProshow(true)}>
                                        {account.name}
                                    </strong>
                                    <Modal
                                        size="sm"
                                        show={proshow}
                                        onHide={() => setProshow(false)}>
150
                                        <Modal.Header closeButton style={{ background: "#F7F3F3" }}>
박상호's avatar
0120    
박상호 committed
151
152
153
154
155
156
157
158
159
160
161
162
163
                                            <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>
164
165
166
167
168
                                </h2>
                            </Col>
                        </Row>
                        <Row className="px-3">
                            <Card.Body className="p-2 text-center">
Kim, Subin's avatar
Kim, Subin committed
169
                                <h4><Link to="/" style={{ textDecoration: "none" }}>
170
                                    <strong title="홈으로">
kusang96's avatar
0115    
kusang96 committed
171
                                        <Image src="/icon/mypagetiger.svg" width={"30rem"} roundedCircle className="img-thumbnail" >
172
                                        </Image>KU#
kusang96's avatar
kusang96 committed
173
                                    </strong>
174
                                </Link>
kusang96's avatar
kusang96 committed
175
176
177
                 방문해주신 <em>{account.name}</em> 님,<br />
                진심으로 환영합니다! 즐거운 쇼핑 되세요.
                </h4>
178
179
180
181
182
183
184
185
186
187
                            </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>
박상호's avatar
good    
박상호 committed
188
            <div className='m-2 mb-5'>
kusang96's avatar
card    
kusang96 committed
189
                <ListCard ordered={ordered} status={'order'} />
kusang96's avatar
kusang96 committed
190
            </div>
Kim, Subin's avatar
정리    
Kim, Subin committed
191
        </Container>
Jiwon Yoon's avatar
Jiwon Yoon committed
192
    )
박상호's avatar
박상호 committed
193
}
Jiwon Yoon's avatar
Jiwon Yoon committed
194

kusang96's avatar
0115    
kusang96 committed
195
export default Account