-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathExpandePost.js
More file actions
385 lines (320 loc) · 9.88 KB
/
ExpandePost.js
File metadata and controls
385 lines (320 loc) · 9.88 KB
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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
import { useContext, useEffect, useState } from 'react';
import { StyleSheet, Text, View, Image, ScrollView, Pressable, Modal, ActivityIndicator, ToastAndroid } from 'react-native';
import { StorageAccessFramework } from 'expo-file-system';
import { Telas, Ip, User } from './Globais';
import { Buffer } from 'buffer';
export default function ExpandePost(props) {
const post = props.post;
const dataPubli = new Date(post.dataPublicacao);
const dataEdicao = new Date(post.dataEdicao);
const { trocaTela } = useContext(Telas);
const { ipAddress } = useContext(Ip);
const { usuario } = useContext(User);
const[maisOpcoes, setMaisOpcoes] = useState(false);
const[confirmaExclusao, setConfirmaExclusao] = useState(false);
const[loading, setLoading] = useState(false);
const[objAnexos, setObjAnexos] = useState([]);
/* ------------------ FUNÇÕES NÃO VISUAIS ------------------ */
useEffect(() => {
if (post['idAnexo'] == null) {
return;
}
var idsAnexo = post['idAnexo'].split(',');
var nomesAnexo = post['nomeArquivo'].split(',');
var objectAnexos = idsAnexo.map((value, index) => {
return {"id": value, "nomeArquivo": nomesAnexo[index]}
})
setObjAnexos(objectAnexos);
}, [])
async function deletaPost() {
if (usuario == null) {
ToastAndroid.show("Não é possível deletar posts sem fazer login.", ToastAndroid.SHORT);
return;
}
if (usuario.permissao == "MODERADOR" || usuario.permissao == "PROFESSOR") {}
else {
ToastAndroid.show("Apeanas professores e moderadores podem deletar.", ToastAndroid.SHORT);
return;
}
setLoading(true);
try {
const controller = new AbortController();
setTimeout(() => {controller.abort()}, 10000)
var respCrua = await fetch("http://" + ipAddress + ":8000/publicacao/" + post.idPublicacao, {method: "DELETE", signal: controller.signal});
var resposta = await respCrua.json();
if (resposta.erro != null) {
throw resposta.erro;
}
} catch (error) {
ToastAndroid.show("Houve um erro deletando o post, por favor verifique sua conexão", ToastAndroid.SHORT);
setLoading(false);
return;
}
setLoading(false);
ToastAndroid.show("Post deletado com sucesso!", ToastAndroid.SHORT);
trocaTela("inicio");
return;
}
async function downloadAnexo(id) {
const uriDownload = StorageAccessFramework.getUriForDirectoryInRoot("Download")
const permissao = await StorageAccessFramework.requestDirectoryPermissionsAsync(uriDownload);
if (permissao.granted) {
setLoading(true);
try {
const controller = new AbortController();
var timeoutAnexo = setTimeout(() => {controller.abort()}, 30000)
var anexo = await fetch("http://" + ipAddress + ":8000/anexo/" + id, {signal: controller.signal});
var anexoJson = await anexo.json();
clearTimeout(timeoutAnexo);
if (anexoJson.erro != null) {
throw anexoJson.erro;
}
var uriArquivo = await StorageAccessFramework.createFileAsync(permissao.directoryUri, anexoJson.resposta[0].nomeArquivo, "");
await StorageAccessFramework.writeAsStringAsync(
uriArquivo,
Buffer.from(anexoJson.resposta[0].anexo.data).toString("base64"), {encoding: "base64"});
} catch (error) {
ToastAndroid.show("Houve uma falha baixando o arquivo, por favor verifique sua conexão.", ToastAndroid.SHORT);
setLoading(false);
return;
}
setLoading(false);
ToastAndroid.show("O arquivo foi baixado com sucesso!", ToastAndroid.SHORT);
}
return;
}
/* ------------------ ELEMENTOS VISUAIS ------------------ */
function Anexo({item}) {
return (
<View style={{
width: "100%",
flexDirection: 'row',
alignItems: "center",
padding: 10,
backgroundColor: "#eee",
borderRadius: 15,
borderWidth: 1,
borderColor: "#ccc",
marginTop: 5
}}>
<Image source={require('./assets/IconArquivo.png')} style={{
height: 20,
width: 20,
marginRight: 5,
resizeMode: "contain",
tintColor: "#777"
}}/>
<Text style={{flex: 1, color: "#555", fontSize: 15}}>{item.nomeArquivo}</Text>
<Pressable style={{width: 30, height: 30, justifyContent: "center", alignItems: "center"}}
onPress={() => {downloadAnexo(item.id)}}>
<Image
style={{width: "70%", height: "70%", resizeMode: "contain", tintColor: "#0880d5"}}
source={require('./assets/BtnDownload.png')}/>
</Pressable>
</View>
);
}
return (
<View style={styles.container}>
<Modal transparent={false} visible={loading} animationType="slide">
<View style={{height: "100%", width: "100%", backgroundColor: "white", justifyContent: "center"}}>
<ActivityIndicator size={'large'} color={"#190933"}/>
</View>
</Modal>
<Modal transparent={true} visible={maisOpcoes} animationType="slide" onRequestClose={() => {setMaisOpcoes(false)}}>
<Pressable style={styles.modalOpcoes} onPress={() => {setMaisOpcoes(false)}}>
<View style={styles.modalOpcoesFilho}>
<Pressable style={styles.btnOpcoes} onPress={() => {trocaTela('editPost', post)}}>
<Text style={styles.txtOpcoes}>Editar</Text>
</Pressable>
<Pressable style={styles.btnOpcoes} onPress={() => {setConfirmaExclusao(true)}}>
<Text style={[styles.txtOpcoes, {color: "red"}]}>Excluir</Text>
</Pressable>
</View>
</Pressable>
</Modal>
<Modal transparent={true} visible={confirmaExclusao} animationType="fade" onRequestClose={() => {setConfirmaExclusao(false)}}>
<Pressable style={styles.modalExclusao} onPress={() => {setConfirmaExclusao(false)}}>
{/* O exclusaoFilho é um pressable apenas para não fechar o modal quando clicar sobre ele */}
<Pressable style={styles.exclusaoFilho}>
<View style={styles.viewTxtConfirmacao}>
<Text style={styles.txtConfirmacao}>Tem certeza que deseja excluir este post?</Text>
</View>
<View style={styles.viewBtnExclusao}>
<Pressable onPress={() => {setConfirmaExclusao(false)}} style={[styles.btnExclusao, {borderRightWidth: 1, borderColor: "#ccc"}]}>
<Text style={{fontSize: 20}}>Não</Text>
</Pressable>
<Pressable onPress={() => {deletaPost(); setConfirmaExclusao(false)}} style={styles.btnExclusao}>
<Text style={{color: "red", fontSize: 20}}>Sim</Text>
</Pressable>
</View>
</Pressable>
</Pressable>
</Modal>
<ScrollView style={styles.post} contentContainerStyle={{padding: 20, flexGrow: 1, justifyContent: "space-between"}}>
<View>
<View style={styles.top}>
<View style={styles.prof}>
<Image style={styles.gato} source={{uri: "data:image/jpg;base64," + Buffer.from(post.fotoPerfil || "", "hex").toString("base64")}}/>
<View style={styles.email}>
<Text numberOfLines={1}>{post.nomeUsuario}</Text>
<Text numberOfLines={1}>{post.email}</Text>
</View>
</View>
{usuario != null ?
usuario.permissao == "PROFESSOR" || usuario.permissao == "MODERADOR" ?
<Pressable style={styles.btnMaisOpcoes} onPress={() => {setMaisOpcoes(true)}}>
<Image
style={{width: "70%", height: "70%", resizeMode: "contain", tintColor: "#999"}}
source={require('./assets/TresPontos.png')}/>
</Pressable>
:
<></>
:
<></>
}
</View>
<Text style={styles.titulo}>{post.titulo}</Text>
<View>
<Text style={styles.data}>Publicado em {dataPubli.getDate() + "/" + (dataPubli.getMonth() + 1) + "/" + dataPubli.getFullYear()}</Text>
<Text style={styles.data}>Editado em {dataEdicao.getDate() + "/" + (dataEdicao.getMonth() + 1) + "/" + dataEdicao.getFullYear()}</Text>
</View>
<Text style={styles.categoria}>Categoria: {post.categoria}</Text>
<Text style={styles.mensagem}>{post.conteudo}</Text>
</View>
<View>
{
objAnexos.map((value, index) => {
return(
<Anexo item={value} key={index} />
);
})
}
</View>
</ScrollView>
</View>
);
}
const styles = StyleSheet.create({
container: {
flex: 1,
flexDirection: 'column',
padding: '5%'
},
modalOpcoes: {
width: "100%",
height: "100%",
alignItems: "center",
justifyContent: "flex-end"
},
modalOpcoesFilho: {
width: "100%",
height: 100,
backgroundColor: "white",
elevation: 10
},
btnOpcoes: {
flex: 1,
alignItems: "center",
justifyContent: "center"
},
txtOpcoes: {
fontSize: 20
},
modalExclusao: {
width: "100%",
height: "100%",
backgroundColor: "rgba(0,0,0,0.8)",
justifyContent: "center",
alignItems: "center"
},
exclusaoFilho: {
width: "80%",
height: 150,
backgroundColor: "white",
borderRadius: 15,
justifyContent: "space-between"
},
viewTxtConfirmacao: {
flex: 1,
padding: 25,
alignItems: "center"
},
txtConfirmacao: {
fontSize: 20,
textAlign: "center"
},
viewBtnExclusao: {
height: 50,
flexDirection: "row",
justifyContent: "space-around",
alignItems: "center",
borderTopWidth: 1,
borderColor: "#ccc"
},
btnExclusao: {
flex: 1,
height: "100%",
justifyContent: "center",
alignItems: "center"
},
post:{
flex: 1,
elevation: 5,
backgroundColor: 'white',
borderRadius: 3
},
top: {
flexDirection: "row",
justifyContent: "space-between",
alignItems: "center",
marginBottom: 10,
width: "100%"
},
btnMaisOpcoes: {
alignItems: "center",
justifyContent: "center",
width: 30,
height: 30
},
prof:{
flexDirection: 'row',
justifyContent: 'flex-start',
alignItems: 'center',
backgroundColor: '#fff',
height: 40,
flex: 1
},
gato:{
marginRight: 10,
height: 40,
width: 40,
resizeMode: 'cover',
borderRadius: 40
},
email: {
flexDirection:'column',
flex: 1
},
titulo:{
fontSize: 20,
marginBottom: 10,
textAlign: "justify",
fontWeight: 'bold'
},
categoria: {
fontSize: 15,
marginVertical: 15
},
data:{
fontSize: 15,
marginTop: 2,
textAlign: 'left'
},
mensagem: {
fontSize: 18,
marginVertical: 10,
textAlign: "justify",
color: '#888'
},
});