about summary refs log tree commit diff
path: root/src/view/com/auth/create/CreateAccount.tsx
blob: 8cf1cfaf5dcf63d95aa86185571ca68faec11576 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
import React from 'react'
import {
  ActivityIndicator,
  KeyboardAvoidingView,
  ScrollView,
  StyleSheet,
  TouchableOpacity,
  View,
} from 'react-native'
import {observer} from 'mobx-react-lite'
import {useAnalytics} from 'lib/analytics/analytics'
import {Text} from '../../util/text/Text'
import {LoggedOutLayout} from 'view/com/util/layouts/LoggedOutLayout'
import {s} from 'lib/styles'
import {useStores} from 'state/index'
import {CreateAccountModel} from 'state/models/ui/create-account'
import {usePalette} from 'lib/hooks/usePalette'

import {Step1} from './Step1'
import {Step2} from './Step2'
import {Step3} from './Step3'

export const CreateAccount = observer(
  ({onPressBack}: {onPressBack: () => void}) => {
    const {track, screen} = useAnalytics()
    const pal = usePalette('default')
    const store = useStores()
    const model = React.useMemo(() => new CreateAccountModel(store), [store])

    React.useEffect(() => {
      screen('CreateAccount')
    }, [screen])

    React.useEffect(() => {
      model.fetchServiceDescription()
    }, [model])

    const onPressRetryConnect = React.useCallback(
      () => model.fetchServiceDescription(),
      [model],
    )

    const onPressBackInner = React.useCallback(() => {
      if (model.canBack) {
        model.back()
      } else {
        onPressBack()
      }
    }, [model, onPressBack])

    const onPressNext = React.useCallback(async () => {
      if (!model.canNext) {
        return
      }
      if (model.step < 3) {
        model.next()
      } else {
        try {
          await model.submit()
        } catch {
          // dont need to handle here
        } finally {
          track('Try Create Account')
        }
      }
    }, [model, track])

    return (
      <LoggedOutLayout
        leadin={`Step ${model.step}`}
        title="Create Account"
        description="We're so excited to have you join us!">
        <ScrollView testID="createAccount" style={pal.view}>
          <KeyboardAvoidingView behavior="padding">
            <View style={styles.stepContainer}>
              {model.step === 1 && <Step1 model={model} />}
              {model.step === 2 && <Step2 model={model} />}
              {model.step === 3 && <Step3 model={model} />}
            </View>
            <View style={[s.flexRow, s.pl20, s.pr20]}>
              <TouchableOpacity
                onPress={onPressBackInner}
                testID="backBtn"
                accessibilityRole="button">
                <Text type="xl" style={pal.link}>
                  Back
                </Text>
              </TouchableOpacity>
              <View style={s.flex1} />
              {model.canNext ? (
                <TouchableOpacity
                  testID="nextBtn"
                  onPress={onPressNext}
                  accessibilityRole="button">
                  {model.isProcessing ? (
                    <ActivityIndicator />
                  ) : (
                    <Text type="xl-bold" style={[pal.link, s.pr5]}>
                      Next
                    </Text>
                  )}
                </TouchableOpacity>
              ) : model.didServiceDescriptionFetchFail ? (
                <TouchableOpacity
                  testID="retryConnectBtn"
                  onPress={onPressRetryConnect}
                  accessibilityRole="button"
                  accessibilityLabel="Retry"
                  accessibilityHint="Retries account creation"
                  accessibilityLiveRegion="polite">
                  <Text type="xl-bold" style={[pal.link, s.pr5]}>
                    Retry
                  </Text>
                </TouchableOpacity>
              ) : model.isFetchingServiceDescription ? (
                <>
                  <ActivityIndicator color="#fff" />
                  <Text type="xl" style={[pal.text, s.pr5]}>
                    Connecting...
                  </Text>
                </>
              ) : undefined}
            </View>
            <View style={s.footerSpacer} />
          </KeyboardAvoidingView>
        </ScrollView>
      </LoggedOutLayout>
    )
  },
)

const styles = StyleSheet.create({
  stepContainer: {
    paddingHorizontal: 20,
    paddingVertical: 20,
  },
})