Bday modal tweaks (#3252)

* Smol tweaks to bday dialog

* Juse use existing DateInput for now

* Remove unused code

* Remove passed-in prefs

* Adjust load state

* Revert "Adjust load state"

This reverts commit 802459fd044b380ccc4f96432af416996219a0de.

* Fix type error
This commit is contained in:
Eric Bailey 2024-03-19 09:57:14 -05:00 committed by GitHub
parent 4de8e8fa14
commit 8ac5144a58
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 44 additions and 46 deletions

View file

@ -1,48 +1,64 @@
import React from 'react' import React from 'react'
import {useLingui} from '@lingui/react' import {useLingui} from '@lingui/react'
import {Trans, msg} from '@lingui/macro' import {Trans, msg} from '@lingui/macro'
import {View} from 'react-native'
import * as Dialog from '#/components/Dialog' import * as Dialog from '#/components/Dialog'
import {Text} from '../Typography' import {Text} from '../Typography'
import {DateInput} from '#/view/com/util/forms/DateInput' import {DateInput} from '#/view/com/util/forms/DateInput'
import {logger} from '#/logger' import {logger} from '#/logger'
import { import {
usePreferencesQuery,
usePreferencesSetBirthDateMutation, usePreferencesSetBirthDateMutation,
UsePreferencesQueryResponse, UsePreferencesQueryResponse,
} from '#/state/queries/preferences' } from '#/state/queries/preferences'
import {Button, ButtonText} from '../Button' import {Button, ButtonIcon, ButtonText} from '../Button'
import {atoms as a, useTheme} from '#/alf' import {atoms as a, useTheme} from '#/alf'
import {ErrorMessage} from '#/view/com/util/error/ErrorMessage' import {ErrorMessage} from '#/view/com/util/error/ErrorMessage'
import {cleanError} from '#/lib/strings/errors' import {cleanError} from '#/lib/strings/errors'
import {ActivityIndicator, View} from 'react-native'
import {isIOS, isWeb} from '#/platform/detection' import {isIOS, isWeb} from '#/platform/detection'
import {Loader} from '#/components/Loader'
export function BirthDateSettingsDialog({ export function BirthDateSettingsDialog({
control, control,
preferences,
}: { }: {
control: Dialog.DialogControlProps control: Dialog.DialogControlProps
preferences: UsePreferencesQueryResponse | undefined
}) { }) {
const t = useTheme()
const {_} = useLingui() const {_} = useLingui()
const {isPending, isError, error, mutateAsync} = const {isLoading, error, data: preferences} = usePreferencesQuery()
usePreferencesSetBirthDateMutation()
return ( return (
<Dialog.Outer control={control}> <Dialog.Outer control={control}>
<Dialog.Handle /> <Dialog.Handle />
<Dialog.ScrollableInner label={_(msg`My Birthday`)}> <Dialog.ScrollableInner label={_(msg`My Birthday`)}>
{preferences && !isPending ? ( <View style={[a.gap_sm, a.pb_lg]}>
<BirthdayInner <Text style={[a.text_2xl, a.font_bold]}>
control={control} <Trans>My Birthday</Trans>
preferences={preferences} </Text>
isError={isError} <Text style={[a.text_md, t.atoms.text_contrast_medium]}>
error={error} <Trans>This information is not shared with other users.</Trans>
setBirthDate={mutateAsync} </Text>
</View>
{isLoading ? (
<Loader size="xl" />
) : error || !preferences ? (
<ErrorMessage
message={
error?.toString() ||
_(
msg`We were unable to load your birth date preferences. Please try again.`,
)
}
style={[a.rounded_sm]}
/> />
) : ( ) : (
<ActivityIndicator size="large" style={a.my_5xl} /> <BirthdayInner control={control} preferences={preferences} />
)} )}
<Dialog.Close />
</Dialog.ScrollableInner> </Dialog.ScrollableInner>
</Dialog.Outer> </Dialog.Outer>
) )
@ -51,20 +67,18 @@ export function BirthDateSettingsDialog({
function BirthdayInner({ function BirthdayInner({
control, control,
preferences, preferences,
isError,
error,
setBirthDate,
}: { }: {
control: Dialog.DialogControlProps control: Dialog.DialogControlProps
preferences: UsePreferencesQueryResponse preferences: UsePreferencesQueryResponse
isError: boolean
error: unknown
setBirthDate: (args: {birthDate: Date}) => Promise<unknown>
}) { }) {
const {_} = useLingui() const {_} = useLingui()
const [date, setDate] = React.useState(preferences.birthDate || new Date()) const [date, setDate] = React.useState(preferences.birthDate || new Date())
const t = useTheme() const {
isPending,
isError,
error,
mutateAsync: setBirthDate,
} = usePreferencesSetBirthDateMutation()
const hasChanged = date !== preferences.birthDate const hasChanged = date !== preferences.birthDate
const onSave = React.useCallback(async () => { const onSave = React.useCallback(async () => {
@ -74,21 +88,13 @@ function BirthdayInner({
await setBirthDate({birthDate: date}) await setBirthDate({birthDate: date})
} }
control.close() control.close()
} catch (e) { } catch (e: any) {
logger.error(`setBirthDate failed`, {message: e}) logger.error(`setBirthDate failed`, {message: e.message})
} }
}, [date, setBirthDate, control, hasChanged]) }, [date, setBirthDate, control, hasChanged])
return ( return (
<View style={a.gap_lg} testID="birthDateSettingsDialog"> <View style={a.gap_lg} testID="birthDateSettingsDialog">
<View style={[a.gap_sm]}>
<Text style={[a.text_2xl, a.font_bold]}>
<Trans>My Birthday</Trans>
</Text>
<Text style={t.atoms.text_contrast_medium}>
<Trans>This information is not shared with other users.</Trans>
</Text>
</View>
<View style={isIOS && [a.w_full, a.align_center]}> <View style={isIOS && [a.w_full, a.align_center]}>
<DateInput <DateInput
handleAsUTC handleAsUTC
@ -103,6 +109,7 @@ function BirthdayInner({
accessibilityLabelledBy="birthDate" accessibilityLabelledBy="birthDate"
/> />
</View> </View>
{isError ? ( {isError ? (
<ErrorMessage message={cleanError(error)} style={[a.rounded_sm]} /> <ErrorMessage message={cleanError(error)} style={[a.rounded_sm]} />
) : undefined} ) : undefined}
@ -110,13 +117,14 @@ function BirthdayInner({
<View style={isWeb && [a.flex_row, a.justify_end]}> <View style={isWeb && [a.flex_row, a.justify_end]}>
<Button <Button
label={hasChanged ? _(msg`Save birthday`) : _(msg`Done`)} label={hasChanged ? _(msg`Save birthday`) : _(msg`Done`)}
size={isWeb ? 'small' : 'medium'} size="medium"
onPress={onSave} onPress={onSave}
variant="solid" variant="solid"
color="primary"> color="primary">
<ButtonText> <ButtonText>
{hasChanged ? <Trans>Save</Trans> : <Trans>Done</Trans>} {hasChanged ? <Trans>Save</Trans> : <Trans>Done</Trans>}
</ButtonText> </ButtonText>
{isPending && <ButtonIcon icon={Loader} />}
</Button> </Button>
</View> </View>
</View> </View>

View file

@ -308,10 +308,7 @@ export function ModerationScreenInner({
</ButtonText> </ButtonText>
</Button> </Button>
<BirthDateSettingsDialog <BirthDateSettingsDialog control={birthdateDialogControl} />
control={birthdateDialogControl}
preferences={preferences}
/>
</> </>
)} )}
<View <View

View file

@ -40,10 +40,7 @@ import {
} from '#/state/preferences' } from '#/state/preferences'
import {useSession, useSessionApi, SessionAccount} from '#/state/session' import {useSession, useSessionApi, SessionAccount} from '#/state/session'
import {useProfileQuery} from '#/state/queries/profile' import {useProfileQuery} from '#/state/queries/profile'
import { import {useClearPreferencesMutation} from '#/state/queries/preferences'
useClearPreferencesMutation,
usePreferencesQuery,
} from '#/state/queries/preferences'
// TODO import {useInviteCodesQuery} from '#/state/queries/invites' // TODO import {useInviteCodesQuery} from '#/state/queries/invites'
import {clear as clearStorage} from '#/state/persisted/store' import {clear as clearStorage} from '#/state/persisted/store'
import {clearLegacyStorage} from '#/state/persisted/legacy' import {clearLegacyStorage} from '#/state/persisted/legacy'
@ -156,7 +153,6 @@ export function SettingsScreen({}: Props) {
const {screen, track} = useAnalytics() const {screen, track} = useAnalytics()
const {openModal} = useModalControls() const {openModal} = useModalControls()
const {isSwitchingAccounts, accounts, currentAccount} = useSession() const {isSwitchingAccounts, accounts, currentAccount} = useSession()
const {data: preferences} = usePreferencesQuery()
const {mutate: clearPreferences} = useClearPreferencesMutation() const {mutate: clearPreferences} = useClearPreferencesMutation()
// TODO // TODO
// const {data: invites} = useInviteCodesQuery() // const {data: invites} = useInviteCodesQuery()
@ -295,10 +291,7 @@ export function SettingsScreen({}: Props) {
return ( return (
<View style={s.hContentRegion} testID="settingsScreen"> <View style={s.hContentRegion} testID="settingsScreen">
<ExportCarDialog control={exportCarControl} /> <ExportCarDialog control={exportCarControl} />
<BirthDateSettingsDialog <BirthDateSettingsDialog control={birthdayControl} />
control={birthdayControl}
preferences={preferences}
/>
<SimpleViewHeader <SimpleViewHeader
showBackButton={isMobile} showBackButton={isMobile}