From d3da4af75c342c4af3d8aa6d7566646f33733447 Mon Sep 17 00:00:00 2001
From: Fatih20 <fnri39@protonmail.com>
Date: Thu, 16 Nov 2023 22:55:26 +0700
Subject: [PATCH] Implement resignation.

---
 src/components/gym/GymCard.tsx        |  4 +--
 src/components/profile/Employer.tsx   | 37 +++++++++++++++++++++++++++
 src/pages/Profile.tsx                 |  5 ++--
 src/utils/api/job.ts                  | 14 ++++++++++
 src/utils/context/GymProvider.tsx     | 14 ++++++----
 src/utils/hooks/useFuzzySearch.ts     |  5 ----
 src/utils/validationSchema/trainer.ts |  3 +++
 7 files changed, 67 insertions(+), 15 deletions(-)
 create mode 100644 src/components/profile/Employer.tsx
 create mode 100644 src/utils/api/job.ts
 delete mode 100644 src/utils/hooks/useFuzzySearch.ts

diff --git a/src/components/gym/GymCard.tsx b/src/components/gym/GymCard.tsx
index 3c87c6b..3898455 100644
--- a/src/components/gym/GymCard.tsx
+++ b/src/components/gym/GymCard.tsx
@@ -8,8 +8,8 @@ function GymCard({
   gym: GymReturned;
 }) {
   return (
-    <Link to={`./${id}`}>
-      <article className="flex flex-col rounded-lg items-start justify-start bg-slate-600 text-white font-normal gap-2">
+    <Link to={`/gyms/${id}`} className="w-full">
+      <article className="flex flex-col rounded-lg items-start justify-start bg-slate-600 text-white font-normal gap-2 w-full">
         <img
           src={`${config.GYM_MEDIA_URL}/${pictureId}.jpg`}
           className="w-full rounded-t-lg self-center"
diff --git a/src/components/profile/Employer.tsx b/src/components/profile/Employer.tsx
new file mode 100644
index 0000000..6e3fae9
--- /dev/null
+++ b/src/components/profile/Employer.tsx
@@ -0,0 +1,37 @@
+import { useGym } from "@/utils/context/GymProvider";
+import { UserReturned } from "@/utils/validationSchema/trainer";
+import GymCard from "../gym/GymCard";
+import { resign } from "@/utils/api/job";
+import { useMutation, useQueryClient } from "react-query";
+
+function Employer({ user }: { user: UserReturned }) {
+  const { data: gym, status } = useGym(user.gymId);
+  const queryClient = useQueryClient();
+  const { mutateAsync: resignMutation } = useMutation({
+    mutationFn: async () => await resign({ username: user.username }),
+    onSuccess(data) {
+      queryClient.setQueryData(["user"], { user: data, status: "success" });
+    },
+  });
+  return (
+    <section className="w-full flex flex-col items-center gap-4">
+      <h2 className="text-2xl font-bold self-start">Employer</h2>
+      {status !== "success" ? (
+        <p>Loading...</p>
+      ) : gym === undefined ? (
+        <p>You're not currently employed</p>
+      ) : (
+        <GymCard gym={gym} />
+      )}
+      {user.gymId ? (
+        <button className="btn btn-primary" onClick={() => resignMutation()}>
+          Resign
+        </button>
+      ) : (
+        ""
+      )}
+    </section>
+  );
+}
+
+export default Employer;
diff --git a/src/pages/Profile.tsx b/src/pages/Profile.tsx
index 4dd35b6..1980cf4 100644
--- a/src/pages/Profile.tsx
+++ b/src/pages/Profile.tsx
@@ -1,4 +1,5 @@
 import AddSkillForm from "@/components/profile/AddSkillForm";
+import Employer from "@/components/profile/Employer";
 import ProfileForm from "@/components/profile/ProfileForm";
 import SkillManagement from "@/components/profile/SkillManagement";
 import { logout } from "@/utils/api/auth";
@@ -36,9 +37,7 @@ function Profile() {
             </section>
           </div>
         </section>
-        <section className="w-full flex flex-col items-center">
-          <h2 className="text-2xl font-bold self-start">Employer</h2>
-        </section>
+        {user === undefined ? <p>Loading...</p> : <Employer user={user} />}
         <button
           className="btn w-fit"
           onClick={async () => await logoutMutation()}
diff --git a/src/utils/api/job.ts b/src/utils/api/job.ts
new file mode 100644
index 0000000..12191d4
--- /dev/null
+++ b/src/utils/api/job.ts
@@ -0,0 +1,14 @@
+import axios from "axios";
+import { header } from ".";
+import config from "../config";
+import { UserReturned } from "../validationSchema/trainer";
+
+export async function resign({
+  username,
+}: {
+  username: string;
+}): Promise<UserReturned> {
+  return (
+    await axios.patch(`${config.NODE_JS_API}/api/job/${username}`, {}, header)
+  ).data.user;
+}
diff --git a/src/utils/context/GymProvider.tsx b/src/utils/context/GymProvider.tsx
index f9b877b..e23e0bc 100644
--- a/src/utils/context/GymProvider.tsx
+++ b/src/utils/context/GymProvider.tsx
@@ -20,11 +20,15 @@ export function useAllGyms() {
   return useContext(AllGymsContext);
 }
 
-export function useGym(id: number) {
-  return useQuery(["gyms", String(id)], async () => await getGymById({ id }), {
-    cacheTime: 300000,
-    retry: 3,
-  });
+export function useGym(id: number | null) {
+  return useQuery(
+    ["gyms", String(id)],
+    async () => (id ? await getGymById({ id }) : undefined),
+    {
+      cacheTime: 300000,
+      retry: 3,
+    }
+  );
 }
 
 function GymProvider({ children }: { children: ReactNode }) {
diff --git a/src/utils/hooks/useFuzzySearch.ts b/src/utils/hooks/useFuzzySearch.ts
deleted file mode 100644
index 09ab90b..0000000
--- a/src/utils/hooks/useFuzzySearch.ts
+++ /dev/null
@@ -1,5 +0,0 @@
-function useFuzzySearch() {
-  return true;
-}
-
-export default useFuzzySearch;
diff --git a/src/utils/validationSchema/trainer.ts b/src/utils/validationSchema/trainer.ts
index bff942c..572c648 100644
--- a/src/utils/validationSchema/trainer.ts
+++ b/src/utils/validationSchema/trainer.ts
@@ -30,12 +30,15 @@ export const emailSchema = z
   })
   .email({ message: "Not a valid email!" });
 
+export const gymIdSchema = z.number().nullable();
+
 const userReturned = z
   .object({
     username: usernameSchema,
     name: nameSchema,
     description: descriptionSchema,
     email: emailSchema,
+    gymId: gymIdSchema,
   })
   .required();
 
-- 
GitLab