-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
ChallengeScreen.tsx
197 lines (183 loc) · 5.17 KB
/
ChallengeScreen.tsx
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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import React from "react";
import theme from "../theme";
import {
MediumHeader,
HintHeader,
ActionButton,
Row,
GhostButton,
Container,
SubHeader,
GhostButtonWithGuts,
} from "../ui";
import { textInputStyle, textInputPlaceholderColor } from "../textInputStyle";
import i18n from "../i18n";
import * as stats from "../stats";
import ScreenProps from "../ScreenProps";
import Constants from "expo-constants";
import { get } from "lodash";
import { Thought } from "../thoughts";
import {
ALTERNATIVE_SCREEN,
DISTORTION_SCREEN,
FINISHED_SCREEN,
AUTOMATIC_THOUGHT_SCREEN,
} from "./screens";
import {
TextInput,
KeyboardAvoidingView,
View,
Text,
ScrollView,
} from "react-native";
import { saveThought } from "../thoughtstore";
import haptic from "../haptic";
import * as Haptic from "expo-haptics";
export default class ChallengeScreen extends React.Component<
ScreenProps,
{
thought?: Thought;
isEditing: boolean;
}
> {
static navigationOptions = {
header: null,
};
state = {
thought: undefined,
isEditing: false,
};
componentDidMount() {
this.props.navigation.addListener("willFocus", args => {
this.refreshFromNavigation(args);
});
this.props.navigation.addListener("didFocus", args => {
this.refreshFromNavigation(args);
});
}
refreshFromNavigation = args => {
const thought = get(args, "state.params.thought");
const isEditing = get(args, "state.params.isEditing", false);
this.setState({
thought,
isEditing,
});
};
onChange = (txt: string) => {
this.setState(prevState => {
if (!prevState.thought) {
return prevState;
}
prevState.thought.challenge = txt;
return prevState;
});
};
onNext = async () => {
haptic.impact(Haptic.ImpactFeedbackStyle.Light);
const thought = await saveThought(this.state.thought);
this.props.navigation.push(ALTERNATIVE_SCREEN, {
thought,
});
};
// From editing
onFinish = async () => {
haptic.impact(Haptic.ImpactFeedbackStyle.Light);
const thought = await saveThought(this.state.thought);
this.props.navigation.push(FINISHED_SCREEN, {
thought,
});
};
render() {
return (
<ScrollView
style={{
paddingTop: 24 + Constants.statusBarHeight,
paddingHorizontal: 24,
backgroundColor: theme.lightOffwhite,
flex: 1,
}}
>
<KeyboardAvoidingView
behavior="position"
style={{
paddingBottom: 48,
marginBottom: 24,
}}
>
{this.state.thought && (
<>
<MediumHeader>{i18n.t("challenge")}</MediumHeader>
<HintHeader>
What could you be wrong about? Is there something you don't have
enough evidence for?
</HintHeader>
<View
style={{
marginBottom: 12,
}}
>
<SubHeader>Your Thought</SubHeader>
<GhostButtonWithGuts
borderColor={theme.lightGray}
onPress={() =>
this.props.navigation.navigate(AUTOMATIC_THOUGHT_SCREEN, {
thought: this.state.thought,
})
}
>
<Text>{this.state.thought.automaticThought}</Text>
</GhostButtonWithGuts>
</View>
<SubHeader>Your Challenge</SubHeader>
<TextInput
style={textInputStyle}
placeholderTextColor={textInputPlaceholderColor}
placeholder={i18n.t("cbt_form.changed_placeholder")}
value={this.state.thought.challenge}
multiline={true}
numberOfLines={6}
onChangeText={this.onChange}
onBlur={() => stats.userFilledOutFormField("challenge")}
/>
<Row
style={{
marginTop: 24,
justifyContent: "flex-end",
}}
>
{this.state.isEditing ? (
<ActionButton
title={"Save"}
onPress={() => this.onFinish()}
width={"100%"}
/>
) : (
<>
<GhostButton
borderColor={theme.lightGray}
textColor={theme.veryLightText}
title={"Back"}
style={{
marginRight: 24,
flex: 1,
}}
onPress={() => {
this.props.navigation.navigate(DISTORTION_SCREEN, {
thought: this.state.thought,
});
}}
/>
<ActionButton
title={"Next"}
onPress={() => this.onNext()}
/>
</>
)}
</Row>
</>
)}
</KeyboardAvoidingView>
</ScrollView>
);
}
}