-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
CBTListScreen.tsx
326 lines (294 loc) · 8.36 KB
/
CBTListScreen.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import React from "react";
import {
TouchableOpacity,
ScrollView,
StatusBar,
View,
Image,
} from "react-native";
import { getThoughts, deleteThought } from "../thoughtstore";
import { Header, Row, Container, IconButton, Label, Paragraph } from "../ui";
import theme from "../theme";
import { CBT_FORM_SCREEN, SETTING_SCREEN, CBT_VIEW_SCREEN } from "../screens";
import { SavedThought, ThoughtGroup, groupThoughtsByDay } from "../thoughts";
import {
NavigationScreenProp,
NavigationState,
NavigationAction,
} from "react-navigation";
import universalHaptic from "../haptic";
import Constants from "expo-constants";
import * as Haptic from "expo-haptics";
import { validThoughtGroup } from "../sanitize";
import Alerter from "../alerter";
import alerts from "../alerts";
import {
HistoryButtonLabelSetting,
getHistoryButtonLabel,
} from "../SettingsScreen";
import i18n from "../i18n";
import { emojiForSlug } from "../distortions";
import { take } from "lodash";
import { recordScreenCallOnFocus } from "../navigation";
import { FadesIn } from "../animations";
const ThoughtItem = ({
thought,
historyButtonLabel,
onPress,
onDelete,
}: {
thought: SavedThought;
historyButtonLabel: HistoryButtonLabelSetting;
onPress: (thought: SavedThought | boolean) => void;
onDelete: (thought: SavedThought) => void;
}) => (
<Row style={{ marginBottom: 18 }}>
<TouchableOpacity
onPress={() => onPress(thought)}
style={{
backgroundColor: "white",
borderColor: theme.lightGray,
borderBottomWidth: 2,
borderRadius: 8,
borderWidth: 1,
marginRight: 18,
flex: 1,
}}
>
<Paragraph
style={{
color: theme.darkText,
fontWeight: "400",
fontSize: 16,
marginBottom: 8,
paddingLeft: 12,
paddingRight: 12,
paddingTop: 12,
paddingBottom: 6,
}}
>
{historyButtonLabel === "alternative-thought"
? thought.alternativeThought
: thought.automaticThought}
</Paragraph>
<View
style={{
backgroundColor: theme.lightOffwhite,
paddingLeft: 12,
paddingRight: 12,
paddingBottom: 12,
paddingTop: 6,
margin: 4,
borderRadius: 8,
}}
>
<Paragraph>
{take(
thought.cognitiveDistortions
.filter(n => n) // Filters out any nulls or undefineds which can crop up
.filter(distortion => distortion.selected)
.map(dist => emojiForSlug(dist.slug)),
8 // only take a max of 8
)
.filter(n => n)
.join(" ")
.trim()}
</Paragraph>
</View>
</TouchableOpacity>
<IconButton
style={{
alignSelf: "flex-start",
}}
accessibilityLabel={i18n.t("accessibility.delete_thought_button")}
featherIconName={"trash"}
onPress={() => onDelete(thought)}
/>
</Row>
);
const EmptyThoughtIllustration = () => (
<View
style={{
alignItems: "center",
marginTop: 36,
}}
>
<Image
source={require("../../assets/looker/Looker.png")}
style={{
width: 200,
height: 150,
alignSelf: "center",
marginBottom: 32,
}}
/>
<Label marginBottom={18} textAlign={"center"}>
No thoughts yet!
</Label>
</View>
);
interface ThoughtListProps {
groups: ThoughtGroup[];
historyButtonLabel: HistoryButtonLabelSetting;
navigateToViewer: (thought: SavedThought) => void;
onItemDelete: (thought: SavedThought) => void;
}
const ThoughtItemList = ({
groups,
navigateToViewer,
onItemDelete,
historyButtonLabel,
}: ThoughtListProps) => {
if (!groups || groups.length === 0) {
return <EmptyThoughtIllustration />;
}
const items = groups.map(group => {
const thoughts = group.thoughts.map(thought => (
<ThoughtItem
key={thought.uuid}
thought={thought}
onPress={navigateToViewer}
onDelete={onItemDelete}
historyButtonLabel={historyButtonLabel}
/>
));
const isToday =
new Date(group.date).toDateString() === new Date().toDateString();
return (
<View key={group.date} style={{ marginBottom: 18 }}>
<Label>{isToday ? "Today" : group.date}</Label>
{thoughts}
</View>
);
});
return <>{items}</>;
};
interface Props {
navigation: NavigationScreenProp<NavigationState, NavigationAction>;
}
interface State {
groups: ThoughtGroup[];
historyButtonLabel: HistoryButtonLabelSetting;
isReady: boolean;
}
class CBTListScreen extends React.Component<Props, State> {
static navigationOptions = {
header: null,
};
constructor(props) {
super(props);
this.state = {
groups: [],
historyButtonLabel: "alternative-thought",
isReady: false,
};
this.props.navigation.addListener("willFocus", () => {
this.loadSettings();
});
recordScreenCallOnFocus(this.props.navigation, "list");
}
loadExercises = (): void => {
const fixTimestamps = (json): SavedThought => {
const createdAt: Date = new Date(json.createdAt);
const updatedAt: Date = new Date(json.updatedAt);
return {
createdAt,
updatedAt,
...json,
};
};
getThoughts()
.then(data => {
const thoughts: SavedThought[] = data
.map(([_, value]) => JSON.parse(value))
.filter(n => n) // Worst case scenario, if bad data gets in we don't show it.
.map(fixTimestamps);
const groups: ThoughtGroup[] = groupThoughtsByDay(thoughts).filter(
validThoughtGroup
);
this.setState({ groups });
})
.catch(console.error)
.finally(() => {
this.setState({
isReady: true,
});
});
};
loadSettings = (): void => {
getHistoryButtonLabel().then(historyButtonLabel => {
this.setState({ historyButtonLabel });
});
};
componentDidMount = () => {
this.loadExercises();
this.loadSettings();
};
navigateToSettings = () => {
this.props.navigation.push(SETTING_SCREEN);
};
navigateToForm = () => {
universalHaptic.impact(Haptic.ImpactFeedbackStyle.Light);
this.props.navigation.navigate(CBT_FORM_SCREEN, {
thought: false,
});
};
navigateToViewerWithThought = (thought: SavedThought) => {
this.props.navigation.push(CBT_VIEW_SCREEN, {
thought,
});
};
onItemDelete = (thought: SavedThought) => {
// Ignore the typescript error here, Expo's v31 has a bug
// Upgrade to 32 when it's released to fix
universalHaptic.notification(Haptic.NotificationFeedbackType.Success);
deleteThought(thought.uuid).then(() => this.loadExercises());
};
render() {
const { groups, historyButtonLabel, isReady } = this.state;
return (
<View style={{ backgroundColor: theme.lightOffwhite }}>
<ScrollView
style={{
backgroundColor: theme.lightOffwhite,
marginTop: Constants.statusBarHeight,
paddingTop: 24,
height: "100%",
}}
>
<Container>
<StatusBar barStyle="dark-content" translucent={true} />
<Row style={{ marginBottom: 18 }}>
<Header allowFontScaling={false}>.quirk</Header>
<View style={{ flexDirection: "row" }}>
<IconButton
featherIconName={"settings"}
onPress={() => this.navigateToSettings()}
accessibilityLabel={i18n.t("accessibility.settings_button")}
style={{ marginRight: 18 }}
/>
<IconButton
featherIconName={"x"}
onPress={() => this.navigateToForm()}
accessibilityLabel={i18n.t(
"accessibility.new_thought_button"
)}
/>
</View>
</Row>
<FadesIn pose={isReady ? "visible" : "hidden"}>
<ThoughtItemList
groups={groups}
navigateToViewer={this.navigateToViewerWithThought}
onItemDelete={this.onItemDelete}
historyButtonLabel={historyButtonLabel}
/>
</FadesIn>
</Container>
</ScrollView>
<Alerter alerts={alerts} />
</View>
);
}
}
export default CBTListScreen;