From b329d2b27a0be30e21a428c595040793d0e8c655 Mon Sep 17 00:00:00 2001 From: Justin Edmund Date: Fri, 23 Dec 2022 15:19:14 -0800 Subject: [PATCH] Updates to Select component Use `value` instead of `defaultValue` and properly control it --- components/FilterBar/index.tsx | 14 +-- components/JobDropdown/index.tsx | 4 +- components/JobSkillSearchFilterBar/index.tsx | 4 +- components/Party/index.tsx | 89 ++++++++------------ components/RaidDropdown/index.tsx | 41 ++++----- components/Select/index.tsx | 34 +++++--- 6 files changed, 84 insertions(+), 102 deletions(-) diff --git a/components/FilterBar/index.tsx b/components/FilterBar/index.tsx index edd28eca..dfaec65b 100644 --- a/components/FilterBar/index.tsx +++ b/components/FilterBar/index.tsx @@ -32,11 +32,6 @@ const FilterBar = (props: Props) => { const [recencyOpen, setRecencyOpen] = useState(false) const [elementOpen, setElementOpen] = useState(false) - // Set up refs for filter dropdowns - const elementSelect = React.createRef() - const raidSelect = React.createRef() - const recencySelect = React.createRef() - // Set up classes object for showing shadow on scroll const classes = classNames({ FilterBar: true, @@ -69,9 +64,9 @@ const FilterBar = (props: Props) => {
{props.children} diff --git a/components/JobDropdown/index.tsx b/components/JobDropdown/index.tsx index 9a4cd18c..7763bc3b 100644 --- a/components/JobDropdown/index.tsx +++ b/components/JobDropdown/index.tsx @@ -102,11 +102,11 @@ const JobDropdown = React.forwardRef( return ( diff --git a/components/Party/index.tsx b/components/Party/index.tsx index 6fcc15b9..dcaeab91 100644 --- a/components/Party/index.tsx +++ b/components/Party/index.tsx @@ -25,18 +25,6 @@ interface Props { } const Party = (props: Props) => { - // Cookies - const cookie = getCookie('account') - const accountData: AccountCookie = cookie - ? JSON.parse(cookie as string) - : null - - const headers = useMemo(() => { - return accountData - ? { headers: { Authorization: `Bearer ${accountData.token}` } } - : {} - }, [accountData]) - // Set up router const router = useRouter() @@ -55,12 +43,13 @@ const Party = (props: Props) => { async function createParty(extra: boolean = false) { let body = { party: { - ...(accountData && { user_id: accountData.userId }), extra: extra, }, } - return await api.endpoints.parties.create(body, headers) + console.log(body) + + return await api.endpoints.parties.create(body) } // Methods: Updating the party's details @@ -68,13 +57,9 @@ const Party = (props: Props) => { appState.party.extra = event.target.checked if (party.id) { - api.endpoints.parties.update( - party.id, - { - party: { extra: event.target.checked }, - }, - headers - ) + api.endpoints.parties.update(party.id, { + party: { extra: event.target.checked }, + }) } } @@ -86,17 +71,13 @@ const Party = (props: Props) => { ) { if (appState.party.id) api.endpoints.parties - .update( - appState.party.id, - { - party: { - name: name, - description: description, - raid_id: raid?.id, - }, + .update(appState.party.id, { + party: { + name: name, + description: description, + raid_id: raid?.id, }, - headers - ) + }) .then(() => { appState.party.name = name appState.party.description = description @@ -110,7 +91,7 @@ const Party = (props: Props) => { function deleteTeam(event: React.MouseEvent) { if (appState.party.editable && appState.party.id) { api.endpoints.parties - .destroy({ id: appState.party.id, params: headers }) + .destroy({ id: appState.party.id }) .then(() => { // Push to route router.push('/') @@ -131,26 +112,26 @@ const Party = (props: Props) => { } // Methods: Storing party data - const storeParty = function (party: Party) { + const storeParty = function (team: Party) { // Store the important party and state-keeping values - appState.party.name = party.name - appState.party.description = party.description - appState.party.raid = party.raid - appState.party.updated_at = party.updated_at - appState.party.job = party.job - appState.party.jobSkills = party.job_skills + appState.party.name = team.name + appState.party.description = team.description + appState.party.raid = team.raid + appState.party.updated_at = team.updated_at + appState.party.job = team.job + appState.party.jobSkills = team.job_skills - appState.party.id = party.id - appState.party.extra = party.extra - appState.party.user = party.user - appState.party.favorited = party.favorited - appState.party.created_at = party.created_at - appState.party.updated_at = party.updated_at + appState.party.id = team.id + appState.party.extra = team.extra + appState.party.user = team.user + appState.party.favorited = team.favorited + appState.party.created_at = team.created_at + appState.party.updated_at = team.updated_at // Populate state - storeCharacters(party.characters) - storeWeapons(party.weapons) - storeSummons(party.summons) + storeCharacters(team.characters) + storeWeapons(team.weapons) + storeSummons(team.summons) } const storeCharacters = (list: Array) => { @@ -256,13 +237,11 @@ const Party = (props: Props) => { {navigation}
{currentGrid()}
- { - - } +
) } diff --git a/components/RaidDropdown/index.tsx b/components/RaidDropdown/index.tsx index fc33b757..f575b455 100644 --- a/components/RaidDropdown/index.tsx +++ b/components/RaidDropdown/index.tsx @@ -42,7 +42,7 @@ const RaidDropdown = React.forwardRef( // Set up local states for storing raids const [open, setOpen] = useState(false) - const [currentRaid, setCurrentRaid] = useState() + const [currentRaid, setCurrentRaid] = useState(undefined) const [raids, setRaids] = useState() const [sortedRaids, setSortedRaids] = useState() @@ -78,7 +78,7 @@ const RaidDropdown = React.forwardRef( useEffect(() => { if (raids && props.currentRaid) { const raid = raids.find((raid) => raid.slug === props.currentRaid) - setCurrentRaid(raid) + if (raid) setCurrentRaid(raid) } }, [raids, props.currentRaid]) @@ -100,13 +100,12 @@ const RaidDropdown = React.forwardRef( sortedRaids[index].length > 0 && sortedRaids[index] .sort((a, b) => a.element - b.element) - .map((item, i) => { - return ( - - {item.name[locale]} - - ) - }) + .map((item, i) => ( + + {item.name[locale]} + + )) + return ( ( } return ( - + + + ) } ) diff --git a/components/Select/index.tsx b/components/Select/index.tsx index 8a0dd7f4..99afae29 100644 --- a/components/Select/index.tsx +++ b/components/Select/index.tsx @@ -1,4 +1,4 @@ -import React from 'react' +import React, { useEffect, useState } from 'react' import * as RadixSelect from '@radix-ui/react-select' import classNames from 'classnames' @@ -7,26 +7,34 @@ import ArrowIcon from '~public/icons/Arrow.svg' import './index.scss' // Props -interface Props { +interface Props + extends React.DetailedHTMLProps< + React.SelectHTMLAttributes, + HTMLSelectElement + > { open: boolean - defaultValue?: string | number placeholder?: string trigger?: React.ReactNode children?: React.ReactNode onClick?: () => void - onChange?: (value: string) => void + onValueChange?: (value: string) => void triggerClass?: string } -const Select = React.forwardRef(function useFieldSet( - props, - ref -) { +const Select = (props: Props) => { + const [value, setValue] = useState('') + + useEffect(() => { + if (props.value) setValue(`${props.value}`) + }, [props.value]) + + function onValueChange(newValue: string) { + setValue(`${newValue}`) + if (props.onValueChange) props.onValueChange(newValue) + } + return ( - + (function useFieldSet( ) -}) +} export default Select