RoomMake.js 4.4 KB
Newer Older
Choi Ga Young's avatar
Choi Ga Young committed
1
import React, { useState } from 'react'
Soo Hyun Kim's avatar
Soo Hyun Kim committed
2
3
4
5
6
7
8
9
10
11
12
13
14
import axios from 'axios';
import { Row, Col, Modal, Button, Form, Alert } from 'react-bootstrap';
import catchErrors from '../utils/catchErrors';

const INIT_ROOM = {
    roomName: '',
    interest: '',
    isOpen: false
}

function RoomMake(props) {
    const [room, setRoom] = useState(INIT_ROOM);
    const [error, setError] = useState('');
15
    const [roomCode, setRoomCode] = useState('')
Soo Hyun Kim's avatar
Soo Hyun Kim committed
16

Choi Ga Young's avatar
에러    
Choi Ga Young committed
17
    const member = sessionStorage.getItem('userId');
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
    const realTime = new Date().toISOString()

    async function recordEntryLog(Id) {
        const leaveInfo = { userId: member, roomCode: Id, leaveTime: realTime }
        try {
            const check = await axios.get('/room/entrylog', { params: leaveInfo })
            if (check.data) {       //있으면 put으로
                await axios.put('/room/entrylog', leaveInfo)
            } else {        //없으면 post
                await axios.post('/room/entrylog', leaveInfo)
            }
        } catch (error) {
            catchErrors(error, setError)
        }

    }
Soo Hyun Kim's avatar
Soo Hyun Kim committed
34
35
    function handleChange(event) {
        const { name, value } = event.target
Choi Ga Young's avatar
Choi Ga Young committed
36
        setRoom({ ...room, [name]: value, member })
Choi Ga Young's avatar
xxxxx    
Choi Ga Young committed
37
        // console.log(room)
Soo Hyun Kim's avatar
Soo Hyun Kim committed
38
39
40
41
42
43
    }

    async function handleSubmit(event) {
        event.preventDefault()
        try {
            setError('')
Choi Ga Young's avatar
에러    
Choi Ga Young committed
44
            let res = await axios.post('/room/makeRoom', room)
45
46
47
            const Id = res.data.roomId
            alert(`방암호는 ${Id}입니다`)
            props.handleCloseModal()
Soo Hyun Kim's avatar
Soo Hyun Kim committed
48
            setRoom(INIT_ROOM)
49
50
            // setRoomCode(Id)
            recordEntryLog(Id)
Choi Ga Young's avatar
Choi Ga Young committed
51
        } catch (error) {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
52
53
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
89
90
91
92
93
94
95
96
97
98
99
100
101
            catchErrors(error, setError)
        }
    }

    return (
        <Modal show={props.showModal} onHide={props.handleCloseModal}>
            <Modal.Header closeButton>
                <Modal.Title> 생성</Modal.Title>
            </Modal.Header>
            {error && <Alert variant='danger'>
                {error}
            </Alert>}
            <Modal.Body>
                <Form onSubmit={handleSubmit}>
                    <Form.Group as={Row} controlId="chatName">
                        <Form.Label column sm={4}> 이름</Form.Label>
                        <Col>
                            <Form.Control name='roomName' type='text' value={room.roomName} onChange={handleChange} />
                        </Col>
                    </Form.Group>
                    <Form.Group as={Row} controlId="chatInterest">
                        <Form.Label column sm={4}>관심 분야</Form.Label>
                        <Col>
                            <Form.Control as="select" defaultValue="Choose..." name='interest' type='text' value={room.interest} onChange={handleChange}>
                                <option>Choose...</option>
                                <option>과학</option>
                                <option>수학</option>
                                <option>예술</option>
                                <option>언어</option>
                                <option>취미</option>
                            </Form.Control>
                        </Col>
                    </Form.Group>
                    <Form.Group as={Row} controlId="chatIsOpen">
                        <Form.Label column sm={4}>공개방</Form.Label>
                        <Col>
                            <Form.Check
                                type="checkbox"
                                checked={room.isOpen}
                                name='isOpen'
                                onChange={() => setRoom({ ...room, isOpen: !room.isOpen })} />
                        </Col>
                    </Form.Group>
                    {
                        (room.isOpen)
                            ? (<p><b>공개방</b>으로 개설되어 공개방 목록에 공개되며, 코드를 공유하여 참가할 수도 있습니다.</p>)
                            : (<p><b>비밀방</b>으로 개설되며, 참여자들에게 코드를 공유해야합니다.</p>)
                    }
                    <Form.Group as={Row}>
                        <Col sm={{ span: 5, offset: 4 }}>
102
                            <Button type="submit" style={{ backgroundColor: "#9174ad", borderColor: "#9174ad" }} > 생성</Button>
Soo Hyun Kim's avatar
Soo Hyun Kim committed
103
104
105
106
107
108
109
110
111
112
                        </Col>
                    </Form.Group>
                </Form>
            </Modal.Body>
        </Modal>

    )
}

export default RoomMake