LocCodeChange.js 7.54 KB
Newer Older
Spark's avatar
Spark committed
1
import React, { useEffect, useState } from 'react'
Spark's avatar
Spark committed
2
import '../App.css'
Spark's avatar
Spark committed
3
import { Form, Button, Row, Col, Card } from 'react-bootstrap';
Spark's avatar
Spark committed
4
import axios from 'axios';
Spark's avatar
Spark committed
5
import Swal from 'sweetalert2'
6
import { callUserInfo } from '../utils/CheckDB';
Spark's avatar
Spark committed
7
import { routesClient } from '../routesClient';
Spark's avatar
Spark committed
8

Spark's avatar
Spark committed
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26

function LocCodeChange() {

    const cardstyled = {
        margin: 'auto',
        padding: '1em',
        display: 'flex',
        justifyContent: 'center',
        width: '100%',
        borderWidth: '3px',
        borderRadius: '20px',
        borderColor: 'rgb(110, 189, 142)',
        color: '#04AB70'
    }

    const inboxstyled = {
        display: 'flex',
        flexDirection: 'column',
27
        maxWidth: '90%',
Spark's avatar
Spark committed
28
29
        justifyContent: 'center',
        margin: 'auto',
Spark's avatar
Spark committed
30
31
        padding: '0.5em',
        color: 'black'
Spark's avatar
Spark committed
32
33
34
35
    }

    const btnstyled2 = {
        background: 'white',
Spark's avatar
Spark committed
36
        width: '50%',
Spark's avatar
Spark committed
37
38
39
40
41
        borderWidth: '2px',
        color: 'rgb(110, 189, 142)',
        borderColor: 'rgba(195, 195, 195, 0.753)',
    }

42
43
44
45
46
    const [does, setDoes] = useState([]) // DOE
    const [sggs, setSggs] = useState([]) // SGG
    const [emds, setEmds] = useState([]) // EMD
    const [sggsArray, setSggsArray] = useState([]) // DOE 와 SGG 내의 code_doe 같을 때 => sgg 값
    const [emdsArray, setEmdsArray] = useState([]) // SGG 과 EMD 내의 code_sgg && DOE 와 EMD 내의 code_doe => emd 값
Spark's avatar
Spark committed
47
48
49
50
51

    const doeSelect = document.getElementById('select-doe')
    const sggSelect = document.getElementById('select-sgg')
    const emdSelect = document.getElementById('select-emd')

52
53
54
    // Local code 받아오기
    useEffect(() => {
        async function getLocCode() {
Spark's avatar
Spark committed
55
            const res = await axios.get(routesClient.localdata)
56
            const local_codes = res.data.contents.loc_code
57

58
59
60
            setDoes(local_codes.DOE)
            setSggs(local_codes.SGG)
            setEmds(local_codes.EMD)
Spark's avatar
Spark committed
61
        }
62

Spark's avatar
Spark committed
63
64
        getLocCode()
    }, [])
Spark's avatar
Spark committed
65

Spark's avatar
Spark committed
66
67
    function selectLocal() {
        sggs.map(function (sggvalue) {
68
            if (Number(doeSelect.value) === sggvalue['code_doe']) {
Spark's avatar
Spark committed
69
70
71
72
                setSggsArray(sggvalue['sgg'])
            }
        })
        emds.map(function (emdvalue) {
73
74
            if ((Number(sggSelect.value) === emdvalue['code_sgg'])
                && (Number(doeSelect.value) === emdvalue['code_doe'])) {
Spark's avatar
Spark committed
75
76
77
78
79
80
                setEmdsArray(emdvalue['emd'])
            }
        })
    }


81
82
83
84
85
86
87
88
    async function handleSubmit(event) {
        event.preventDefault()
        if (doeSelect.options[doeSelect.selectedIndex].text !== ''
            && sggSelect.options[sggSelect.selectedIndex].text !== '시군구'
            && emdSelect.options[emdSelect.selectedIndex].text !== '읍면동') {

            const saveCodeEmd = emdSelect.value

Spark's avatar
Spark committed
89
            await axios.post(routesClient.edit, { loc_code: saveCodeEmd }) // loccal code 수정
90
91
92
93
94
95
96
97
98
99
100
101

            callUserInfo().then((res) => {
                console.log('loc_code', res[0].loc_code)
                if (res[0].loc_code) {
                    Swal.fire({
                        title: '변경되었습니다.',
                        text: '축하드립니다!👏',
                        icon: 'success',
                        customClass: 'swal-wide',
                        confirmButtonText: '확인',
                    }).then((res) => {
                        if (res.isConfirmed) {
102
                            window.location.replace('/')
103
104
                        }
                        else {
105
                            window.location.replace('/')
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
                        }
                    })
                }
            })


        }
        else {
            Swal.fire({
                title: '실패',
                text: '전부 선택해주세요',
                icon: 'error',
                customClass: 'swal-wide',
                confirmButtonText: '확인'
            })
        }

    }




Spark's avatar
Spark committed
128

Spark's avatar
Spark committed
129
130
131
132
    return (
        <Row className='text-center w-100 my-2'>
            <Card style={cardstyled}>
                <Card.Title id='impactTitle'>
Spark's avatar
Spark committed
133
                    지역 코드
Spark's avatar
Spark committed
134
135
                </Card.Title>
                <Card.Subtitle style={{ fontWeight: 'lighter' }}>
Spark's avatar
Spark committed
136
                    본인의 지역을 선택해주세요
Spark's avatar
Spark committed
137
138
                </Card.Subtitle>
                <hr />
Spark's avatar
Spark committed
139
                <Card.Text className='m-0'>
140
                    <Form style={inboxstyled} onSubmit={handleSubmit}>
Spark's avatar
Spark committed
141
                        <Row md={12} xs={12} className='m-auto w-100 d-flex justify-content-center' style={{ padding: '0', display: 'flex', justifyContent: 'center', width: '100%' }}>
142
                            <Form.Group className='m-auto w-100' style={btnstyled2} onChange={selectLocal}>
143
                                <Row className='m-auto pb-3'>
Spark's avatar
Spark committed
144
145
                                    <Col md={4} xs={4} style={{ padding: '2px' }}>

146
                                        <Form.Control as='select' size="sm" id='select-doe' required>
Spark's avatar
Spark committed
147
148
149
150
151
152
153
154
155
156
157
158
                                            <option selected disabled></option>
                                            {
                                                does.map((doevalue) => (
                                                    <option value={`${doevalue["code_doe"]}`}>
                                                        {`${doevalue["name_doe"]}`}
                                                    </option>
                                                ))
                                            }
                                        </Form.Control>
                                    </Col>

                                    <Col md={4} xs={4} style={{ padding: '2px' }}>
159
                                        <Form.Control as='select' size="sm" id='select-sgg' required>
Spark's avatar
Spark committed
160
161
162
163
164
165
166
167
168
169
170
171
                                            <option selected disabled>시군구</option>
                                            {
                                                sggsArray.map((sggvalue) => (
                                                    <option value={`${sggvalue["code_sgg"]}`}>
                                                        {`${sggvalue["name_sgg"]}`}
                                                    </option>
                                                ))
                                            }
                                        </Form.Control>
                                    </Col>

                                    <Col md={4} xs={4} style={{ padding: '2px' }}>
172
                                        <Form.Control as='select' size="sm" id='select-emd' required>
Spark's avatar
Spark committed
173
174
175
176
177
178
179
180
181
182
183
184
                                            <option selected disabled>읍면동</option>
                                            {
                                                emdsArray.map((emdvalue) => (
                                                    <option value={`${emdvalue["code_emd"]}`}>
                                                        {`${emdvalue["name_emd"]}`}
                                                    </option>
                                                ))
                                            }
                                        </Form.Control>
                                    </Col>
                                </Row>
                            </Form.Group>
185
186
187
188
                            <Button
                                variant='light' style={btnstyled2} type='submit'>
                                확인
                            </Button>
Spark's avatar
Spark committed
189
190
191
192
193
194
195
196
197
                        </Row>
                    </Form>
                </Card.Text>
            </Card>
        </Row>
    )
}

export default LocCodeChange;