From 5cf05cedd8cbd3cd63642caef6bb80e52190bd2a Mon Sep 17 00:00:00 2001 From: vidvidvid Date: Sun, 17 Oct 2021 18:52:49 +0200 Subject: [PATCH] fix displaying the update after saving --- .../components/Player/Section/PlayerHero.tsx | 77 ++++++++++--------- 1 file changed, 42 insertions(+), 35 deletions(-) diff --git a/packages/web/components/Player/Section/PlayerHero.tsx b/packages/web/components/Player/Section/PlayerHero.tsx index 559a55a9..8b2ca08d 100644 --- a/packages/web/components/Player/Section/PlayerHero.tsx +++ b/packages/web/components/Player/Section/PlayerHero.tsx @@ -20,7 +20,7 @@ import { EditProfileForm } from 'components/EditProfileForm'; import { PlayerAvatar } from 'components/Player/PlayerAvatar'; import { PlayerFragmentFragment } from 'graphql/autogen/types'; import { useUser } from 'lib/hooks'; -import React, { useMemo } from 'react'; +import React, { useEffect, useState } from 'react'; import { FaClock, FaGlobe } from 'react-icons/fa'; import { getPlayerTimeZoneDisplay } from 'utils/dateHelpers'; import { getPlayerDescription, getPlayerName } from 'utils/playerHelpers'; @@ -33,20 +33,33 @@ import { PlayerHeroTile } from './PlayerHeroTile'; const MAX_BIO_LENGTH = 240; type Props = { player: PlayerFragmentFragment; isOwnProfile: boolean }; -type AvailabilityProps = { player: PlayerFragmentFragment }; +type AvailabilityProps = { availabilityHours?: number }; type TimeZoneDisplayProps = { timeZone?: string; offset?: string }; export const PlayerHero: React.FC = ({ player, isOwnProfile }) => { const description = getPlayerDescription(player); - const [show, setShow] = React.useState(description.length <= MAX_BIO_LENGTH); + const [show, setShow] = useState(description.length <= MAX_BIO_LENGTH); const { isOpen, onOpen, onClose } = useDisclosure(); - const { timeZone, offset } = useMemo( - () => getPlayerTimeZoneDisplay(player.timezone), - [player.timezone], - ); + const [timeZone, setTimeZone] = useState(''); + const [offset, setOffset] = useState(''); + const [availabilityHours, setAvailabilityHours] = useState(0); + const { user } = useUser(); + useEffect(() => { + const person = isOwnProfile ? user?.player : player; + + if (person) { + const timeDisplay = getPlayerTimeZoneDisplay(person.timezone); + if (timeDisplay.timeZone) setTimeZone(timeDisplay.timeZone); + if (timeDisplay.offset) setOffset(timeDisplay.offset); + + const hours = person.availability_hours; + if (hours) setAvailabilityHours(hours); + } + }, [user, player, isOwnProfile]); + return ( {isOwnProfile && ( @@ -87,23 +100,25 @@ export const PlayerHero: React.FC = ({ player, isOwnProfile }) => { - - {show - ? description - : `${description.substring(0, MAX_BIO_LENGTH - 9)}…`} - {description.length > MAX_BIO_LENGTH && ( - setShow((s) => !s)} - pl={1} - > - Read {show ? 'less' : 'more'} - - )} - + + + {show + ? description + : `${description.substring(0, MAX_BIO_LENGTH - 9)}…`} + {description.length > MAX_BIO_LENGTH && ( + setShow((s) => !s)} + pl={1} + > + Read {show ? 'less' : 'more'} + + )} + + @@ -118,19 +133,13 @@ export const PlayerHero: React.FC = ({ player, isOwnProfile }) => { He */} - - {player?.profile_cache?.description && ( - - {player?.profile_cache?.description} - - )} {/* www.mycoolportfolio.com */} - + @@ -196,15 +205,13 @@ export const PlayerHero: React.FC = ({ player, isOwnProfile }) => { ); }; -const Availability: React.FC = ({ - player: { availability_hours }, -}) => ( +const Availability: React.FC = ({ availabilityHours }) => ( - {`${availability_hours || '0'} h/week`} + {`${availabilityHours || '0'} h/week`} );