mirror of https://git.lenooby09.tech/LeNooby09/social-app.git
1import React from 'react'
2import {View} from 'react-native'
3import {msg, Trans} from '@lingui/macro'
4import {useLingui} from '@lingui/react'
5
6import {cleanError} from '#/lib/strings/errors'
7import {logger} from '#/logger'
8import {isIOS, isWeb} from '#/platform/detection'
9import {
10 usePreferencesQuery,
11 UsePreferencesQueryResponse,
12 usePreferencesSetBirthDateMutation,
13} from '#/state/queries/preferences'
14import {ErrorMessage} from '#/view/com/util/error/ErrorMessage'
15import {DateInput} from '#/view/com/util/forms/DateInput'
16import {atoms as a, useTheme} from '#/alf'
17import * as Dialog from '#/components/Dialog'
18import {Loader} from '#/components/Loader'
19import {Button, ButtonIcon, ButtonText} from '../Button'
20import {Text} from '../Typography'
21
22export function BirthDateSettingsDialog({
23 control,
24}: {
25 control: Dialog.DialogControlProps
26}) {
27 const t = useTheme()
28 const {_} = useLingui()
29 const {isLoading, error, data: preferences} = usePreferencesQuery()
30
31 return (
32 <Dialog.Outer control={control}>
33 <Dialog.Handle />
34
35 <Dialog.ScrollableInner label={_(msg`My Birthday`)}>
36 <View style={[a.gap_sm, a.pb_lg]}>
37 <Text style={[a.text_2xl, a.font_bold]}>
38 <Trans>My Birthday</Trans>
39 </Text>
40 <Text style={[a.text_md, t.atoms.text_contrast_medium]}>
41 <Trans>This information is not shared with other users.</Trans>
42 </Text>
43 </View>
44
45 {isLoading ? (
46 <Loader size="xl" />
47 ) : error || !preferences ? (
48 <ErrorMessage
49 message={
50 error?.toString() ||
51 _(
52 msg`We were unable to load your birth date preferences. Please try again.`,
53 )
54 }
55 style={[a.rounded_sm]}
56 />
57 ) : (
58 <BirthdayInner control={control} preferences={preferences} />
59 )}
60
61 <Dialog.Close />
62 </Dialog.ScrollableInner>
63 </Dialog.Outer>
64 )
65}
66
67function BirthdayInner({
68 control,
69 preferences,
70}: {
71 control: Dialog.DialogControlProps
72 preferences: UsePreferencesQueryResponse
73}) {
74 const {_} = useLingui()
75 const [date, setDate] = React.useState(preferences.birthDate || new Date())
76 const {
77 isPending,
78 isError,
79 error,
80 mutateAsync: setBirthDate,
81 } = usePreferencesSetBirthDateMutation()
82 const hasChanged = date !== preferences.birthDate
83
84 const onSave = React.useCallback(async () => {
85 try {
86 // skip if date is the same
87 if (hasChanged) {
88 await setBirthDate({birthDate: date})
89 }
90 control.close()
91 } catch (e: any) {
92 logger.error(`setBirthDate failed`, {message: e.message})
93 }
94 }, [date, setBirthDate, control, hasChanged])
95
96 return (
97 <View style={a.gap_lg} testID="birthDateSettingsDialog">
98 <View style={isIOS && [a.w_full, a.align_center]}>
99 <DateInput
100 handleAsUTC
101 testID="birthdayInput"
102 value={date}
103 onChange={setDate}
104 buttonType="default-light"
105 buttonStyle={[a.rounded_sm]}
106 buttonLabelType="lg"
107 accessibilityLabel={_(msg`Birthday`)}
108 accessibilityHint={_(msg`Enter your birth date`)}
109 accessibilityLabelledBy="birthDate"
110 />
111 </View>
112
113 {isError ? (
114 <ErrorMessage message={cleanError(error)} style={[a.rounded_sm]} />
115 ) : undefined}
116
117 <View style={isWeb && [a.flex_row, a.justify_end]}>
118 <Button
119 label={hasChanged ? _(msg`Save birthday`) : _(msg`Done`)}
120 size="medium"
121 onPress={onSave}
122 variant="solid"
123 color="primary">
124 <ButtonText>
125 {hasChanged ? <Trans>Save</Trans> : <Trans>Done</Trans>}
126 </ButtonText>
127 {isPending && <ButtonIcon icon={Loader} />}
128 </Button>
129 </View>
130 </View>
131 )
132}