EnterRoom.js 2.13 KB
Newer Older
Soo Hyun Kim's avatar
Soo Hyun Kim committed
1
2
import React, { useState } from 'react'
import axios from 'axios';
3
import { Row, Col, Modal, Button, Form } from 'react-bootstrap';
Soo Hyun Kim's avatar
Soo Hyun Kim committed
4
5
6
7
8
9
import catchErrors from '../utils/catchErrors'


function EnterRoom(props) {
    const [enterCode, setEnterCode] = useState('');
    const [error, setError] = useState('');
Soo Hyun Kim's avatar
Soo Hyun Kim committed
10
11
12

    const userId = sessionStorage.getItem('userId'); //sessionStorage에 저장된 userId가져옴
    
Soo Hyun Kim's avatar
Soo Hyun Kim committed
13
14
15
16
17
18
19
20
21
22
23
    
    function handleChange(event) {
        const { name, value } = event.target
        setEnterCode(value)
        console.log(enterCode)
    }
    
    async function handleSubmit(event) {
        event.preventDefault()
        try {
            setError('')
Soo Hyun Kim's avatar
Soo Hyun Kim committed
24
            let res = await axios.post('/room/enterRoom', { enterCode })
Soo Hyun Kim's avatar
Soo Hyun Kim committed
25
            await axios.put('/room/member', { userId: userId, roomId: enterCode })
Soo Hyun Kim's avatar
Soo Hyun Kim committed
26
            props.setRoomName(res.data)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
27
            props.enterChatRoom(enterCode)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
28
29
            props.handleCloseEnter()
            props.handleChato()
Soo Hyun Kim's avatar
Soo Hyun Kim committed
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
            setEnterCode('')
        } catch (error){
            catchErrors(error, setError)
        }
    }

    return (
        <>
            <Modal show={props.showEnter} onHide={props.handleCloseEnter}>
                <Modal.Header closeButton>
                    <Modal.Title>참여 코드로 채팅 참가</Modal.Title>
                </Modal.Header>
                <Modal.Body>
                    <Form onSubmit={handleSubmit}>
                        <Form.Group as={Row} controlId="formCodeE">
                            <Form.Label column sm={4}>참여 코드</Form.Label>
                            <Col>
                                <Form.Control name='roomCode' type='text' value={enterCode} onChange={handleChange} />
                            </Col>
                        </Form.Group>
                        <Form.Group as={Row}>
                            <Col sm={{ span: 5, offset: 4 }}>
                                <Button type="submit">참가</Button>
                            </Col>
                        </Form.Group>
                    </Form>
                </Modal.Body>
            </Modal>
        </>
    )
}

export default EnterRoom