-
Notifications
You must be signed in to change notification settings - Fork 3k
/
Copy pathNewTaskPage.tsx
215 lines (196 loc) · 9.86 KB
/
NewTaskPage.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
import type {StackScreenProps} from '@react-navigation/stack';
import React, {useEffect, useMemo, useState} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import type {OnyxCollection, OnyxEntry} from 'react-native-onyx';
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView';
import FormAlertWithSubmitButton from '@components/FormAlertWithSubmitButton';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import MenuItem from '@components/MenuItem';
import MenuItemWithTopDescription from '@components/MenuItemWithTopDescription';
import ScreenWrapper from '@components/ScreenWrapper';
import ScrollView from '@components/ScrollView';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as LocalePhoneNumber from '@libs/LocalePhoneNumber';
import Navigation from '@libs/Navigation/Navigation';
import type {NewTaskNavigatorParamList} from '@libs/Navigation/types';
import * as OptionsListUtils from '@libs/OptionsListUtils';
import * as ReportUtils from '@libs/ReportUtils';
import playSound, {SOUNDS} from '@libs/Sound';
import * as TaskActions from '@userActions/Task';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import type {PersonalDetailsList, Report, Task} from '@src/types/onyx';
import {isEmptyObject} from '@src/types/utils/EmptyObject';
type NewTaskPageOnyxProps = {
/** Task Creation Data */
task: OnyxEntry<Task>;
/** All of the personal details for everyone */
personalDetails: OnyxEntry<PersonalDetailsList>;
/** All reports shared with the user */
reports: OnyxCollection<Report>;
};
type NewTaskPageProps = NewTaskPageOnyxProps & StackScreenProps<NewTaskNavigatorParamList, typeof SCREENS.NEW_TASK.ROOT>;
function NewTaskPage({task, reports, personalDetails}: NewTaskPageProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const [assignee, setAssignee] = useState<TaskActions.Assignee>();
const assigneeTooltipDetails = ReportUtils.getDisplayNamesWithTooltips(
OptionsListUtils.getPersonalDetailsForAccountIDs(task?.assigneeAccountID ? [task.assigneeAccountID] : [], personalDetails),
false,
);
const [shareDestination, setShareDestination] = useState<TaskActions.ShareDestination>();
const [title, setTitle] = useState('');
const [description, setDescription] = useState('');
const [errorMessage, setErrorMessage] = useState('');
const [parentReport, setParentReport] = useState<OnyxEntry<Report>>(null);
const isAllowedToCreateTask = useMemo(() => isEmptyObject(parentReport) || ReportUtils.isAllowedToComment(parentReport), [parentReport]);
useEffect(() => {
setErrorMessage('');
// If we have an assignee, we want to set the assignee data
// If there's an issue with the assignee chosen, we want to notify the user
if (task?.assignee) {
const displayDetails = TaskActions.getAssignee(task?.assigneeAccountID ?? -1, personalDetails);
setAssignee(displayDetails);
}
// We only set the parentReportID if we are creating a task from a report
// this allows us to go ahead and set that report as the share destination
// and disable the share destination selector
if (task?.parentReportID) {
TaskActions.setShareDestinationValue(task.parentReportID);
}
// If we have a share destination, we want to set the parent report and
// the share destination data
if (task?.shareDestination) {
setParentReport(reports?.[`report_${task.shareDestination}`] ?? null);
const displayDetails = TaskActions.getShareDestination(task.shareDestination, reports, personalDetails);
setShareDestination(displayDetails);
}
// If we have a title, we want to set the title
if (task?.title !== undefined) {
setTitle(task.title);
}
// If we have a description, we want to set the description
if (task?.description !== undefined) {
setDescription(task.description);
}
}, [personalDetails, reports, task?.assignee, task?.assigneeAccountID, task?.description, task?.parentReportID, task?.shareDestination, task?.title]);
// On submit, we want to call the createTask function and wait to validate
// the response
const onSubmit = () => {
if (!task?.title && !task?.shareDestination) {
setErrorMessage('newTaskPage.confirmError');
return;
}
if (!task.title) {
setErrorMessage('newTaskPage.pleaseEnterTaskName');
return;
}
if (!task.shareDestination) {
setErrorMessage('newTaskPage.pleaseEnterTaskDestination');
return;
}
playSound(SOUNDS.DONE);
TaskActions.createTaskAndNavigate(
parentReport?.reportID ?? '',
task.title,
task?.description ?? '',
task?.assignee ?? '',
task.assigneeAccountID,
task.assigneeChatReport,
parentReport?.policyID,
);
};
return (
<ScreenWrapper
shouldEnableKeyboardAvoidingView={false}
testID={NewTaskPage.displayName}
>
<FullPageNotFoundView
shouldShow={!isAllowedToCreateTask}
onBackButtonPress={() => TaskActions.dismissModalAndClearOutTaskInfo()}
shouldShowLink={false}
>
<HeaderWithBackButton
title={translate('newTaskPage.confirmTask')}
onCloseButtonPress={() => TaskActions.dismissModalAndClearOutTaskInfo()}
shouldShowBackButton
onBackButtonPress={() => {
Navigation.goBack(ROUTES.NEW_TASK_DETAILS);
}}
/>
<ScrollView
contentContainerStyle={styles.flexGrow1}
// on iOS, navigation animation sometimes cause the scrollbar to appear
// on middle/left side of scrollview. scrollIndicatorInsets with right
// to closest value to 0 fixes this issue, 0 (default) doesn't work
// See: https://github.com/Expensify/App/issues/31441
scrollIndicatorInsets={{right: Number.MIN_VALUE}}
>
<View style={styles.flex1}>
<View style={styles.mb5}>
<MenuItemWithTopDescription
description={translate('task.title')}
title={title}
onPress={() => Navigation.navigate(ROUTES.NEW_TASK_TITLE)}
shouldShowRightIcon
/>
<MenuItemWithTopDescription
description={translate('task.description')}
title={description}
onPress={() => Navigation.navigate(ROUTES.NEW_TASK_DESCRIPTION)}
shouldShowRightIcon
shouldParseTitle
numberOfLinesTitle={2}
titleStyle={styles.flex1}
/>
<MenuItem
label={assignee?.displayName ? translate('task.assignee') : ''}
title={assignee?.displayName ?? ''}
description={assignee?.displayName ? LocalePhoneNumber.formatPhoneNumber(assignee?.subtitle) : translate('task.assignee')}
icon={assignee?.icons}
onPress={() => Navigation.navigate(ROUTES.NEW_TASK_ASSIGNEE)}
shouldShowRightIcon
titleWithTooltips={assigneeTooltipDetails}
/>
<MenuItem
label={shareDestination?.displayName ? translate('newTaskPage.shareSomewhere') : ''}
title={shareDestination?.displayName ?? ''}
description={shareDestination?.displayName ? shareDestination.subtitle : translate('newTaskPage.shareSomewhere')}
icon={shareDestination?.icons}
onPress={() => Navigation.navigate(ROUTES.NEW_TASK_SHARE_DESTINATION)}
interactive={!task?.parentReportID}
shouldShowRightIcon={!task?.parentReportID}
titleWithTooltips={shareDestination?.shouldUseFullTitleToDisplay ? undefined : shareDestination?.displayNamesWithTooltips}
/>
</View>
</View>
<View style={styles.flexShrink0}>
<FormAlertWithSubmitButton
isAlertVisible={Boolean(errorMessage)}
message={errorMessage}
onSubmit={onSubmit}
enabledWhenOffline
buttonText={translate('newTaskPage.confirmTask')}
containerStyles={[styles.mh0, styles.mt5, styles.flex1, styles.ph5]}
/>
</View>
</ScrollView>
</FullPageNotFoundView>
</ScreenWrapper>
);
}
NewTaskPage.displayName = 'NewTaskPage';
export default withOnyx<NewTaskPageProps, NewTaskPageOnyxProps>({
task: {
key: ONYXKEYS.TASK,
},
reports: {
key: ONYXKEYS.COLLECTION.REPORT,
},
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
},
})(NewTaskPage);