Guest User

org-id

a guest
Aug 2nd, 2024
54
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. export const Route = createFileRoute("/dashboard/organization/$organizationId")(
  2.   {
  3.     loader: ({ params }) => ({ organizationId: params.organizationId }),
  4.     component: OrganizationDetails,
  5.   },
  6. );
  7.  
  8. function OrganizationDetails() {
  9.   const navigate = useNavigate();
  10.   const { organizationId } = Route.useParams();
  11.   const queryClient = useQueryClient();
  12.  
  13.   const {
  14.     data: organizationData,
  15.     isLoading,
  16.     isError,
  17.     error,
  18.   } = useQuery<Organization | null>({
  19.     queryKey: ["organization", organizationId],
  20.     queryFn: async () => {
  21.       const response = await fetch(
  22.         `${ADMIN_ORG_DETAILS}?organizationId=${organizationId}`,
  23.         {
  24.           credentials: "include",
  25.         },
  26.       );
  27.       if (!response.ok) {
  28.         throw new Error("Network response was not ok");
  29.       }
  30.       const data = await response.json();
  31.       return data || null;
  32.     },
  33.   });
  34.  
  35.   const toggleOrganizationStatus = useMutation({
  36.     mutationFn: (isActive: boolean) =>
  37.       fetch(
  38.         `${isActive ? ADMIN_INACTIVE_ORG : ADMIN_ACTIVE_ORG}?organizationId=${organizationId}`,
  39.         {
  40.           method: "PUT",
  41.           headers: { "Content-Type": "application/json" },
  42.           credentials: "include",
  43.         },
  44.       ),
  45.     onSuccess: async () => {
  46.       await queryClient.invalidateQueries({
  47.         queryKey: ["organizations", organizationId],
  48.       });
  49.     },
  50.   });
  51.  
  52.   if (isLoading) return <div>Laddar...</div>;
  53.   if (isError) return <div>Ett fel uppstod: {(error as Error).message}</div>;
  54.   if (!organizationData) return <div>Organisation hittades inte</div>;
  55.  
  56.   const { users, ...organization } = organizationData;
  57.    
  58.   const handleToggleStatus = () => {
  59.     toggleOrganizationStatus.mutate(organization.isActive);
  60.   };
  61.  
  62.   return (
  63.             <div>
  64.               <h3 className="font-semibold">Aktiv</h3>
  65.               <div className="flex items-center space-x-2">
  66.                 <Switch
  67.                   id="organization-active"
  68.                   checked={organization.isActive}
  69.                   onCheckedChange={handleToggleStatus}
  70.                   disabled={isLoading}
  71.                 />
  72.                 <Label htmlFor="organization-active">
  73.                   {organization.isActive ? "Aktiv" : "Inaktiv"}
  74.                 </Label>
  75.               </div>
  76.             </div>
  77.           </div>
  78.   );
  79. }
  80.  
Add Comment
Please, Sign In to add comment