sync UI with platform link changes
This commit is contained in:
		| @@ -25,7 +25,7 @@ import { | |||||||
| } from "../../forms/selectors"; | } from "../../forms/selectors"; | ||||||
| import { GenericInput, InfoInputLine } from "~/components/forms/textInput"; | import { GenericInput, InfoInputLine } from "~/components/forms/textInput"; | ||||||
| import { PriceIcon } from "~/prices/Icons"; | import { PriceIcon } from "~/prices/Icons"; | ||||||
| import { type Dispatch, type SetStateAction, useState } from "react"; | import { type Dispatch, type SetStateAction, useState, useEffect } from "react"; | ||||||
| import { | import { | ||||||
|   type UseFormReturn, |   type UseFormReturn, | ||||||
|   FormProvider, |   FormProvider, | ||||||
| @@ -82,7 +82,8 @@ const LinkModal = ({ | |||||||
|   isOpen: boolean; |   isOpen: boolean; | ||||||
|   setOpen: Dispatch<SetStateAction<boolean>>; |   setOpen: Dispatch<SetStateAction<boolean>>; | ||||||
| }) => { | }) => { | ||||||
|   const { register } = useForm<PlatformLink>(); |   const { setValue, getValues } = useFormContext<ResourceUpdateInput>(); | ||||||
|  |   const { register, handleSubmit } = useForm<PlatformLink>(); | ||||||
|   const platformTypeOptions = [ |   const platformTypeOptions = [ | ||||||
|     { |     { | ||||||
|       label: "Website", |       label: "Website", | ||||||
| @@ -102,6 +103,14 @@ const LinkModal = ({ | |||||||
|     }, |     }, | ||||||
|   ]; |   ]; | ||||||
|  |  | ||||||
|  |   const onSubmit = (data: PlatformLink) => { | ||||||
|  |     const values = getValues().platform_links ?? []; | ||||||
|  |  | ||||||
|  |     values.push(data); | ||||||
|  |     setValue("platform_links", values); | ||||||
|  |     setOpen(false); | ||||||
|  |   }; | ||||||
|  |  | ||||||
|   return ( |   return ( | ||||||
|     <Modal |     <Modal | ||||||
|       style={{ |       style={{ | ||||||
| @@ -143,7 +152,14 @@ const LinkModal = ({ | |||||||
|           </button> |           </button> | ||||||
|         </section> |         </section> | ||||||
|  |  | ||||||
|         <div className="space-y-4 p-4"> |         <form | ||||||
|  |           onSubmit={(event) => { | ||||||
|  |             handleSubmit(onSubmit)(event).catch((error) => { | ||||||
|  |               console.error(error); | ||||||
|  |             }); | ||||||
|  |           }} | ||||||
|  |           className="space-y-4 p-4" | ||||||
|  |         > | ||||||
|           <DropdownSelector |           <DropdownSelector | ||||||
|             options={platformTypeOptions} |             options={platformTypeOptions} | ||||||
|             label="Type" |             label="Type" | ||||||
| @@ -155,7 +171,15 @@ const LinkModal = ({ | |||||||
|             type="text" |             type="text" | ||||||
|             details={register("link", { required: true })} |             details={register("link", { required: true })} | ||||||
|           /> |           /> | ||||||
|         </div> |           <section className="py-4"> | ||||||
|  |             <button | ||||||
|  |               type="submit" | ||||||
|  |               className="mx-auto block w-fit rounded-md border border-neutral-900 bg-yellow-200 px-4 py-2 align-middle font-semibold shadow shadow-black/50 duration-200 ease-out hover:bg-yellow-300 hover:shadow-md" | ||||||
|  |             > | ||||||
|  |               <span className="hidden sm:inline-block">Create</span> | ||||||
|  |             </button> | ||||||
|  |           </section> | ||||||
|  |         </form> | ||||||
|       </div> |       </div> | ||||||
|     </Modal> |     </Modal> | ||||||
|   ); |   ); | ||||||
| @@ -165,10 +189,30 @@ const LinkModal = ({ | |||||||
|  * Contains the input fields for editing the links for a resource |  * Contains the input fields for editing the links for a resource | ||||||
|  * @returns |  * @returns | ||||||
|  */ |  */ | ||||||
| const ResourceLinkSubForm = ({ links }: { links: PlatformLink[] }) => { | const ResourceLinkSubForm = () => { | ||||||
|   const { register } = useFormContext<ResourceUpdateInput>(); |   const { setValue, getValues, watch } = useFormContext<ResourceUpdateInput>(); | ||||||
|   const [linkModalOpen, setLinkModalOpen] = useState(false); |   const [linkModalOpen, setLinkModalOpen] = useState(false); | ||||||
|   const [selectedLinks, setSelectedLinks] = useState(links); |   const [selectedLinks, setSelectedLinks] = useState<PlatformLink[]>( | ||||||
|  |     getValues().platform_links ?? [] | ||||||
|  |   ); | ||||||
|  |  | ||||||
|  |   useEffect(() => { | ||||||
|  |     watch((value, { name }) => { | ||||||
|  |       if (name === "platform_links") { | ||||||
|  |         const validLinks = value.platform_links?.filter((value) => { | ||||||
|  |           return value?.link && value?.platform; | ||||||
|  |         }) as unknown as PlatformLink[]; | ||||||
|  |         setSelectedLinks(validLinks); | ||||||
|  |       } | ||||||
|  |     }); | ||||||
|  |   }, [watch]); | ||||||
|  |  | ||||||
|  |   const removeLink = (key: number) => { | ||||||
|  |     const newLinks = [...selectedLinks]; | ||||||
|  |     newLinks.splice(key, 1); | ||||||
|  |  | ||||||
|  |     setValue("platform_links", newLinks); | ||||||
|  |   }; | ||||||
|  |  | ||||||
|   return ( |   return ( | ||||||
|     <div className="mx-4"> |     <div className="mx-4"> | ||||||
| @@ -197,6 +241,9 @@ const ResourceLinkSubForm = ({ links }: { links: PlatformLink[] }) => { | |||||||
|                 <PlatformLinkButton platformLink={link} /> |                 <PlatformLinkButton platformLink={link} /> | ||||||
|               </span> |               </span> | ||||||
|               <button |               <button | ||||||
|  |                 onClick={() => { | ||||||
|  |                   removeLink(index); | ||||||
|  |                 }} | ||||||
|                 type="button" |                 type="button" | ||||||
|                 className="my-auto h-9 w-9 grow-0 rounded-xl border border-red-100 bg-red-300 p-1 hover:bg-red-500" |                 className="my-auto h-9 w-9 grow-0 rounded-xl border border-red-100 bg-red-300 p-1 hover:bg-red-500" | ||||||
|               > |               > | ||||||
| @@ -397,8 +444,7 @@ const ResourceForm = ({ | |||||||
|       ) : undefined} |       ) : undefined} | ||||||
|       <form className="mx-auto flex max-w-2xl flex-col flex-col-reverse py-1 sm:flex-row sm:divide-x sm:py-4"> |       <form className="mx-auto flex max-w-2xl flex-col flex-col-reverse py-1 sm:flex-row sm:divide-x sm:py-4"> | ||||||
|         <div className="my-5 mr-4 flex flex-col text-lg font-bold"> |         <div className="my-5 mr-4 flex flex-col text-lg font-bold"> | ||||||
|           <ResourceLinkSubForm links={resource?.platform_links ?? []} />{" "} |           <ResourceLinkSubForm /> {/** //resource={resource} /> */} | ||||||
|           {/** //resource={resource} /> */} |  | ||||||
|         </div> |         </div> | ||||||
|         <div> |         <div> | ||||||
|           <h1 className="mx-4 mb-2 border-b border-neutral-400 text-xl font-bold sm:hidden"> |           <h1 className="mx-4 mb-2 border-b border-neutral-400 text-xl font-bold sm:hidden"> | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Brandon Egger
					Brandon Egger