Guest User

Untitled

a guest
Jan 2nd, 2025
30
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use client';
  2.  
  3. import Image from "next/image";
  4. import { updateProfileInfo } from "../actions/actions";
  5. import { useRouter } from "next/navigation";
  6. import { updateFormSchema } from "../utils/validation";
  7. import { z } from "zod";
  8. import { useActionState, useState } from "react";
  9.  
  10. export default function EditProfileTemplate({ userData, setIsEditing }) {
  11.  
  12.     const [errors, setErrors] = useState({});
  13.     const notifySucess = () => toast("Profile Information updated successfully!", { type: "success" });
  14.     const router = useRouter();
  15.     const handleFormSubmit = async (prevState, formData) => {
  16.  
  17.         try {
  18.             const formValues = {
  19.                 name: formData.get("name"),
  20.                 email: formData.get("email"),
  21.                 address: formData.get("address"),
  22.                 city: formData.get("city"),
  23.             }
  24.  
  25.  
  26.             await updateFormSchema.parseAsync(formValues);
  27.  
  28.             const result = await updateProfileInfo(prevState, formValues);
  29.  
  30.             if (result.success === "ERROR") {
  31.                 console.log("Profile information update failed", result.error);
  32.                 return { ...prevState, error: result.error, status: "ERROR" };
  33.             }
  34.  
  35.             setIsEditing(false);
  36.             notifySucess();
  37.             return JSON.stringify({ ...prevState, status: "SUCCESS" });
  38.  
  39.         } catch (error) {
  40.  
  41.             if (error instanceof z.ZodError) {
  42.                 const fieldErrors = error.flatten().fieldErrors;
  43.  
  44.                 setErrors(fieldErrors);
  45.  
  46.                 return { ...prevState, error: "Validation failed", status: "ERROR" };
  47.             }
  48.  
  49.             return {
  50.                 ...prevState,
  51.                 error: "An unexpected error has occured",
  52.                 status: "ERROR"
  53.             }
  54.         }
  55.  
  56.     }
  57.  
  58.     const [state, formAction, isPending] = useActionState(handleFormSubmit, {
  59.         error: "",
  60.         status: "INITIAL"
  61.     })
  62.  
  63.  
  64.  
  65.     return (
  66.         <>
  67.             <div className="bg-white w-full justify-end flex flex-col gap-5 px-3 md:px-16 lg:px-28 md:flex-row text-[#161931]">
  68.                 <main className="w-full min-h-screen py-1 md:w-2/3 lg:w-3/4">
  69.                     <div className="p-2 md:p-4">
  70.                         <div className="w-full px-6 pb-8 mt-8 sm:max-w-xl sm:rounded-lg">
  71.                             <h2 className="pl-6 text-2xl font-bold sm:text-xl">Public Profile</h2>
  72.  
  73.                             <div className="grid max-w-2xl mx-auto mt-8">
  74.                                 <div className="flex flex-col items-center space-y-5 sm:flex-row sm:space-y-0">
  75.  
  76.                                     <Image
  77.                                         className="object-cover w-40 h-40 p-1 rounded-full ring-2 ring-indigo-300 dark:ring-indigo-500"
  78.                                         width={200}
  79.                                         height={200}
  80.                                         src={userData?.image}
  81.                                         alt="Bordered avatar" />
  82.  
  83.                                     <div className="flex flex-col space-y-5 sm:ml-8">
  84.                                         <button type="button"
  85.                                             className="py-3.5 px-7 text-base font-medium text-indigo-100 focus:outline-none bg-[#202142] rounded-lg border border-indigo-200 hover:bg-indigo-900 focus:z-10 focus:ring-4 focus:ring-indigo-200 ">
  86.                                             Change picture
  87.                                         </button>
  88.                                         <button type="button"
  89.                                             className="py-3.5 px-7 text-base font-medium text-indigo-900 focus:outline-none bg-white rounded-lg border border-indigo-200 hover:bg-indigo-100 hover:text-[#202142] focus:z-10 focus:ring-4 focus:ring-indigo-200 ">
  90.                                             Delete picture
  91.                                         </button>
  92.                                     </div>
  93.                                 </div>
  94.  
  95.                                 <form
  96.                                     action={formAction}
  97.                                     className="items-center mt-8 sm:mt-14 text-[#202142]">
  98.  
  99.                                     <div
  100.                                         className="flex flex-col items-center w-full mb-2 space-x-0 space-y-2 sm:flex-row sm:space-x-4 sm:space-y-0 sm:mb-6">
  101.                                         <div className="w-full">
  102.                                             <label htmlFor="first_name"
  103.                                                 className="block mb-2 text-sm font-medium text-indigo-900">
  104.                                                 Name</label>
  105.                                             <input
  106.                                                 type="text"
  107.                                                 id="name"
  108.                                                 name="name"
  109.                                                 className="bg-indigo-50 border border-indigo-300 text-indigo-900 text-sm rounded-lg focus:ring-indigo-500 focus:border-indigo-500 block w-full p-2.5 "
  110.                                                 placeholder="Your name"
  111.                                                 defaultValue={userData?.name}
  112.                                                 required
  113.                                             />
  114.                                         </div>
  115.                                         {errors.name && <p className="text-red-500">{errors.name}</p>}
  116.  
  117.                                     </div>
  118.  
  119.                                     <div className="mb-2 sm:mb-6">
  120.                                         <label htmlFor="email"
  121.                                             className="block mb-2 text-sm font-medium text-indigo-900">
  122.                                             Email</label>
  123.                                         <input
  124.                                             type="email"
  125.                                             id="email"
  126.                                             name="email"
  127.                                             className="bg-indigo-50 border border-indigo-300 text-indigo-900 text-sm rounded-lg focus:ring-indigo-500 focus:border-indigo-500 block w-full p-2.5 "
  128.                                             placeholder="[email protected]"
  129.                                             defaultValue={userData?.email}
  130.                                             required
  131.                                         />
  132.                                     </div>
  133.                                     {errors.name && <p className="text-red-500">{errors.email}</p>}
  134.  
  135.                                     <div className="mb-2 sm:mb-6">
  136.                                         <label htmlFor="profession"
  137.                                             className="block mb-2 text-sm font-medium text-indigo-900">Address</label>
  138.                                         <input
  139.                                             type="text"
  140.                                             id="address"
  141.                                             name="address"
  142.                                             className="bg-indigo-50 border border-indigo-300 text-indigo-900 text-sm rounded-lg focus:ring-indigo-500 focus:border-indigo-500 block w-full p-2.5 "
  143.                                             placeholder="Your address"
  144.                                             defaultValue={userData?.address}
  145.                                             required
  146.                                         />
  147.                                     </div>
  148.                                     {errors.name && <p className="text-red-500">{errors.address}</p>}
  149.  
  150.                                     <div className="mb-2 sm:mb-6">
  151.                                         <label htmlFor="email"
  152.                                             className="block mb-2 text-sm font-medium text-indigo-900">
  153.                                             City</label>
  154.                                         <input
  155.                                             type="text"
  156.                                             id="city"
  157.                                             name="city"
  158.                                             className="bg-indigo-50 border border-indigo-300 text-indigo-900 text-sm rounded-lg focus:ring-indigo-500 focus:border-indigo-500 block w-full p-2.5 "
  159.                                             placeholder="Your city"
  160.                                             defaultValue={userData?.city}
  161.                                             required
  162.                                         />
  163.                                     </div>
  164.                                     {errors.name && <p className="text-red-500">{errors.city}</p>}
  165.  
  166.                                     <div className="flex justify-end">
  167.                                         <button
  168.                                             disabled={isPending}
  169.                                             type="submit"
  170.                                             className="text-white bg-indigo-700  hover:bg-indigo-800 focus:ring-4 focus:outline-none focus:ring-indigo-300 font-medium rounded-lg text-sm w-full sm:w-auto px-5 py-2.5 text-center dark:bg-indigo-600 dark:hover:bg-indigo-700 dark:focus:ring-indigo-800">
  171.                                             {isPending ? "Saving..." : "Save"}
  172.                                         </button>
  173.                                     </div>
  174.  
  175.                                 </form>
  176.                             </div>
  177.                         </div>
  178.                     </div>
  179.                 </main>
  180.             </div>
  181.         </>
  182.     )
  183. }
Advertisement
Add Comment
Please, Sign In to add comment