HomePage.js 7.21 KB
Newer Older
Soo Hyun Kim's avatar
Soo Hyun Kim committed
1
2
import React, { useState, useEffect } from 'react';
import { Row, Col, Modal, Button, Form } from 'react-bootstrap';
우지원's avatar
ul    
우지원 committed
3
4
5
6
7
import Tabs from 'react-bootstrap/Tabs';
import Tab from 'react-bootstrap/Tab';
import ClosedList from '../Components/ClosedList';
import OpenList from '../Components/OpenList';
import Chat from '../Components/Chat';
Choi Ga Young's avatar
Choi Ga Young committed
8
import Menu from '../Components/Menu';
우지원's avatar
ul    
우지원 committed
9
10
11
12
13
14
// import styled from 'styled-components';

// const List = styled.div`
//   background: #FFFAFA;
// `

Soo Hyun Kim's avatar
Soo Hyun Kim committed
15
16
17
18
19
const INIT_CHATR = {
    name: '',
    interest: '',
    isOpen: false
}
우지원's avatar
ul    
우지원 committed
20
21

function Home() {
Soo Hyun Kim's avatar
Soo Hyun Kim committed
22
23
24
25
26
27
28
29
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
    const [show, setShow] = useState(false);
    const [show2, setShow2] = useState(false);
    const [chat, setChat] = useState(false);
    const [checkedI, setCheckedI] = useState(false);
    const [chatR, setChatR] = useState(INIT_CHATR);
    const [disabled, setDisabled] = useState(true);

    const handleClose = () => setShow(false);
    const handleShow = () => setShow(true);
    const handleChato = () => setChat(true);
    const handleChatc = () => setChat(false);
    const handleClose2 = () => setShow2(false);
    const handleShow2 = () => setShow2(true);
    // variant="pills"

    useEffect(() => {
        const isChatR = Object.values(chatR).every(el => Boolean(el))
        isChatR ? setDisabled(false) : setDisabled(true)
    }, [chatR])

    function handleChange(event){
        const {name, value} = event.target
        setChatR({...chatR, [name]:value})
        console.log(chatR)
    }

    async function handleSubmit(event){
        event.preventDefault()
        const response = await fetch('chat/makeChat', {
            method: 'POST',
            headers: {
                'Content-Type': 'application/json'
            },
            body: JSON.stringify(chatR)
        })
        const data = await response.json()
        console.log(data)
        setChatR(INIT_CHATR)
    }

    return (
        <>
            <Menu />
            <Row className="mr-0">
                <Col className="list" md={5}>
                    <Tabs defaultActiveKey="closed" id="uncontrolled-tab-example">
                        <Tab eventKey="closed" title="내 채팅" onClick={handleChato} >
                            <ClosedList />
                        </Tab>
                        <Tab eventKey="open" title="공개방" >
                            <OpenList />
                        </Tab>
                    </Tabs>
                </Col>
                <Col style={{ padding: "0" }}>
                    {chat ? <Chat handleChatc={handleChatc} /> : null}
우지원's avatar
ul    
우지원 committed
78

Soo Hyun Kim's avatar
Soo Hyun Kim committed
79
80
81
82
                    <div style={{ position: "fixed", bottom: "20px", right: "30px" }}>
                        <Button variant="primary" onClick={handleShow} size="lg" block>
                            생성
                        </Button>
우지원's avatar
ul    
우지원 committed
83

Soo Hyun Kim's avatar
Soo Hyun Kim committed
84
85
86
87
                        <Button variant="secondary" onClick={handleShow2} size="lg" block>
                            참가
                        </Button>
                    </div>
우지원's avatar
ul    
우지원 committed
88

Soo Hyun Kim's avatar
Soo Hyun Kim committed
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
                    <Modal show={show} onHide={handleClose}>
                        <Modal.Header closeButton>
                            <Modal.Title> 생성</Modal.Title>
                        </Modal.Header>
                        <Modal.Body>
                            <Form onSubmit={handleSubmit}>
                                <Form.Group as={Row} controlId="chatName">
                                    <Form.Label column sm={4}> 이름</Form.Label>
                                    <Col>
                                        <Form.Control name='name' type='text' value={chatR.name} 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={chatR.interest} onChange={handleChange}>
                                            <option>Choose...</option>
                                            <option>과학</option>
                                            <option>수학</option>
                                            <option>예술</option>
                                            <option>언어</option>
                                            <option>취미</option>
                                        </Form.Control>
                                        {/* <Form.Control type="text" /> */}
                                    </Col>
                                </Form.Group>
                                <Form.Group as={Row} controlId="chatIsOpen">
                                    <Form.Label column sm={4}>공개방</Form.Label>
                                    <Col>
                                        <Form.Check
                                            type="checkbox"
                                            checked={chatR.isOpen}
                                            name='isOpen'
                                            onChange={() => setChatR({...chatR, isOpen: !chatR.isOpen})}/>
                                    </Col>
                                </Form.Group>
                                {
                                    (chatR.isOpen)
                                        ? (<p><b>공개방</b>으로 개설되어 공개방 목록에 공개되며, 코드를 공유하여 참가할 수도 있습니다.</p>)
                                        : (<p><b>비밀방</b>으로 개설되며, 참여자들에게 코드를 공유해야합니다.</p>)
                                }
                                <Form.Group as={Row}>
                                    {console.log(chatR)}
                                    <Col sm={{ span: 5, offset: 4 }}>
                                        <Button type="submit" > 생성</Button>
                                    </Col>
                                </Form.Group>
                            </Form>
                        </Modal.Body>
                    </Modal>
우지원's avatar
ul    
우지원 committed
139

Soo Hyun Kim's avatar
Soo Hyun Kim committed
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
                    <Modal show={show2} onHide={handleClose2}>
                    <Modal.Header closeButton>
                        <Modal.Title>참여 코드로 채팅 참가</Modal.Title>
                    </Modal.Header>
                    <Modal.Body>
                        <Form onSubmit={() => { console.log('제출') }}>
                            <Form.Group as={Row} controlId="formCodeE">
                                <Form.Label column sm={4}>참여 코드</Form.Label>
                                <Col>
                                    <Form.Control type="text" />
                                </Col>
                            </Form.Group>
                            <Form.Group as={Row}>
                                <Col sm={{ span: 5, offset: 4 }}>
                                    <Button type="submit">참가</Button>
                                </Col>
                            </Form.Group>
                        </Form>
                    </Modal.Body>
                </Modal>
                </Col>
            </Row>
        </>
    );
우지원's avatar
ul    
우지원 committed
164
165
166
}

export default Home;