Guest User

user-id

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