From bd8e4fe321e4bc350da9fed42a0176437b0393d0 Mon Sep 17 00:00:00 2001 From: Seroxdesign Date: Thu, 21 Sep 2023 10:16:37 -0400 Subject: [PATCH] edit --- .../components/Player/Section/PlayerLinks.tsx | 108 +++++++++++++++++- .../web/components/Setup/SetupPlayerLinks.tsx | 19 ++- 2 files changed, 116 insertions(+), 11 deletions(-) diff --git a/packages/web/components/Player/Section/PlayerLinks.tsx b/packages/web/components/Player/Section/PlayerLinks.tsx index 4c42940e..914c368a 100644 --- a/packages/web/components/Player/Section/PlayerLinks.tsx +++ b/packages/web/components/Player/Section/PlayerLinks.tsx @@ -20,6 +20,7 @@ import { Player, useAddPlayerLinkMutation, useDeletePlayerLinkMutation, + useUpdatePlayerLinkMutation, } from 'graphql/autogen/types'; import { getPlayerLinks } from 'graphql/queries/player'; import React, { useCallback, useEffect, useState } from 'react'; @@ -34,6 +35,7 @@ type Props = { isOwnProfile?: boolean; editing?: boolean; admin?: boolean; + switchToEdit?: any }; export interface PlayerLinkFormInputs { @@ -46,9 +48,7 @@ export const AddPlayerLink: React.FC<{ player?: Player; metadata?: BoxMetadata; setMetadata?: (d: BoxMetadata) => void; - editId?: string; - editData?: PlayerLinkFormInputs; -}> = ({ player, editId, editData }) => { +}> = ({ player }) => { const { register, @@ -140,6 +140,7 @@ export const PlayerLinks: React.FC = ({ isOwnProfile, editing, admin, + switchToEdit }) => { const [links, setLinks] = useState([]); const [, deleteLink] = useDeletePlayerLinkMutation(); @@ -220,8 +221,8 @@ export const PlayerLinks: React.FC = ({ - )} @@ -231,4 +232,99 @@ export const PlayerLinks: React.FC = ({ ); -}; \ No newline at end of file +}; + +export const EditPlayerLink: React.FC<{ + player?: Player; + metadata?: BoxMetadata; + setMetadata?: (d: BoxMetadata) => void; + editId?: string; + editData?: PlayerLinkFormInputs; +}> = ({ player, editId, editData }) => { + + const { + register, + formState: { errors }, + handleSubmit, + getValues + } = useForm({ + mode: 'onTouched', + }); + + const [, addLink] = useAddPlayerLinkMutation(); + const [, updateLink] = useUpdatePlayerLinkMutation(); + + const onSubmit = useCallback( + async (link: PlayerLinkFormInputs) => { + if (!editId) return + const playerLink = { id: editId, name: link.name || link.type, url: link.url, type: link.type}; + const { error } = await updateLink(playerLink); + + if (error) { + throw new Error(`Unable to add link. Error: ${error}`); + } + }, + [updateLink, player?.id], + ); + + return ( + + + + + + + + + + + + + Edit Link + + + + ); +}; diff --git a/packages/web/components/Setup/SetupPlayerLinks.tsx b/packages/web/components/Setup/SetupPlayerLinks.tsx index b54e6e79..6382b535 100644 --- a/packages/web/components/Setup/SetupPlayerLinks.tsx +++ b/packages/web/components/Setup/SetupPlayerLinks.tsx @@ -10,7 +10,7 @@ import { } from '@metafam/ds'; import { Player, useUpsertDeworkProfileMutation } from 'graphql/autogen/types'; import React, { useState } from 'react'; -import { AddPlayerLink } from 'components/Player/Section/PlayerLinks'; +import { AddPlayerLink, EditPlayerLink } from 'components/Player/Section/PlayerLinks'; import { PlayerLinks } from 'components/Player/Section/PlayerLinks'; export const SetupPlayerLinks: React.FC<{ @@ -22,10 +22,17 @@ export const SetupPlayerLinks: React.FC<{ const toast = useToast(); const [linkToEdit, setLinkToEdit] = useState(); const [role, setRole] = useState('view'); + const [linkId, setLinkId] = useState(''); + + const handleSetRole = (newRole: string, id?: string) => { + setRole(newRole) + setLinkId(id || '') + } return ( <> - + + ); }; @@ -33,10 +40,12 @@ export const SetupPlayerLinks: React.FC<{ const PlayerLinksView: React.FC<{ role: string, player: Player -}> = ({role, player}) => { + setRole: any +}> = ({role, player, setRole}) => { const currentView = { - view: , - add: + view: , + add: , + edit: }[role] return <> {currentView}