Skip to content

Commit

Permalink
no untouched, they're all touched
Browse files Browse the repository at this point in the history
  • Loading branch information
brekk committed Dec 16, 2021
1 parent faf1b58 commit 701b7e3
Showing 1 changed file with 13 additions and 22 deletions.
35 changes: 13 additions & 22 deletions components/user/Tabs/SettingsContent.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -33,39 +33,32 @@ const getSocialChoice = (

export default function SettingsContent({ authedUser }: Props) {
const $router = useRouter()
const $graffiti = useField(FIELDS.graffiti)
const $graffiti = useField({
...FIELDS.graffiti,
defaultValue: authedUser.graffiti,
})
const $email = useField({ ...FIELDS.email, defaultValue: authedUser.email })
const $social = useField({
...FIELDS.social,
defaultValue: authedUser.discord || authedUser.telegram,
})
const $country = useField(FIELDS.country)
const $country = useField({
...FIELDS.country,
defaultValue: authedUser.country_code,
})
const [$error, $setError] = useState<string>(UNSET)
const [$loaded, $setLoaded] = useState<boolean>(!!authedUser)
const testInvalid = useCallback(() => {
const noEmail = !$email?.touched
const noGraffiti = !$graffiti?.touched
const noSocial = !$social?.touched
const noCountry = !$country?.touched
const untouched = noEmail || noGraffiti || noSocial || noCountry
const invalid =
!$email?.valid || !$graffiti?.valid || !$social?.valid || !$country?.valid

if (invalid || untouched) {
if (untouched) {
$setError('Please fill out all fields')
if (noEmail) $email?.setTouched(true)
if (noGraffiti) $graffiti?.setTouched(true)
if (noSocial) $social?.setTouched(true)
if (noCountry) $country?.setTouched(true)
} else {
$setError('Please correct the invalid fields below')
}
if (invalid) {
$setError('Please correct the invalid fields below')
scrollUp()
} else {
$setError(UNSET)
}
return invalid || untouched
return invalid
}, [$email, $graffiti, $social, $country])

const { choice: socialChoice, value: socialValue } =
Expand Down Expand Up @@ -125,17 +118,15 @@ export default function SettingsContent({ authedUser }: Props) {
{$email && (
<TextField {...$email} defaultValue={authedUser.email} disabled />
)}
{$graffiti && (
<TextField {...$graffiti} defaultValue={authedUser.graffiti} />
)}
{$graffiti && <TextField {...$graffiti} />}
{$social && (
<TextField
{...$social}
choice={socialChoice}
defaultValue={socialValue}
/>
)}
{$country && <Select {...$country} value={authedUser.country_code} />}
{$country && <Select {...$country} />}
<Button className="mt-8" onClick={update}>
Save
</Button>
Expand Down

0 comments on commit 701b7e3

Please sign in to comment.