Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

front(UI): Adiciona pop-up ao tentar salvar grades na nuvem #200

Merged
merged 12 commits into from
Feb 13, 2024
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions api/api/tests/test_schedule_save.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from rest_framework_simplejwt.serializers import TokenObtainPairSerializer

from ..views.save_schedule import SCHEDULES_LIMIT, SCHEDULES_LIMIT_ERROR_MSG
from ..views.save_schedule import SCHEDULES_LIMIT, SCHEDULES_LIMIT_ERROR_MSG, SCHEDULES_INVALID_SCHEDULES_MSG

from utils import db_handler as dbh

Expand Down Expand Up @@ -251,7 +251,7 @@ def test_save_incorrect_schedule_with_compatible_classes(self):

response = self.make_post_request(schedule=schedule)

error_msg = 'error while saving schedule, you may have chosen classes that are not compatible'
error_msg = SCHEDULES_INVALID_SCHEDULES_MSG
self.assertEqual(response.data.get('errors'), error_msg)
self.assertEqual(response.status_code, 400)

Expand Down
8 changes: 5 additions & 3 deletions api/api/views/save_schedule.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
from api import serializers

SCHEDULES_LIMIT = 10
SCHEDULES_LIMIT_ERROR_MSG = f"you have reached the limit of {SCHEDULES_LIMIT} schedules"
SCHEDULES_LIMIT_ERROR_MSG = f"Você atingiu o limite de {SCHEDULES_LIMIT} grades na nuvem."
SCHEDULES_INVALID_SCHEDULES_MSG = "Erro ao salvar a grade horária. Você pode ter escolhido disciplinas com horários incompatíveis."
SCHEDULES_SAVE_ERROR_MSG = "Ocorreu um erro no servidor ao salvar a grade horária. Tente novamente."


class SaveSchedule():
Expand Down Expand Up @@ -51,13 +53,13 @@ def post(self, request: request.Request, *args, **kwargs) -> response.Response:
try:
valid_schedule = validate_received_schedule(current_db_classes_ids)
except:
error_msg = "error while saving schedule, you may have chosen classes that are not compatible"
error_msg = SCHEDULES_INVALID_SCHEDULES_MSG
return handle_400_error(error_msg)

user = request.user
answer = save_schedule(user, valid_schedule)

return response.Response(status=status.HTTP_201_CREATED) if answer else handle_400_error("error while saving schedule")
return response.Response(status=status.HTTP_201_CREATED) if answer else handle_400_error(SCHEDULES_SAVE_ERROR_MSG)
mateusvrs marked this conversation as resolved.
Show resolved Hide resolved


def check_discipline_key_existence(key: str, discipline_key: str, **kwargs):
Expand Down
34 changes: 25 additions & 9 deletions web/app/components/SchedulePreview/SchedulePreview.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,11 @@ import getSchedules from '@/app/utils/api/getSchedules';
import { days, months } from '@/app/utils/dates';
import deleteSchedule from '@/app/utils/api/deleteSchedule';
import { errorToast } from '@/app/utils/errorToast';
import { successToast } from '@/app/utils/successToast';

import jsPDF from 'jspdf';
import toast from 'react-hot-toast';
import { Axios, AxiosError } from 'axios';

const commonError = () => errorToast('Houve um erro na atualização das grades!');

Expand Down Expand Up @@ -96,15 +99,28 @@ function BottomPart(props: {

const [changeDate, setChangeDate] = useState('');

/**
* Tenta salvar a grade na nuvem, se der certo, atualiza as grades locais.
* Caso contrário, exibe errotToast com mensagem retornada pela API.
*/
async function handleUploadToCloud() {
const saveResponse = await saveSchedule(props.schedules.localSchedule, user.access);

if (saveResponse.status == 201) {
getSchedules(user.access).then(response => {
props.handleDelete();
setCloudSchedules(response.data);
}).catch(() => commonError());
} else errorToast('Não foi possível salvar a grade na nuvem!');
try {
const saveResponse = await saveSchedule(props.schedules.localSchedule, user.access);

if (saveResponse.status == 201) {
getSchedules(user.access).then(response => {
props.handleDelete();
setCloudSchedules(response.data);
}).catch(() => commonError());
successToast('Grade salva com sucesso!');
}
} catch (error) {
const axiosError = error as AxiosError;
if (axiosError.response) {
const data = axiosError.response.data as { errors: string };
errorToast(data.errors);
}
}
}

useEffect(() => {
Expand Down Expand Up @@ -220,4 +236,4 @@ export default function SchedulePreview({ localSchedule, cloudSchedule, index, p
/>
</>
);
}
}
11 changes: 11 additions & 0 deletions web/app/utils/successToast.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import toast from 'react-hot-toast';
import React from 'react';

export const successToast = (message: string, centered: boolean = true) => {
caio-felipee marked this conversation as resolved.
Show resolved Hide resolved
toast.success(message, {
duration: 5000,
style: {
textAlign: centered ? 'center' : 'justify'
},
});
};
Loading