Skip to content

Commit

Permalink
fix: minor fixes in backend and frontend
Browse files Browse the repository at this point in the history
backend: simple array
front:
- logout req
- React.useState -> useState
- better error response in plans functions
  • Loading branch information
qamarq committed Dec 9, 2024
1 parent 0bd6143 commit ec7b260
Show file tree
Hide file tree
Showing 4 changed files with 13 additions and 25 deletions.
18 changes: 3 additions & 15 deletions backend/app/controllers/schedules_controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,27 +79,15 @@ export default class SchedulesController {
})

if (payload.groups !== undefined) {
if (payload.groups.length === 0) {
await schedule.related('groups').sync([])
} else {
await schedule.related('groups').sync(payload.groups.map((group) => group.id))
}
await schedule.related('groups').sync(payload.groups.map((group) => group.id))
}

if (payload.registrations !== undefined) {
if (payload.registrations.length === 0) {
await schedule.related('registrations').sync([])
} else {
await schedule.related('registrations').sync(payload.registrations.map((group) => group.id))
}
await schedule.related('registrations').sync(payload.registrations.map((group) => group.id))
}

if (payload.courses !== undefined) {
if (payload.courses.length === 0) {
await schedule.related('courses').sync([])
} else {
await schedule.related('courses').sync(payload.courses.map((group) => group.id))
}
await schedule.related('courses').sync(payload.courses.map((group) => group.id))
}

return { message: 'Schedule created.', schedule }
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/actions/logout.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ export const signOutFunction = async () => {
path: "/",
});

await fetch(`${env.NEXT_PUBLIC_API_URL}/user`, { method: "DELETE" });
await fetch(`${env.NEXT_PUBLIC_API_URL}/user/logout`, { method: "DELETE" });

return true;
};
12 changes: 6 additions & 6 deletions frontend/src/actions/plans.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ export const createNewPlan = async ({
}) => {
const isLogged = await auth({});
if (!isLogged) {
return false;
throw new Error("Not logged in");
}

const data = await fetchToAdonis<CreatePlanResponseType>({
Expand All @@ -71,7 +71,7 @@ export const createNewPlan = async ({
body: JSON.stringify({ name, courses, registrations, groups }),
});
if (!data) {
return false;
throw new Error("Failed to create new plan");
}
return data;
};
Expand All @@ -91,7 +91,7 @@ export const updatePlan = async ({
}) => {
const isLogged = await auth({});
if (!isLogged) {
return false;
throw new Error("Not logged in");
}

const data = await fetchToAdonis<CreatePlanResponseType>({
Expand All @@ -100,23 +100,23 @@ export const updatePlan = async ({
body: JSON.stringify({ name, courses, registrations, groups }),
});
if (!data) {
return false;
throw new Error("Failed to update plan");
}
return data;
};

export const deletePlan = async ({ id }: { id: number }) => {
const isLogged = await auth({});
if (!isLogged) {
return false;
throw new Error("Not logged in");
}

const data = await fetchToAdonis<DeletePlanResponseType>({
url: `/user/schedules/${id}`,
method: "DELETE",
});
if (!(data?.success ?? false)) {
return false;
throw new Error("Failed to delete plan");
}
revalidatePath("/plans");
return data;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import { formatDistance, isAfter, isEqual } from "date-fns";
import { pl } from "date-fns/locale";
import { DownloadCloudIcon, Loader2Icon, UploadCloudIcon } from "lucide-react";
import React from "react";
import React, { useState } from "react";

import { Button } from "@/components/ui/button";
import { cn } from "@/lib/utils";
Expand Down Expand Up @@ -49,8 +49,8 @@ export const SyncErrorAlert = ({
downloadChanges: () => void;
sendChanges: () => void;
}) => {
const [loadingSending, setLoadingSending] = React.useState(false);
const [loadingDownloading, setLoadingDownloading] = React.useState(false);
const [loadingSending, setLoadingSending] = useState(false);
const [loadingDownloading, setLoadingDownloading] = useState(false);
if (!onlinePlan) {
return null;
}
Expand Down

0 comments on commit ec7b260

Please sign in to comment.