-
Notifications
You must be signed in to change notification settings - Fork 0
/
editor.js
185 lines (150 loc) · 5.16 KB
/
editor.js
1
2
3
4
5
6
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
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
78
79
80
81
82
83
84
85
86
87
88
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
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
183
184
185
import Button from '@material-ui/core/Button'
import ButtonGroup from '@material-ui/core/ButtonGroup'
import NavBar from '../../components/index/AppBar'
import styles from '../../styles/Editor.module.css'
import { useState } from 'react'
import { makeStyles } from '@material-ui/core/styles'
import Select from '@material-ui/core/Select'
import MenuItem from '@material-ui/core/MenuItem'
import InputLabel from '@material-ui/core/InputLabel'
import FormControl from '@material-ui/core/FormControl'
import List from '@material-ui/core/List'
import { useUser, withPageAuthRequired, getSession } from '@auth0/nextjs-auth0';
import base_URL from '../../utils/variables'
import AulasForm from '../../components/editor/AulasForm'
import CardAula from '../../components/turma/CardAula'
const { BASE_URL } = process.env
const useStyles = makeStyles((theme) => ({
root: {},
formItem: {
width: '90%',
margin: '5px 5% 5px 5%'
}
}))
export default function Editor({turmas}) {
const classes = useStyles()
const [ edTurmas, setEdTurmas ] = useState(false)
const [ edAulas, setEdAulas ] = useState(false)
const handleEdTurmas = () => {
if(edTurmas){
setEdTurmas(false)
} else {
setEdTurmas(true)
setEdAulas(false)
}
}
const handleEdAulas = () => {
if(edAulas){
setEdAulas(false)
} else {
setEdAulas(true)
setEdTurmas(false)
}
}
const handleNewAula = async (newAula) => {
const url = `/api/aulas/${turma}`
console.log(newAula)
const res = await fetch(
url,
{
body: JSON.stringify(newAula),
headers: {
'Content-Type': 'application/json'
},
method: 'POST'
}
)
const result = await res.json()
if(result.success) {
console.log(result.data)
setDados(result.data)
}
}
const handleDelete = async (id) => {
const url = `/api/aulas/${turma}`
const res = await fetch(
url,
{
body: JSON.stringify({_id: id}),
headers: {
'Content-Type': 'application/json'
},
method: 'DELETE'
}
)
const result = await res.json()
if(result.success)
console.log(result.data)
setDados(result.data)
}
const [dados, setDados ] = useState(turmas[0])
const [turma, setTurma ] = useState('')
const handleTurma = (event) => {
const newTurma = event.target.value
const newDados = turmas.find(({ key }) => key === newTurma)
setDados(newDados.aulas)
setTurma(newTurma)
}
if(!turmas) {
return (
<>
<p>Você não possui Permissão</p>
</>
)
}
return (
<>
<NavBar />
<div className={classes.formItem}>
<ButtonGroup className={styles.painel} variant="contained" color="primary" aria-label="contained primary button group">
<Button onClick={handleEdTurmas}>Editar Turma</Button>
<Button onClick={handleEdAulas}>Editar Aulas </Button>
<Button>***</Button>
</ButtonGroup>
</div>
{ edTurmas && (<p>Hello World</p>)}
{ edAulas && (<>
<div>
<FormControl className={classes.formItem} >
<InputLabel id="turma-select-label">Selecione Turma</InputLabel>
<Select
labelId="turma-select-label"
id="turma-select"
value={turma}
onChange={handleTurma}
>
{turmas.map((option, index) => (
<MenuItem key={index} value={option.key}>{option.title}</MenuItem>
))}
</Select>
</FormControl>
</div>
</>)}
{ turma !== '' && dados && (
<>
<AulasForm send={handleNewAula} />
<div className={classes.formItem} >
<List dense={true} className={classes.root}>
{dados.map((aula, index) => (
<CardAula key={index} aula={aula} del={true} delFunction={handleDelete}/>
))}
</List>
</div>
</>
)}
</>
)
}
Editor.getInitialProps = async (context) => {
const session = await getSession(context.req, context.res)
if(session){
if(session.user.email == '[email protected]') {
const response = await fetch(base_URL + '/api/turmas')
const turmass = await response.json()
return { turmas: turmass.data }
}
}
return { turmas: false }
}
/*
*/