Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import React, { Fragment, useEffect, useState } from "react";
- import { useForm } from "react-hook-form";
- import { zodResolver } from "@hookform/resolvers/zod";
- import * as z from "zod";
- import Page from "../../../components/Page";
- import { PhotoIcon, UserCircleIcon } from "@heroicons/react/24/solid";
- import HobbiesDropdown from "../../../components/DropdownMenu";
- import DayPicker from "../../../components/DayPicker";
- import {
- DEPARTMENT_NAME_QUERY,
- DepartmentName,
- } from "../../../gql/departmentQuery";
- import { useQuery } from "@apollo/client";
- import { useMutation } from "@apollo/client";
- import { EDIT_STAFF_MUTATION } from "../../../gql/staffMutation";
- import { UserI } from "../../../types";
- import { getUser } from "../../../utils/getUser";
- import { UseQueryResult, useQuery as reactUseQuery } from "react-query";
- import Upload from "../../../components/UploadAntDesign";
- import { useToasts } from "react-toast-notifications";
- import useStaffData from "../../../common/getLoggedInUserHook";
- import Avatar from "../../../components/Nav/components/Avatar";
- import { getPeoplePhotoUrl } from "../../../common/peoplePhoto";
- import { StaffData } from "../../../gql/staffQuery";
- import { BiPencil } from "react-icons/bi";
- import { useUser } from "../../../utils/userContext";
- export default function UpdateProfileForm() {
- const [selectedDepartment, setSelectedDepartment] = useState("");
- const [selectedTeam, setSelectedTeam] = useState("");
- const [action, setAction] = useState("");
- const azureUserData = useUser().userData;
- const { addToast } = useToasts();
- const handleDepartmentChange = (
- event: React.ChangeEvent<HTMLSelectElement>
- ) => {
- setSelectedDepartment(event.target.value);
- clearErrors("department");
- };
- const handleTeamChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
- setSelectedTeam(event.target.value);
- clearErrors("team");
- };
- const handlePurposeChange = (event: React.ChangeEvent<HTMLSelectElement>) => {
- setAction(event.target.value);
- };
- const { loading, error, data } = useQuery<DepartmentName>(
- DEPARTMENT_NAME_QUERY
- );
- console.log(data);
- const MAX_FILE_SIZE = 500000;
- const ACCEPTED_IMAGE_TYPES = [
- "image/jpeg",
- "image/jpg",
- "image/png",
- "image/webp",
- ];
- const formSchema = z.object({
- technology: z.string().nonempty("Technology is required"),
- dropDownMenu: z.array(z.string()).max(3, "You can only select up to 3 hobbies").optional(),
- });
- const {
- register,
- handleSubmit,
- formState: { errors },
- control,
- getValues,
- clearErrors,
- } = useForm({
- resolver: zodResolver(formSchema),
- });
- const [editStaff] = useMutation(EDIT_STAFF_MUTATION);
- const onSubmit = async (data: any) => {
- console.log("submit:", data);
- try{
- const response = await editStaff({
- variables: {
- modifyStaffId: editProfile?.staff?.id,
- technology: editProfile?.staff?.technology,
- hobbies: editProfile?.staff?.hobbies.map(hobby => hobby.icon).join(", "),
- imageLink: editProfile?.staff?.image,
- },
- });
- console.log("Add request response:", response.data);
- addToast("Profile Updated", {
- appearance: "success",
- autoDismiss: true,
- });
- } catch (e) {
- console.log(e);
- addToast("An error occurred", {
- appearance: "error",
- autoDismiss: true,
- });
- }
- };
- const dropDownMenu = getValues("dropDownMenu");
- const {data: meData, loading: meLoading, error: meError} = useStaffData();
- const [editProfile, setEditProfile] = useState<StaffData|undefined>(undefined);
- // sets the editProfile state to the current user data
- useEffect(() => {
- if ((editProfile === undefined || meLoading) && meData !== null){
- setEditProfile(meData);
- }
- }, [meLoading, meData]);
- return (
- <Page>
- {
- editProfile !== undefined? (
- <div className="grid place-items-center py-12">
- <div
- className="absolute inset-x-0 -top-40 -z-20 transform-gpu overflow-hidden blur-3xl sm:-top-80"
- aria-hidden="true"
- >
- <div
- className="relative left-[calc(50%-11rem)] aspect-[1155/678] w-[36.125rem] -translate-x-1/2 rotate-[30deg] bg-gradient-to-tr from-[#bfdbfe] to-[#2563eb] opacity-30 sm:left-[calc(50%-30rem)] sm:w-[72.1875rem]"
- style={{
- clipPath:
- "polygon(74.1% 44.1%, 100% 61.6%, 97.5% 26.9%, 85.5% 0.1%, 80.7% 2%, 72.5% 32.5%, 60.2% 62.4%, 52.4% 68.1%, 47.5% 58.3%, 45.2% 34.5%, 27.5% 76.7%, 0.1% 64.9%, 17.9% 100%, 27.6% 76.8%, 76.1% 97.7%, 74.1% 44.1%)",
- }}
- />
- </div>
- <div className="mx-auto max-w-2xl">
- <h1 className="text-4xl font-semibold tracking-tight text-text-primary sm:text-6xl animate__animated animate__slideInDown flex pl-4 justify-center">
- Edit Profile
- </h1>
- </div>
- <div className="py-12 w-9/12">
- <form
- className="bg-neutral shadow-sm ring-1 ring-gray-900/5 sm:rounded-xl"
- onSubmit={handleSubmit(onSubmit)}
- >
- <div className="px-4 py-6 sm:p-8">
- <div>
- <div className="mb-5">
- <div className="grid grid-cols-2 gap-x-28 gap-y-8">
- {/* <div className="flex flex-col gap-3">
- <h1 className="text-3xl font-bold text-text-primary">
- {meData?.staff?.name}
- </h1>
- <p className="text-text-primary">{meData?.staff?.role}</p>
- </div> */}
- </div>
- </div>
- <div className="col-span-full">
- <label htmlFor="photo" className="block text-sm font-medium leading-6 text-gray-900">
- Photo
- </label>
- <div className="flex flex-col">
- <div className="flex flex-row">
- <div className="mt-2 flex items-center gap-x-3 mb-5">
- <Avatar
- avatar={getPeoplePhotoUrl(meData?.staff?.image === undefined? "" : meData?.staff?.image, true) as Blob}
- loading={loading}
- size={50}
- user={meData?.staff}
- userLoading={loading}
- grayed={false}
- />
- <button
- type="button"
- className="rounded-md bg-white px-2.5 py-1.5 text-sm font-semibold text-gray-900 shadow-sm ring-1 ring-inset ring-gray-300 hover:bg-gray-50"
- >
- <BiPencil/>
- </button>
- </div>
- </div>
- <div className="grid grid-cols-2 gap-x-28 gap-y-8 z-40 pb-8">
- <label
- htmlFor="name"
- className="block text-sm font-medium leading-6 text-text-primary"
- >
- Employee's Name{" "}
- <span className="text-red-500">*</span>
- </label>
- <input
- type="text"
- name="name"
- id="name"
- disabled={true}
- value={editProfile?.staff.name}
- onChange={(e) => {
- setEditProfile({
- ...editProfile,
- staff: {
- ...editProfile?.staff,
- name: e.target.value,
- },
- });
- }}
- className="block w-full rounded-md bg-neutral-shadow border-0 py-1.5 text-text-primary shadow-sm ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-inset focus:ring-blue-600 sm:max-w-xs sm:text-sm sm:leading-6"
- placeholder="Enter Favourite Technology"
- />
- </div>
- <div className="grid grid-cols-2 gap-x-28 gap-y-8 z-40 pb-8">
- <label
- htmlFor="name"
- className="block text-sm font-medium leading-6 text-text-primary"
- >
- Employee's Job Title{" "}
- <span className="text-red-500">*</span>
- </label>
- <input
- type="text"
- name="role"
- id="role"
- disabled={true}
- value={editProfile?.staff.role}
- onChange={(e) => {
- setEditProfile({
- ...editProfile,
- staff: {
- ...editProfile?.staff,
- role: e.target.value,
- },
- });
- }}
- className="block w-full rounded-md bg-neutral-shadow border-0 py-1.5 text-text-primary shadow-sm ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-inset focus:ring-blue-600 sm:max-w-xs sm:text-sm sm:leading-6"
- placeholder="Enter Favourite Technology"
- />
- </div>
- <div className="grid grid-cols-2 gap-x-28 gap-y-8 z-40 pb-8">
- <label
- htmlFor="name"
- className="block text-sm font-medium leading-6 text-text-primary"
- >
- Employee's Job Start{" "}
- <span className="text-red-500">*</span>
- </label>
- <input
- type="text"
- name="jobStart"
- id="jobStart"
- disabled={true}
- value={editProfile?.staff.jobStart}
- onChange={(e) => {
- setEditProfile({
- ...editProfile,
- staff: {
- ...editProfile?.staff,
- jobStart: e.target.value,
- },
- });
- }}
- className="block w-full rounded-md bg-neutral-shadow border-0 py-1.5 text-text-primary shadow-sm ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-inset focus:ring-blue-600 sm:max-w-xs sm:text-sm sm:leading-6"
- placeholder="Enter Favourite Technology"
- />
- </div>
- </div>
- </div>
- <div className="grid grid-cols-2 gap-x-28 gap-y-8 z-40 pb-8">
- <label
- htmlFor="technology"
- className="block text-sm font-medium leading-6 text-text-primary"
- >
- Employee's Favourite Technology{" "}
- <span className="text-red-500">*</span>
- </label>
- <input
- {...register("technology")}
- type="text"
- name="technology"
- id="technology"
- value={editProfile?.staff.technology}
- onChange={(e) => {
- setEditProfile({
- ...editProfile,
- staff: {
- ...editProfile?.staff,
- technology: e.target.value,
- },
- });
- }}
- className="block w-full rounded-md bg-neutral-dimmed border-0 py-1.5 text-text-primary shadow-sm ring-1 ring-inset ring-gray-300 focus:ring-2 focus:ring-inset focus:ring-blue-600 sm:max-w-xs sm:text-sm sm:leading-6"
- placeholder="Enter Favourite Technology"
- />
- </div>
- {/* grayed out inputs with name and title */}
- <HobbiesDropdown
- register={register}
- errors={errors}
- formSchema={formSchema}
- control={control}
- selectedHobbies={editProfile?.staff.hobbies.map(hobby => hobby.icon).sort()}
- setSelectedHobbies={(hobbies: string[]) => {
- setEditProfile({
- ...editProfile,
- staff: {
- ...editProfile?.staff,
- hobbies: hobbies.map(hobby => {
- return {
- icon: hobby,
- name: hobby,
- }
- }),
- },
- });
- }}
- columns={2}
- />
- </div>
- </div>
- <div className="flex items-center justify-end gap-x-6 border-t border-gray-900/10 px-4 py-4 sm:px-8">
- <input
- type="reset"
- className="text-sm font-semibold leading-6 text-text-primary"
- value="Cancel"
- />
- <button
- type="submit"
- className="rounded-md bg-blue-600 px-3 py-2 text-sm font-semibold text-white shadow-sm hover:bg-blue-500 focus-visible:outline focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-blue-600"
- >
- Save
- </button>
- </div>
- </form>
- </div>
- <div
- className="absolute inset-x-0 top-[calc(100%-13rem)] -z-10 transform-gpu overflow-hidden blur-3xl sm:top-[calc(100%-30rem)]"
- aria-hidden="true"
- >
- <div
- className="relative left-[calc(50%+3rem)] aspect-[1155/678] w-[36.125rem] -translate-x-1/2 bg-gradient-to-tr from-[#bfdbfe] to-[#2563eb] opacity-30 sm:left-[calc(50%+36rem)] sm:w-[72.1875rem]"
- style={{
- clipPath:
- "polygon(74.1% 44.1%, 100% 61.6%, 97.5% 26.9%, 85.5% 0.1%, 80.7% 2%, 72.5% 32.5%, 60.2% 62.4%, 52.4% 68.1%, 47.5% 58.3%, 45.2% 34.5%, 27.5% 76.7%, 0.1% 64.9%, 17.9% 100%, 27.6% 76.8%, 76.1% 97.7%, 74.1% 44.1%)",
- }}
- />
- </div>
- </div>
- ) : (
- <div/>
- )
- }
- {/* Hero Section */}
- </Page>
- );
- }
Advertisement
Add Comment
Please, Sign In to add comment