Commit 9e22fd14 authored by Ha YeaJin's avatar Ha YeaJin
Browse files

행복...하자....

parent 6031e83e
import React, { useState, useEffect } from 'react'; import React, { useState, useEffect } from 'react';
import { Link, Redirect } from 'react-router-dom'; import { Link, Redirect } from 'react-router-dom';
import styled from 'styled-components'; import styled from 'styled-components';
import { Navbar, Nav, NavDropdown, NavLink } from 'react-bootstrap'; import { Navbar, Nav, NavLink } from 'react-bootstrap';
import axios from 'axios';
const MENU = styled(Navbar)` const MENU = styled(Navbar)`
background-color: #7B031D; background-color: #7B031D;
...@@ -13,6 +14,7 @@ const MENU = styled(Navbar)` ...@@ -13,6 +14,7 @@ const MENU = styled(Navbar)`
function Menu() { function Menu() {
const [state, setState] = useState() const [state, setState] = useState()
const [user, setUser] = useState({ role: "" })
const name = localStorage.getItem('name'); const name = localStorage.getItem('name');
function logout() { function logout() {
...@@ -21,6 +23,21 @@ function Menu() { ...@@ -21,6 +23,21 @@ function Menu() {
setState(true); setState(true);
} }
useEffect(() => {
acheck();
}, [])
function acheck() {
axios.get(`/users/${localStorage.getItem('_id')}`)
.then(res => {
if (res.data.role == "admin") {
setUser(res.data)
}
}).catch(err => {
alert(err.error)
});
}
if (state) return <Redirect to="/" /> if (state) return <Redirect to="/" />
return ( return (
<MENU expand="md" variant="dark"> <MENU expand="md" variant="dark">
...@@ -42,11 +59,12 @@ function Menu() { ...@@ -42,11 +59,12 @@ function Menu() {
}} className="nav-link"> }} className="nav-link">
대관 확인/취소</NavLink> 대관 확인/취소</NavLink>
<NavLink as={Link} to={{ {user.role === "admin" ? (
pathname: `/acheck/${localStorage.getItem('_id')}`, <NavLink as={Link} to={{
state: { id: localStorage.getItem('_id') }, pathname: `/acheck/${localStorage.getItem('_id')}`,
}} className="nav-link"> state: { id: localStorage.getItem('_id') },
대관 확인/취소(관리자)</NavLink> }} className="nav-link">
대관 확인/취소(관리자)</NavLink>) : null}
</Nav> </Nav>
<Nav > <Nav >
<NavLink> <NavLink>
......
...@@ -8,12 +8,12 @@ function ACheck(props) { ...@@ -8,12 +8,12 @@ function ACheck(props) {
const [state, setState] = useState() const [state, setState] = useState()
const [reserve, setReserve] = useState([]); const [reserve, setReserve] = useState([]);
useEffect(() => { useEffect(() => {
getReserve(); getReserve();
}, []) }, [])
function getReserve() { function getReserve() {
axios.get(`/users/admin/${props.match.params.id}`, { axios.get(`/users/admin/${props.match.params.id}`, {
headers: { authorization: localStorage.getItem('token') }, headers: { authorization: localStorage.getItem('token') },
}) })
.then(res => { .then(res => {
if (res.status === 404) { if (res.status === 404) {
...@@ -30,10 +30,19 @@ function ACheck(props) { ...@@ -30,10 +30,19 @@ function ACheck(props) {
if (state) return <Redirect to="/home" />; if (state) return <Redirect to="/home" />;
function remove(index) { function remove(index) {
axios.delete(`/reserves/${reserve[index]._id}`) // axios.delete(`/reserves/${reserve[index]._id}`)
// .then(res => {
// if (res.status === 404) return alert(res.data.error)
// alert("삭제되었습니다!");
// getReserve();
// })
// .catch(err => {
// alert(err.error)
// });
axios.put(`/reserves/${reserve[index]._id}`)
.then(res => { .then(res => {
if (res.status === 404) return alert(res.data.error) if (res.status === 404) return alert(res.data.error)
alert("삭제되었습니다!"); alert("승인을 거절했습니다!");
getReserve(); getReserve();
}) })
.catch(err => { .catch(err => {
...@@ -42,7 +51,9 @@ function ACheck(props) { ...@@ -42,7 +51,9 @@ function ACheck(props) {
}; };
function admit(index) { function admit(index) {
axios.put(`/reserves/${reserve[index]._id}`) axios.put(`/reserves/${reserve[index]._id}`, {
approve: true,
})
.then(res => { .then(res => {
if (res.status === 404) return alert(res.data.error) if (res.status === 404) return alert(res.data.error)
alert("승인되었습니다!"); alert("승인되었습니다!");
...@@ -69,25 +80,26 @@ function ACheck(props) { ...@@ -69,25 +80,26 @@ function ACheck(props) {
</tr> </tr>
</thead> </thead>
<tbody> <tbody>
{reserve.map((reserve, index) => { {reserve != "" ? (
return ( reserve.map((reserve, index) => {
<tr key={index}> return (
<td>{reserve.user.name}</td> <tr key={index}>
<td>{reserve.date}</td> <td>{reserve.user.name}</td>
<td>{reserve.time}</td> <td>{reserve.date}</td>
<td>{reserve.room}</td> <td>{reserve.starttime}~{(Number(reserve.starttime) + reserve.usetime)}</td>
<td>{reserve.num}</td> <td>{reserve.room}</td>
<td> <td>{reserve.num}</td>
<button onClick={() => admit(index)} className="btn btn-primary"> <td>
승인 <button onClick={() => admit(index)} className="btn btn-primary">
승인
</button> </button>
<button onClick={() => remove(index)} className="btn btn-danger"> <button onClick={() => remove(index)} className="btn btn-danger">
거절 거절
</button> </button>
</td> </td>
</tr> </tr>
) )
})} })) : <div>최근 대관 신청 내역이 없습니다.</div>}
</tbody> </tbody>
</table> </table>
...@@ -96,4 +108,4 @@ function ACheck(props) { ...@@ -96,4 +108,4 @@ function ACheck(props) {
) )
} }
export default ACheck export default ACheck
\ No newline at end of file
...@@ -22,27 +22,26 @@ function Apply(props) { ...@@ -22,27 +22,26 @@ function Apply(props) {
} }
function time(starttime) { function time(starttime) {
console.log(starttime) if (starttime == 21) {
if (starttime == 21) { return (<Field as="select" name="usetime">
return (<Field as="select" name="usetime"> <option value="">이용시간을 선택하세요</option>
<option value="">이용시간을 선택하세요</option> <option value="1">1시간</option>
<option value="1">1시간</option> </Field>)
</Field>) }
} if (starttime == 20) {
if (starttime == 20) {
return (<Field as="select" name="usetime">
<option value="">이용시간을 선택하세요</option>
<option value="1">1시간</option>
<option value="2">2시간</option>
</Field>)
}
return (<Field as="select" name="usetime"> return (<Field as="select" name="usetime">
<option value="">이용시간을 선택하세요</option> <option value="">이용시간을 선택하세요</option>
<option value="1">1시간</option> <option value="1">1시간</option>
<option value="2">2시간</option> <option value="2">2시간</option>
<option value="3">3시간</option>
</Field>) </Field>)
}
return (<Field as="select" name="usetime">
<option value="">이용시간을 선택하세요</option>
<option value="1">1시간</option>
<option value="2">2시간</option>
<option value="3">3시간</option>
</Field>)
} }
function getUser() { function getUser() {
...@@ -93,17 +92,21 @@ function Apply(props) { ...@@ -93,17 +92,21 @@ function Apply(props) {
url: '/reserves', url: '/reserves',
data: values data: values
}).then(res => { }).then(res => {
if (res.status === 404) return alert(res.data.error) if (res.status === 404) {
alert(res.data.error)
return window.location.reload();
}
alert("신청이 완료되었습니다!"); alert("신청이 완료되었습니다!");
setState(true); setState(true);
console.log("res.data", res.data)
}) })
.catch(err => { .catch(err => {
alert(err.error) alert(err.error)
}); });
// setTimeout(() => { setTimeout(() => {
// setSubmitting(false); setSubmitting(false);
// }, 400); // finish the cycle in handler }, 400); // finish the cycle in handler
}} }}
> >
{({ {({
...@@ -208,7 +211,6 @@ function Apply(props) { ...@@ -208,7 +211,6 @@ function Apply(props) {
> >
X X
</button> </button>
</div> </div>
</div> </div>
))} ))}
...@@ -234,4 +236,4 @@ function Apply(props) { ...@@ -234,4 +236,4 @@ function Apply(props) {
) )
} }
export default Apply export default Apply
\ No newline at end of file
...@@ -59,7 +59,7 @@ function Check(props) { ...@@ -59,7 +59,7 @@ function Check(props) {
<td>{reserve.starttime}~{(Number(reserve.starttime) + reserve.usetime)}</td> <td>{reserve.starttime}~{(Number(reserve.starttime) + reserve.usetime)}</td>
<td>{reserve.room}</td> <td>{reserve.room}</td>
<td>{reserve.num}</td> <td>{reserve.num}</td>
<td>{reserve.approve ? "승인" : "미승인"}</td> <td>{reserve.check ? (reserve.approve ? "사용가능" : "사용불가") : "승인대기중"}</td>
<td> <td>
<button onClick={() => remove(index)} className="btn btn-danger"> <button onClick={() => remove(index)} className="btn btn-danger">
취소 취소
......
...@@ -4,7 +4,7 @@ import * as Yup from 'yup'; ...@@ -4,7 +4,7 @@ import * as Yup from 'yup';
import axios from 'axios'; import axios from 'axios';
import { Link, Redirect } from 'react-router-dom'; import { Link, Redirect } from 'react-router-dom';
import styled from 'styled-components'; import styled from 'styled-components';
import { Col, Container, Navbar } from 'react-bootstrap'; import { Col, Container, Navbar, Button } from 'react-bootstrap';
const Menu = styled(Navbar)` const Menu = styled(Navbar)`
background-color: #7B031D; background-color: #7B031D;
...@@ -46,7 +46,7 @@ function Signup() { ...@@ -46,7 +46,7 @@ function Signup() {
</Menu> </Menu>
<Container fluid> <Container fluid>
<Wow className="row justify-content-center"> <Wow className="row justify-content-center">
<Col md={4} className="d-flex align-items-center h-100"> <Col md={3} xs={11} className="p-0">
<Formik <Formik
initialValues={{ name: '', id: '', password: '', password2: '', question: '', answer: '' }} initialValues={{ name: '', id: '', password: '', password2: '', question: '', answer: '' }}
validationSchema={Yup.object({ validationSchema={Yup.object({
...@@ -154,8 +154,8 @@ function Signup() { ...@@ -154,8 +154,8 @@ function Signup() {
{...getFieldProps('answer')} {...getFieldProps('answer')}
placeholder="Input answer" /> placeholder="Input answer" />
</div> </div>
<button type="submit" className="btn btn-dark" disabled={isSubmitting}>회원가입</button> <Button type="submit" variant="secondary" disabled={isSubmitting}>회원가입</Button>
<button class="btn btn-light"><Link to="/login">로그인하러 가기</Link></button> <Button variant="outline-secondary" as={Link} to="/login">로그인하러 가기</Button>
</form> </form>
)} )}
</Formik> </Formik>
......
...@@ -7,14 +7,13 @@ const router = express.Router(); ...@@ -7,14 +7,13 @@ const router = express.Router();
router.post('/', function (req, res, next) { router.post('/', function (req, res, next) {
console.log('/reserve post req.body', req.body) console.log('/reserve post req.body', req.body)
const reserve = new Reserve({ const reserve = new Reserve({
user: req.body._id, user: req.body._id,
date: req.body.date, date: req.body.date,
starttime: Number(req.body.starttime), starttime: Number(req.body.starttime),
usetime: Number(req.body.usetime), usetime: Number(req.body.usetime),
start: `${req.body.date}T`+`${req.body.starttime}:00:00`, start: `${req.body.date}T` + `${req.body.starttime}:00:00`,
end: `${req.body.date}T`+`${Number(req.body.starttime)+Number(req.body.usetime)}:00:00`, end: `${req.body.date}T` + `${Number(req.body.starttime) + Number(req.body.usetime)}:00:00`,
room: req.body.room, room: req.body.room,
reason: req.body.reason, reason: req.body.reason,
students: req.body.students, students: req.body.students,
...@@ -22,26 +21,60 @@ router.post('/', function (req, res, next) { ...@@ -22,26 +21,60 @@ router.post('/', function (req, res, next) {
num: req.body.students.length + 1, num: req.body.students.length + 1,
}); });
reserve.save() Reserve.find({ room: req.body.room, approve: true }, function (err, reserves) {
.then((result) => { if (err) return res.status(500).json({ error: err });
console.log(result);
res.status(201).json(result); if (!reserves) {
}) reserve.save()
.catch((err) => { .then((result) => {
console.error(err); console.log(result);
next(err); res.status(201).json(result);
}); })
.catch((err) => {
console.error(err);
next(err);
});
}
const strt = new Date(reserve.start)
const endt = new Date(reserve.end)
const reserveArr = reserves.map(item => (
(strt >= new Date(item.start) && strt <= new Date(item.end)) ||
(endt >= new Date(item.start) && endt <= new Date(item.end)) ?
"item" :
null
))
console.log("array", reserveArr)
if (!reserveArr.includes("item")) {
reserve.save()
.then((result) => {
console.log(result);
res.status(201).json(reserves);
})
.catch((err) => {
console.error(err);
next(err);
});
}
else return res.status(404).json({ error: "다른 시간을 선택해주세요." })
})
}); });
router.get('/room/:room', function (req, res, next) {
console.log('reserves get room req.params', req.params)
Reserve.find({ room: req.params.room, approve: true }, function (err, reserve) {
if (err) return res.status(500).json({ error: err });
console.log('reserve room list', reserve);
res.status(201).json(reserve);
})
})
// router.get('/:_id', verifyToken, function (req, res, next) { // router.get('/:_id', verifyToken, function (req, res, next) {
router.get('/:_id', function (req, res, next) { router.get('/:_id', function (req, res, next) {
console.log('/reserves get req.params', req.params) console.log('/reserves get req.params', req.params)
Reserve.find({ user: req.params._id }, function (err, reserve) { Reserve.find({ user: req.params._id }, function (err, reserve) {
console.log('id.name',reserve)
if (err) return res.status(500).json({ error: err }); if (err) return res.status(500).json({ error: err });
console.log('reserve list', reserve)
res.status(201).json(reserve); res.status(201).json(reserve);
}) })
}); });
...@@ -73,9 +106,10 @@ router.delete('/:_id', function (req, res, next) { ...@@ -73,9 +106,10 @@ router.delete('/:_id', function (req, res, next) {
router.put('/:id', function (req, res, next) { router.put('/:id', function (req, res, next) {
console.log('/reserves put req.body', req.params) console.log('/reserves put req.body', req.params)
Reserve.findOne({ _id: req.params.id }, 'approve', function (err, reserve) { Reserve.findOne({ _id: req.params.id }, 'check approve', function (err, reserve) {
if (err) return res.status(500).json({ error: err }); if (err) return res.status(500).json({ error: err });
reserve.approve = true; reserve.check = true;
reserve.approve = req.body.approve;
reserve.save() reserve.save()
.then((result) => { .then((result) => {
console.log(result); console.log(result);
......
...@@ -39,11 +39,14 @@ const reserveSchema = new Schema({ ...@@ -39,11 +39,14 @@ const reserveSchema = new Schema({
approve: { approve: {
type: Boolean, type: Boolean,
default: false, default: false,
},
check: {
type: Boolean,
default: false,
}, },
num: { num: {
type: Number, type: Number,
}, },
}); });
module.exports = mongoose.model('Reserve', reserveSchema); module.exports = mongoose.model('Reserve', reserveSchema);
\ No newline at end of file
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment