From eaf094e7f7876985c0b8e9ff991d3121056e9228 Mon Sep 17 00:00:00 2001 From: QamarQ Date: Sun, 15 Dec 2024 13:05:41 +0100 Subject: [PATCH] feat: better organization in .env module wywailem env baseurl bo doslownie do niczego ona nie byla potrzebne, tylko robilismy z tego apps url wiec po prostu to dodalem do env, dzieki czemu moglem uzywac tego pliku tez po kliencie jak juz nie bylo eksportu serwerowego, do tego lepsze nazwy i mniej zmiennych w funkcjach w page.client --- frontend/src/app/api/login/route.ts | 4 +- .../edit/[id]/_components/SyncErrorAlert.tsx | 3 -- .../edit/[id]/_components/SyncedButton.tsx | 43 ++++++++++--------- .../src/app/plans/edit/[id]/page.client.tsx | 22 +++------- frontend/src/app/plans/edit/[id]/page.tsx | 4 +- frontend/src/env.mjs | 6 +-- frontend/src/lib/auth/index.ts | 12 +++--- frontend/src/services/usos/usosClient.ts | 4 +- 8 files changed, 42 insertions(+), 56 deletions(-) diff --git a/frontend/src/app/api/login/route.ts b/frontend/src/app/api/login/route.ts index 55ddbab..e7ea7a4 100644 --- a/frontend/src/app/api/login/route.ts +++ b/frontend/src/app/api/login/route.ts @@ -1,7 +1,7 @@ import { cookies as cookiesPromise } from "next/headers"; import { redirect } from "next/navigation"; -import { USOS_APPS_URL } from "@/env.mjs"; +import { env } from "@/env.mjs"; import { getRequestToken } from "@/lib/auth"; export const dynamic = "force-dynamic"; @@ -39,6 +39,6 @@ export async function GET() { }); return redirect( - `${USOS_APPS_URL}/services/oauth/authorize?oauth_token=${token.token}`, + `${env.USOS_APPS_URL}/services/oauth/authorize?oauth_token=${token.token}`, ); } diff --git a/frontend/src/app/plans/edit/[id]/_components/SyncErrorAlert.tsx b/frontend/src/app/plans/edit/[id]/_components/SyncErrorAlert.tsx index 1cc757b..beb2320 100644 --- a/frontend/src/app/plans/edit/[id]/_components/SyncErrorAlert.tsx +++ b/frontend/src/app/plans/edit/[id]/_components/SyncErrorAlert.tsx @@ -39,13 +39,11 @@ interface PlanResponseType { export const SyncErrorAlert = ({ onlinePlan, planDate, - bounce = false, downloadChanges, sendChanges, }: { onlinePlan: PlanResponseType | null | undefined; planDate: Date; - bounce?: boolean; downloadChanges: () => void; sendChanges: () => void; }) => { @@ -74,7 +72,6 @@ export const SyncErrorAlert = ({
diff --git a/frontend/src/app/plans/edit/[id]/_components/SyncedButton.tsx b/frontend/src/app/plans/edit/[id]/_components/SyncedButton.tsx index d079dbb..4dfaa7c 100644 --- a/frontend/src/app/plans/edit/[id]/_components/SyncedButton.tsx +++ b/frontend/src/app/plans/edit/[id]/_components/SyncedButton.tsx @@ -8,6 +8,7 @@ import { RefreshCwOffIcon, } from "lucide-react"; import React, { useMemo } from "react"; +import { toast } from "sonner"; import { Button } from "@/components/ui/button"; import { @@ -15,36 +16,33 @@ import { TooltipContent, TooltipTrigger, } from "@/components/ui/tooltip"; +import type { PlanState } from "@/lib/usePlan"; export const SyncedButton = ({ - synced, - onlineId, - syncing, + plan, + isSyncing, onClick, - bounceAlert, - equalsDates, + isEqualsDates, isOffline, }: { - synced: boolean; - onlineId: string | null; - syncing: boolean; + plan: PlanState; + isSyncing: boolean; onClick: () => Promise; - bounceAlert: () => void; - equalsDates: boolean; + isEqualsDates: boolean; isOffline: boolean; }) => { const message = useMemo(() => { - if (synced && equalsDates) { + if (plan.synced && isEqualsDates) { return "Zsynchronizowano"; - } else if (!(onlineId ?? "")) { + } else if (!(plan.onlineId ?? "")) { return "Plan dostępny tylko lokalnie"; - } else if (syncing) { + } else if (isSyncing) { return "Synchronizowanie..."; - } else if (!equalsDates) { + } else if (!isEqualsDates) { return "Twoja wersja różni się od wersji online"; } return "Masz lokalne zmiany"; - }, [synced, onlineId, syncing, equalsDates]); + }, [plan.synced, plan.onlineId, isSyncing, isEqualsDates]); return ( @@ -53,8 +51,11 @@ export const SyncedButton = ({ variant="outline" className="min-w-10" onClick={() => { - if (!equalsDates) { - bounceAlert(); + if (!isEqualsDates) { + toast.info( + "Wybierz akcję z alertu powyżej, aby zsynchronizować dane tak jak chcesz", + { duration: 5000 }, + ); } else if (isOffline) { return; } else { @@ -62,13 +63,13 @@ export const SyncedButton = ({ } }} > - {synced && equalsDates ? ( + {plan.synced && isEqualsDates ? ( - ) : !(onlineId ?? "") ? ( + ) : !(plan.onlineId ?? "") ? ( - ) : syncing ? ( + ) : isSyncing ? ( - ) : !equalsDates ? ( + ) : !isEqualsDates ? ( ) : ( diff --git a/frontend/src/app/plans/edit/[id]/page.client.tsx b/frontend/src/app/plans/edit/[id]/page.client.tsx index d27d71e..cb25396 100644 --- a/frontend/src/app/plans/edit/[id]/page.client.tsx +++ b/frontend/src/app/plans/edit/[id]/page.client.tsx @@ -27,6 +27,7 @@ import { SelectValue, } from "@/components/ui/select"; import { Skeleton } from "@/components/ui/skeleton"; +import { env } from "@/env.mjs"; import { usePlan } from "@/lib/usePlan"; import { registrationReplacer } from "@/lib/utils"; import { createOnlinePlan } from "@/lib/utils/createOnlinePlan"; @@ -49,7 +50,6 @@ export function CreateNewPlanPage({ }) { const [syncing, setSyncing] = useState(false); const [offlineAlert, setOfflineAlert] = useState(false); - const [bouncingAlert, setBouncingAlert] = useState(false); const [faculty, setFaculty] = useState(null); const firstTime = useRef(true); @@ -64,7 +64,7 @@ export function CreateNewPlanPage({ queryKey: ["registrations", faculty], queryFn: async () => { const response = await fetch( - `${process.env.NEXT_PUBLIC_API_URL}/departments/${faculty}/registrations`, + `${env.NEXT_PUBLIC_API_URL}/departments/${faculty}/registrations`, ); if (!response.ok) { @@ -101,7 +101,7 @@ export function CreateNewPlanPage({ mutationKey: ["courses"], mutationFn: async (registrationId: string) => { const response = await fetch( - `${process.env.NEXT_PUBLIC_API_URL}/departments/${faculty}/registrations/${encodeURIComponent(registrationId)}/courses`, + `${env.NEXT_PUBLIC_API_URL}/departments/${faculty}/registrations/${encodeURIComponent(registrationId)}/courses`, ); if (!response.ok) { @@ -176,13 +176,6 @@ export function CreateNewPlanPage({ } }; - const bounceAlert = () => { - setBouncingAlert(true); - setTimeout(() => { - setBouncingAlert(false); - }, 1000); - }; - const resetInactivityTimer = () => { if (inactivityTimeout.current !== null) { clearTimeout(inactivityTimeout.current); @@ -248,7 +241,6 @@ export function CreateNewPlanPage({ }} planDate={plan.updatedAt} onlinePlan={onlinePlan} - bounce={bouncingAlert} />
@@ -292,13 +284,11 @@ export function CreateNewPlanPage({
r.json())) as Array<{ id: string; name: string }> | null; if (!facultiesRes) { return notFound(); diff --git a/frontend/src/env.mjs b/frontend/src/env.mjs index 4f34a27..8fcf279 100644 --- a/frontend/src/env.mjs +++ b/frontend/src/env.mjs @@ -5,7 +5,7 @@ export const env = createEnv({ server: { USOS_CONSUMER_KEY: z.string().min(1), USOS_CONSUMER_SECRET: z.string().min(1), - USOS_BASE_URL: z.string().startsWith("usos").default("usos.pwr.edu.pl"), + USOS_APPS_URL: z.string().url().default("https://apps.usos.pwr.edu.pl"), SITE_URL: z.string().url().default("http://localhost:3000"), NEXT_PUBLIC_API_URL: z.string().url().default("http://localhost:3000/api"), }, @@ -15,11 +15,9 @@ export const env = createEnv({ runtimeEnv: { SITE_URL: process.env.SITE_URL, NEXT_PUBLIC_API_URL: process.env.NEXT_PUBLIC_API_URL, - USOS_BASE_URL: process.env.USOS_BASE_URL, + USOS_APPS_URL: process.env.USOS_APPS_URL, USOS_CONSUMER_KEY: process.env.USOS_CONSUMER_KEY, USOS_CONSUMER_SECRET: process.env.USOS_CONSUMER_SECRET, }, skipValidation: process.env.SKIP_ENV_VALIDATION === "true", }); - -export const USOS_APPS_URL = `https://apps.${env.USOS_BASE_URL}`; diff --git a/frontend/src/lib/auth/index.ts b/frontend/src/lib/auth/index.ts index ec58215..3ca88c3 100644 --- a/frontend/src/lib/auth/index.ts +++ b/frontend/src/lib/auth/index.ts @@ -24,7 +24,7 @@ export const getAccessToken = async ( ) => { const data = oauth.authorize( { - url: `https://apps.${env.USOS_BASE_URL}/services/oauth/access_token`, + url: `${env.USOS_APPS_URL}/services/oauth/access_token`, method: "POST", data: { oauth_token, oauth_verifier }, }, @@ -35,8 +35,8 @@ export const getAccessToken = async ( ); const response = await fetch( - `https://apps.${ - env.USOS_BASE_URL + `${ + env.USOS_APPS_URL }/services/oauth/access_token?${new URLSearchParams(Object.entries(data)).toString()} `, { @@ -62,7 +62,7 @@ const removeMultipleSlashesFromUrl = (url: string) => { export async function getRequestToken() { const data = oauth.authorize({ - url: `https://apps.${env.USOS_BASE_URL}/services/oauth/request_token`, + url: `${env.USOS_APPS_URL}/services/oauth/request_token`, method: "POST", data: { oauth_callback: removeMultipleSlashesFromUrl( @@ -73,9 +73,7 @@ export async function getRequestToken() { }); const response = await fetch( - `https://apps.${ - env.USOS_BASE_URL - }/services/oauth/request_token?${new URLSearchParams( + `${env.USOS_APPS_URL}/services/oauth/request_token?${new URLSearchParams( Object.entries(data), ).toString()}`, { diff --git a/frontend/src/services/usos/usosClient.ts b/frontend/src/services/usos/usosClient.ts index 4aff515..218b28e 100644 --- a/frontend/src/services/usos/usosClient.ts +++ b/frontend/src/services/usos/usosClient.ts @@ -1,9 +1,9 @@ import fetch from "node-fetch"; -import { USOS_APPS_URL } from "@/env.mjs"; +import { env } from "@/env.mjs"; import { oauth } from "@/lib/auth"; -const baseUrl = `${USOS_APPS_URL}/services`; +const baseUrl = `${env.USOS_APPS_URL}/services`; export const createClient = ({ token,