Aplicações disponíveis
diff --git a/src/pages/CreateApplicationPage.jsx b/src/pages/CreateApplicationPage.jsx
index 0eee213..512690d 100644
--- a/src/pages/CreateApplicationPage.jsx
+++ b/src/pages/CreateApplicationPage.jsx
@@ -140,15 +140,12 @@ function CreateApplicationPage(props) {
useEffect(() => {
if (isLoading && user.status !== 'loading') {
- if (!isEditing && user.role === 'USER') {
+ if (user.role === 'USER') {
setError({
text: 'Operação não permitida',
- description: 'Você não tem permissão para criar aplicações',
+ description: `Você não tem permissão para ${isEditing ? 'editar' : 'criar'} aplicações`,
});
return;
- } else if (isEditing && user.role === 'USER') {
- setError({ text: 'Operação não permitida', description: 'Você não tem permissão para editar esta aplicação' });
- return;
}
const promises = [];
let reqProtocolId = protocolId;
@@ -156,50 +153,47 @@ function CreateApplicationPage(props) {
promises.push(
axios
.get(`${process.env.REACT_APP_API_URL}api/application/getApplication/${applicationId}`, {
- headers: {
- Authorization: `Bearer ${user.token}`,
- },
+ headers: { Authorization: `Bearer ${user.token}` },
})
.then((response) => {
const d = response.data.data;
reqProtocolId = d.protocol.id;
- if (d.applier.id !== user.id && user.role !== 'ADMIN') {
+ if (d.actions.toUpdate !== true)
return Promise.reject({
text: 'Operação não permitida',
description: 'Você não tem permissão para editar esta aplicação',
});
- }
setApplication({
visibility: d.visibility,
answersVisibility: d.answersVisibility,
- viewersUser: d.viewersUser.map((v) => v.id),
- viewersClassroom: d.viewersClassroom.map((v) => v.id),
- answersViewersUser: d.answersViewersUser.map((v) => v.id),
- answersViewersClassroom: d.answersViewersClassroom.map((v) => v.id),
+ viewersUser: d.viewersUser.map(({ id }) => id),
+ viewersClassroom: d.viewersClassroom.map(({ id }) => id),
+ answersViewersUser: d.answersViewersUser.map(({ id }) => id),
+ answersViewersClassroom: d.answersViewersClassroom.map(({ id }) => id),
keepLocation: d.keepLocation,
+ actions: d.actions,
});
- setSearchedClassrooms(d.viewersClassroom.map((c) => ({ id: c.id, name: c.name, users: c.users })));
- setSearchedUsers(d.viewersUser.map((u) => ({ id: u.id, username: u.username, classrooms: u.classrooms })));
- setSearchedAnswerClassrooms(d.answersViewersClassroom.map((c) => ({ id: c.id, name: c.name, users: c.users })));
+ setSearchedClassrooms(d.viewersClassroom.map(({ id, name, users }) => ({ id, name, users })));
+ setSearchedUsers(d.viewersUser.map(({ id, username, classrooms }) => ({ id, username, classrooms })));
+ setSearchedAnswerClassrooms(d.answersViewersClassroom.map(({ id, name, users }) => ({ id, name, users })));
setSearchedAnswerUsers(
- d.answersViewersUser.map((u) => ({ id: u.id, username: u.username, classrooms: u.classrooms }))
+ d.answersViewersUser.map(({ id, username, classrooms }) => ({ id, username, classrooms }))
);
})
- .catch((error) => {
- return Promise.reject({
- text: 'Erro ao buscar aplicação.',
- description: error.response?.data.message,
- });
- })
+ .catch((error) =>
+ Promise.reject(
+ error.text
+ ? error
+ : { text: 'Erro ao obter informações da aplicação', description: error.response?.data.message }
+ )
+ )
);
}
Promise.all(promises)
.then(() => {
axios
.get(`${process.env.REACT_APP_API_URL}api/protocol/getProtocol/${reqProtocolId}`, {
- headers: {
- Authorization: `Bearer ${user.token}`,
- },
+ headers: { Authorization: `Bearer ${user.token}` },
})
.then((response) => {
const d = response.data.data;
@@ -218,10 +212,8 @@ function CreateApplicationPage(props) {
viewersUser: d.viewersUser.map((u) => u.id),
viewersClassroom: d.viewersClassroom.map((c) => c.id),
}));
- setSearchedUsers(
- d.viewersUser.map((u) => ({ id: u.id, username: u.username, classrooms: u.classrooms }))
- );
- setSearchedClassrooms(d.viewersClassroom.map((c) => ({ id: c.id, name: c.name, users: c.users })));
+ setSearchedUsers(d.viewersUser.map(({ id, username, classrooms }) => ({ id, username, classrooms })));
+ setSearchedClassrooms(d.viewersClassroom.map(({ id, name, users }) => ({ id, name, users })));
}
if (d.answersVisibility === 'RESTRICT') {
setApplication((prev) => ({
@@ -230,21 +222,21 @@ function CreateApplicationPage(props) {
answersViewersClassroom: d.answersViewersClassroom.map((c) => c.id),
}));
setSearchedAnswerUsers(
- d.answersViewersUser.map((u) => ({ id: u.id, username: u.username, classrooms: u.classrooms }))
+ d.answersViewersUser.map(({ id, username, classrooms }) => ({ id, username, classrooms }))
);
setSearchedAnswerClassrooms(
- d.answersViewersClassroom.map((c) => ({ id: c.id, name: c.name, users: c.users }))
+ d.answersViewersClassroom.map(({ id, name, users }) => ({ id, name, users }))
);
}
}
setIsLoading(false);
})
- .catch((error) => {
- showAlert({
- headerText: 'Erro ao buscar visualizadores do protocolo.',
- bodyText: error.response?.data.message,
- });
- });
+ .catch((error) =>
+ Promise.reject({
+ text: 'Erro ao obter informações do protocolo',
+ description: error.response?.data.message,
+ })
+ );
})
.catch((error) => setError(error));
}
@@ -256,59 +248,48 @@ function CreateApplicationPage(props) {
if (isEditing) {
axios
.put(`${process.env.REACT_APP_API_URL}api/application/updateApplication/${applicationId}`, formData, {
- headers: {
- 'Content-Type': 'multipart/form-data',
- Authorization: `Bearer ${user.token}`,
- },
+ headers: { 'Content-Type': 'multipart/form-data', Authorization: `Bearer ${user.token}` },
})
.then((response) => {
clearLocalApplications();
showAlert({
- headerText: 'Aplicação atualizada com sucesso.',
+ headerText: 'Aplicação atualizada com sucesso',
onPrimaryBtnClick: () => navigate(`/dash/applications/${response.data.data.id}`),
});
})
- .catch((error) => showAlert({ headerText: 'Erro ao atualizar aplicação.', description: error.response?.data.message }));
+ .catch((error) => showAlert({ headerText: 'Erro ao atualizar aplicação', description: error.response?.data.message }));
} else {
axios
.post(`${process.env.REACT_APP_API_URL}api/application/createApplication`, formData, {
- headers: {
- 'Content-Type': 'multipart/form-data',
- Authorization: `Bearer ${user.token}`,
- },
+ headers: { 'Content-Type': 'multipart/form-data', Authorization: `Bearer ${user.token}` },
})
.then((response) => {
showAlert({
- headerText: 'Aplicação criada com sucesso.',
+ headerText: 'Aplicação criada com sucesso',
onPrimaryBtnClick: () => navigate(`/dash/applications/${response.data.data.id}`),
});
})
- .catch((error) => showAlert({ headerText: 'Erro ao criar aplicação.', bodyText: error.response?.data.message }));
+ .catch((error) => showAlert({ headerText: 'Erro ao criar aplicação', bodyText: error.response?.data.message }));
}
};
const deleteApplication = () => {
axios
.delete(`${process.env.REACT_APP_API_URL}api/application/deleteApplication/${applicationId}`, {
- headers: {
- Authorization: `Bearer ${user.token}`,
- },
+ headers: { Authorization: `Bearer ${user.token}` },
})
.then((response) => {
clearLocalApplications();
- showAlert({ headerText: 'Aplicação excluída com sucesso.', onPrimaryBtnClick: () => navigate(`/dash/applications/`) });
+ showAlert({ headerText: 'Aplicação excluída com sucesso', onPrimaryBtnClick: () => navigate(`/dash/applications/`) });
})
- .catch((error) => showAlert({ headerText: 'Erro ao excluir aplicação.', bodyText: error.response?.data.message }));
+ .catch((error) => showAlert({ headerText: 'Erro ao excluir aplicação', bodyText: error.response?.data.message }));
};
const searchUsers = (term) => {
const formData = serialize({ term }, { indices: true });
axios
.post(`${process.env.REACT_APP_API_URL}api/user/searchUserByUsername`, formData, {
- headers: {
- 'Content-Type': 'multipart/form-data',
- Authorization: `Bearer ${user.token}`,
- },
+ headers: { 'Content-Type': 'multipart/form-data', Authorization: `Bearer ${user.token}` },
})
.then((response) => {
const d = response.data.data;
@@ -326,17 +307,14 @@ function CreateApplicationPage(props) {
];
setSearchedUsers(newUsers);
})
- .catch((error) => showAlert({ headerText: 'Erro ao buscar usuários.', bodyText: error.response?.data.message }));
+ .catch((error) => showAlert({ headerText: 'Erro ao buscar usuários', bodyText: error.response?.data.message }));
};
const searchAnswerUsers = (term) => {
const formData = serialize({ term }, { indices: true });
axios
.post(`${process.env.REACT_APP_API_URL}api/user/searchUserByUsername`, formData, {
- headers: {
- 'Content-Type': 'multipart/form-data',
- Authorization: `Bearer ${user.token}`,
- },
+ headers: { 'Content-Type': 'multipart/form-data', Authorization: `Bearer ${user.token}` },
})
.then((response) => {
const d = response.data.data;
@@ -354,17 +332,14 @@ function CreateApplicationPage(props) {
];
setSearchedAnswerUsers(newUsers);
})
- .catch((error) => showAlert({ headerText: 'Erro ao buscar usuários.', bodyText: error.response?.data.message }));
+ .catch((error) => showAlert({ headerText: 'Erro ao buscar usuários', bodyText: error.response?.data.message }));
};
const searchClassrooms = (term) => {
const formData = serialize({ term }, { indices: true });
axios
.post(`${process.env.REACT_APP_API_URL}api/classroom/searchClassroomByName`, formData, {
- headers: {
- 'Content-Type': 'multipart/form-data',
- Authorization: `Bearer ${user.token}`,
- },
+ headers: { 'Content-Type': 'multipart/form-data', Authorization: `Bearer ${user.token}` },
})
.then((response) => {
const d = response.data.data;
@@ -399,17 +374,14 @@ function CreateApplicationPage(props) {
// }
setSearchedClassrooms(concatenedClassrooms);
})
- .catch((error) => showAlert({ headerText: 'Erro ao buscar grupos.', bodyText: error.response?.data.message }));
+ .catch((error) => showAlert({ headerText: 'Erro ao buscar grupos', bodyText: error.response?.data.message }));
};
const searchAnswerClassrooms = (term) => {
const formData = serialize({ term }, { indices: true });
axios
.post(`${process.env.REACT_APP_API_URL}api/classroom/searchClassroomByName`, formData, {
- headers: {
- 'Content-Type': 'multipart/form-data',
- Authorization: `Bearer ${user.token}`,
- },
+ headers: { 'Content-Type': 'multipart/form-data', Authorization: `Bearer ${user.token}` },
})
.then((response) => {
const d = response.data.data;
@@ -444,7 +416,7 @@ function CreateApplicationPage(props) {
// }
setSearchedAnswerClassrooms(concatenedClassrooms);
})
- .catch((error) => showAlert({ headerText: 'Erro ao buscar grupos.', bodyText: error.response?.data.message }));
+ .catch((error) => showAlert({ headerText: 'Erro ao buscar grupos', bodyText: error.response?.data.message }));
};
const unselectUser = (id) => {
@@ -465,9 +437,7 @@ function CreateApplicationPage(props) {
setSearchedUsers((prev) =>
[
...prev.map((u) => {
- if (newUsers.includes(u.id)) {
- return { ...u, classrooms: [...u.classrooms, c.id] };
- }
+ if (newUsers.includes(u.id)) return { ...u, classrooms: [...u.classrooms, c.id] };
return u;
}),
...newUsers.filter((u) => !prev.map((u) => u.id).includes(u.id)),
@@ -504,9 +474,7 @@ function CreateApplicationPage(props) {
setSearchedAnswerUsers((prev) =>
[
...prev.map((u) => {
- if (newUsers.includes(u.id)) {
- return { ...u, classrooms: [...u.classrooms, c.id] };
- }
+ if (newUsers.includes(u.id)) return { ...u, classrooms: [...u.classrooms, c.id] };
return u;
}),
...newUsers.filter((u) => !prev.map((u) => u.id).includes(u.id)),
@@ -525,13 +493,9 @@ function CreateApplicationPage(props) {
}));
};
- if (error) {
- return
;
- }
+ if (error) return
;
- if (isLoading) {
- return
;
- }
+ if (isLoading) return
;
return (
@@ -562,9 +526,7 @@ function CreateApplicationPage(props) {
role="switch"
id="enabled"
checked={application.keepLocation || false}
- onChange={(event) =>
- setApplication((prev) => ({ ...prev, keepLocation: event.target.checked }))
- }
+ onChange={(e) => setApplication((prev) => ({ ...prev, keepLocation: e.target.checked }))}
/>
@@ -635,7 +595,7 @@ function CreateApplicationPage(props) {
className="form-check-input bg-grey"
checked={application.viewersUser.includes(u.id)}
onChange={(e) => {
- if (e.target.checked) {
+ if (e.target.checked)
setApplication((prev) => ({
...prev,
viewersUser: [
@@ -643,9 +603,7 @@ function CreateApplicationPage(props) {
parseInt(e.target.value),
],
}));
- } else {
- unselectUser(parseInt(e.target.value));
- }
+ else unselectUser(parseInt(e.target.value));
}}
/>
@@ -709,16 +665,14 @@ function CreateApplicationPage(props) {
className="form-check-input bg-grey"
checked={application.viewersClassroom.includes(c.id)}
onChange={(e) => {
- if (e.target.checked) {
- selectClassroom(parseInt(e.target.value));
- } else {
+ if (e.target.checked) selectClassroom(parseInt(e.target.value));
+ else
setApplication((prev) => ({
...prev,
viewersClassroom: prev.viewersClassroom.filter(
(id) => id !== parseInt(e.target.value)
),
}));
- }
}}
/>