diff --git a/src/app/admin/organizations/[id]/update/page.tsx b/src/app/admin/organizations/[id]/update/page.tsx index d7e2e60..a3609cf 100644 --- a/src/app/admin/organizations/[id]/update/page.tsx +++ b/src/app/admin/organizations/[id]/update/page.tsx @@ -1,41 +1,47 @@ -"use client" -import { icons } from "#/assets/icons" -import Image from "next/image" +"use client"; + import axios from "axios"; import { useSession } from "next-auth/react"; import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query"; -import { Company, CompanyById } from "#/types"; -import FloatingLabelInput from "#/components/floatingLabelInput"; +import { Admin, Company, CompanyById } from "#/types"; import { usePathname } from "next/navigation"; import Link from "next/link"; import Form from "#/components/form/form"; -import { adminSchema } from "#/schema/loginSchema"; +import { adminUpdateSchema } from "#/schema/loginSchema"; +import { Modal } from "#/components/modal"; +import { useState } from "react"; +import { toast } from "sonner"; export default function Update() { - const pathname = usePathname(); + const queryClient = useQueryClient(); const segments = pathname.split("/"); const uid = segments[segments.length - 2]; - const queryClient = useQueryClient() - const { data: session, status } = useSession(); + const [openResetModal, setOpenResetModal] = useState(false); + const [openBlockModal, setOpenBlockModal] = useState(false); - const { data: companyInfos, refetch, isLoading } = useQuery({ - enabled: status === 'authenticated', - queryKey: ["companyStats", session?.user.access_token], + const { + data: companyInfos, + isLoading: companyLoading, + refetch, + } = useQuery({ + enabled: status === "authenticated", + queryKey: ["company"], queryFn: async () => { try { const response = await axios.get( - `https://private-docs-api.intside.co/users/${uid}`, { - headers: { - 'Authorization': `Bearer ${session?.user.access_token}` - }, - params: { - details: true + `https://private-docs-api.intside.co/companies/${uid}`, + { + headers: { + Authorization: `Bearer ${session?.user.access_token}`, + }, + params: { + details: true, + }, } - } ); if (response.data) { @@ -44,82 +50,296 @@ export default function Update() { } catch (error: any) { console.error(error); } - } + }, + }); + + const { data: users } = useQuery({ + enabled: status === "authenticated", + queryKey: ["organizations"], + queryFn: async () => { + try { + const response = await axios.get( + "https://private-docs-api.intside.co/users", + { + headers: { Authorization: `Bearer ${session?.user.access_token}` }, + } + ); + return response.data.data.map((user: Admin) => ({ + id: user.id, + name: `${user.first_name} ${user.last_name}`, + })); + } catch (error) { + console.error(error); + return []; + } + }, }); const { mutate, isPending } = useMutation({ - mutationFn: async (id: string) => { + mutationFn: async (data: { id: string }) => { try { - const response = await axios.delete( - `https://private-docs-api.intside.co/companies/${id}/`, { - headers: { - 'Authorization': `Bearer ${session?.user.access_token}` + const response = await axios.put( + `https://private-docs-api.intside.co/companies/${data.id}/`, + data, + { + headers: { + Authorization: `Bearer ${session?.user.access_token}`, + }, } - } - ) + ); if (response.status === 200 || response.status === 201) { - console.log('Suppresion réussie !') + console.log("Modif réussie !"); + toast.success("Modification réussie !"); } } catch (error) { - console.error(error) + console.error(error); } }, + onSuccess: () => { - queryClient.invalidateQueries({ queryKey: ["companies"] }) + queryClient.invalidateQueries({ queryKey: ["company"] }); + refetch(); + }, + }); - refetch() - } - }) + const blockMutation = useMutation({ + mutationFn: async (data: { id: string; name: string; status: string }) => { + try { + const response = await axios.put( + `https://private-docs-api.intside.co/companies/${data.id}/`, + data, + { + headers: { + Authorization: `Bearer ${session?.user.access_token}`, + }, + } + ); + if (response.status === 200 || response.status === 201) { + console.log("Statut modifié !"); + setOpenBlockModal(false); + toast.success("Organisation bloquée !"); + } + } catch (error) { + console.error(error); + } + }, + + onSuccess: () => { + queryClient.invalidateQueries({ queryKey: ["company"] }); + refetch(); + }, + }); + + const resetMutation = useMutation({ + mutationFn: async (email: string) => { + try { + const response = await axios.post( + `https://private-docs-api.intside.co/users/reset_password/`, + { + email: email, + }, + { + headers: { + Authorization: `Bearer ${session?.user.access_token}`, + }, + } + ); + + if (response.status === 200 || response.status === 201) { + console.log("Reset réussie !"); + setOpenResetModal(false); + toast.success("Email de réinitialisation envoyé !"); + } + } catch (error) { + console.error(error); + } + }, + }); + + if (status === "loading" && companyLoading) { + return
Chargement...
; + } return ( <> - {/* { company.map() } */} - - - {/*
Connexion} - /> */} - -
-

{companyInfos?.name || "Pentatonic"}

- +

+ {companyInfos?.name || "Pentatonic"} +

+ Annuler
-
+ +
+ ({ + label: user.name, + value: user.id, + })) || [], + }, + { + name: "id", + type: "hidden", + defaultValue: companyInfos?.id, + }, + ]} + submit={mutate} + schema={adminUpdateSchema} + child={ +
+ + + + Mot de passe + + } + content={ +
+

+ Voulez-vous envoyer à{" "} + + {companyInfos?.owner.last_name + + " " + + companyInfos?.owner.first_name + + " "} + + un lien de réinitialisation de mot de passe à son + adresse email ? +

+
+ + +
+
+ } + /> + + + Bloquer + + } + content={ +
+

+ Voulez-vous bloquer l'organisation{" "} + + {companyInfos?.name + " "} + {" "} + ? +

+
+ + +
+
+ } + /> +
+ } + /> +
+ + {/*
@@ -139,9 +359,9 @@ export default function Update() {
-
+
*/}
- ) -} \ No newline at end of file + ); +} diff --git a/src/app/layout.tsx b/src/app/layout.tsx index 7e86cee..13bb7fd 100644 --- a/src/app/layout.tsx +++ b/src/app/layout.tsx @@ -4,6 +4,7 @@ import NextTopLoader from "nextjs-toploader"; import "../assets/css/ruben-ui.css" import { AuthProvider } from "#/components/provider/authProvider"; import { QueryClientProvide } from "#/components/provider/queryClient"; +import { ToastClient } from "#/components/provider/toastClient"; export const metadata: Metadata = { title: "Private Docs", @@ -27,6 +28,7 @@ export default function RootLayout({ {children} + diff --git a/src/components/floatingLabelInput.tsx b/src/components/floatingLabelInput.tsx index b9f87fb..ccda850 100644 --- a/src/components/floatingLabelInput.tsx +++ b/src/components/floatingLabelInput.tsx @@ -34,7 +34,7 @@ export default function FloatingLabelInput({