LocCodeChange.js 7.4 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, DropdownButton, Dropdown, ButtonGroup, Collapse, Fade } from 'react-bootstrap';
Spark's avatar
Spark committed
4
import axios from 'axios';
Spark's avatar
Spark committed
5
6
import Swal from 'sweetalert2'

Spark's avatar
Spark committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31

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',
        justifyContent: 'center',
        margin: 'auto',
        padding: '10px'
    }

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

Spark's avatar
Spark committed
38
39
40
41
42
43
44
45
46
47
48
49
50
51
    const [does, setDoes] = useState([])
    const [sggs, setSggs] = useState([])
    const [emds, setEmds] = useState([])
    const [sggsArray, setSggsArray] = useState([])
    const [emdsArray, setEmdsArray] = useState([])

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


    // console.log(doeSelect.options[doeSelect.selectedIndex].text)
    // console.log(sggSelect.options[sggSelect.selectedIndex].text)
    // console.log(emdSelect.options[emdSelect.selectedIndex].text)
Spark's avatar
Spark committed
52
53

    function handleClickLoc() {
Spark's avatar
Spark committed
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
        if (doeSelect.options[doeSelect.selectedIndex].text !== '' && sggSelect.options[sggSelect.selectedIndex].text !== '시군구' && emdSelect.options[emdSelect.selectedIndex].text !== '읍면동') {
            localStorage.setItem('code_doe', doeSelect.value)
            localStorage.setItem('name_doe', doeSelect.options[doeSelect.selectedIndex].text)
            localStorage.setItem('code_sgg', sggSelect.value)
            localStorage.setItem('name_sgg', sggSelect.options[sggSelect.selectedIndex].text)
            localStorage.setItem('code_emd', emdSelect.value)
            localStorage.setItem('name_emd', emdSelect.options[emdSelect.selectedIndex].text)

            if (localStorage.getItem('name_emd')) {
                Swal.fire({
                    title: '변경되었습니다.',
                    text: '축하드립니다!👏',
                    icon: 'success',
                    customClass: 'swal-wide',
                    confirmButtonText: '확인',
                }).then((res) => {
                    if (res.isConfirmed) {
                        window.location.reload()
                    }
                    else {
                        window.location.reload()
                    }
                })
            }
        }
        else {
            Swal.fire({
                title: '실패',
                text: '전부 선택해주세요',
                icon: 'error',
                customClass: 'swal-wide',
                confirmButtonText: '확인'
            })
        }

Spark's avatar
Spark committed
89
90
    }

Spark's avatar
Spark committed
91

Spark's avatar
Spark committed
92

Spark's avatar
Spark committed
93
94
95
96

    async function getLocCode() {
        const res = await axios.get("http://localhost:4500/api/data/loccode")
        const local_codes = res.data.locCodes
Spark's avatar
Spark committed
97

Spark's avatar
Spark committed
98
99
        setDoes(local_codes.DOE)
        setSggs(local_codes.SGG)
Spark's avatar
Spark committed
100
        setEmds(local_codes.EMD)
Spark's avatar
Spark committed
101
    }
Spark's avatar
Spark committed
102

Spark's avatar
Spark committed
103
104
105
    useEffect(() => {
        getLocCode()
    }, [])
Spark's avatar
Spark committed
106

Spark's avatar
Spark committed
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121

    function selectLocal() {
        sggs.map(function (sggvalue) {
            if (doeSelect.value == sggvalue['code_doe']) {
                setSggsArray(sggvalue['sgg'])
            }
        })
        emds.map(function (emdvalue) {
            if (sggSelect.value == emdvalue['code_sgg']) {
                setEmdsArray(emdvalue['emd'])
            }
        })
    }


Spark's avatar
Spark committed
122
123
124
125
126
127
128
    return (
        <Row className='text-center w-100 my-2'>
            <Card style={cardstyled}>
                <Card.Title id='impactTitle'>
                    Local Code
                </Card.Title>
                <Card.Subtitle style={{ fontWeight: 'lighter' }}>
Spark's avatar
Spark committed
129
                    Please select a your region
Spark's avatar
Spark committed
130
131
132
                </Card.Subtitle>
                <hr />
                <Card.Text>
Spark's avatar
Spark committed
133

Spark's avatar
Spark committed
134
                    <Form style={inboxstyled}>
Spark's avatar
Spark committed
135
136
137
138
139
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
                        <Row md={12} xs={12} className='m-auto w-100 d-flex justify-content-center' style={{ padding: '0', display: 'flex', justifyContent: 'center', width: '100%' }}>
                            <Form.Group className='m-auto w-100' style={btnstyled2}>
                                <Row className='m-auto pb-3' onChange={selectLocal}>
                                    <Col md={4} xs={4} style={{ padding: '2px' }}>

                                        <Form.Control as='select' size="sm" id='select-doe'>
                                            <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' }}>
                                        <Form.Control as='select' size="sm" id='select-sgg'>
                                            <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' }}>
                                        <Form.Control as='select' size="sm" id='select-emd'>
                                            <option selected disabled>읍면동</option>
                                            {
                                                emdsArray.map((emdvalue) => (
                                                    <option value={`${emdvalue["code_emd"]}`}>
                                                        {`${emdvalue["name_emd"]}`}
                                                    </option>
                                                ))
                                            }
                                        </Form.Control>
                                    </Col>
                                </Row>
                                <Button
                                    variant='light' style={btnstyled2} onClick={handleClickLoc}>
                                    확인
                                </Button>
                            </Form.Group>
Spark's avatar
Spark committed
183
184
185
186
187
188
189
190
191
192
193
                        </Row>
                    </Form>

                </Card.Text>
            </Card>

        </Row>
    )
}

export default LocCodeChange;