-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
NewTaskPage.js
221 lines (197 loc) · 8.91 KB
/
NewTaskPage.js
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
import React, {useEffect, useMemo, useState} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import _ from 'underscore';
import PropTypes from 'prop-types';
import lodashGet from 'lodash/get';
import withLocalize, {withLocalizePropTypes} from '../../components/withLocalize';
import compose from '../../libs/compose';
import HeaderWithBackButton from '../../components/HeaderWithBackButton';
import Navigation from '../../libs/Navigation/Navigation';
import ScreenWrapper from '../../components/ScreenWrapper';
import styles from '../../styles/styles';
import ONYXKEYS from '../../ONYXKEYS';
import Permissions from '../../libs/Permissions';
import ROUTES from '../../ROUTES';
import MenuItemWithTopDescription from '../../components/MenuItemWithTopDescription';
import MenuItem from '../../components/MenuItem';
import reportPropTypes from '../reportPropTypes';
import * as Task from '../../libs/actions/Task';
import * as ReportUtils from '../../libs/ReportUtils';
import FormAlertWithSubmitButton from '../../components/FormAlertWithSubmitButton';
import FullPageNotFoundView from '../../components/BlockingViews/FullPageNotFoundView';
import * as LocalePhoneNumber from '../../libs/LocalePhoneNumber';
const propTypes = {
/** Task Creation Data */
task: PropTypes.shape({
assignee: PropTypes.string,
shareDestination: PropTypes.string,
title: PropTypes.string,
description: PropTypes.string,
parentReportID: PropTypes.string,
}),
/** Beta features list */
betas: PropTypes.arrayOf(PropTypes.string),
/** All of the personal details for everyone */
personalDetails: PropTypes.objectOf(
PropTypes.shape({
/** Display name of the person */
displayName: PropTypes.string,
/** Avatar URL of the person */
avatar: PropTypes.string,
/** Login of the person */
login: PropTypes.string,
}),
),
/** All reports shared with the user */
reports: PropTypes.objectOf(reportPropTypes),
...withLocalizePropTypes,
};
const defaultProps = {
betas: [],
task: {},
personalDetails: {},
reports: {},
};
function NewTaskPage(props) {
const [assignee, setAssignee] = useState({});
const [shareDestination, setShareDestination] = useState({});
const [title, setTitle] = useState('');
const [description, setDescription] = useState('');
const [errorMessage, setErrorMessage] = useState('');
const [parentReport, setParentReport] = useState({});
const isAllowedToCreateTask = useMemo(() => _.isEmpty(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 (props.task.assignee) {
const displayDetails = Task.getAssignee(props.task.assigneeAccountID, props.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 (props.task.parentReportID) {
Task.setShareDestinationValue(props.task.parentReportID);
}
// If we have a share destination, we want to set the parent report and
// the share destination data
if (props.task.shareDestination) {
setParentReport(lodashGet(props.reports, `report_${props.task.shareDestination}`, {}));
const displayDetails = Task.getShareDestination(props.task.shareDestination, props.reports, props.personalDetails);
setShareDestination(displayDetails);
}
// If we have a title, we want to set the title
if (!_.isUndefined(props.task.title)) {
setTitle(props.task.title);
}
// If we have a description, we want to set the description
if (!_.isUndefined(props.task.description)) {
setDescription(props.task.description);
}
}, [props]);
// On submit, we want to call the createTask function and wait to validate
// the response
function onSubmit() {
if (!props.task.title && !props.task.shareDestination) {
setErrorMessage(props.translate('newTaskPage.confirmError'));
return;
}
if (!props.task.title) {
setErrorMessage(props.translate('newTaskPage.pleaseEnterTaskName'));
return;
}
if (!props.task.shareDestination) {
setErrorMessage(props.translate('newTaskPage.pleaseEnterTaskDestination'));
return;
}
Task.createTaskAndNavigate(parentReport.reportID, props.task.title, props.task.description, props.task.assignee, props.task.assigneeAccountID, props.task.assigneeChatReport);
}
if (!Permissions.canUseTasks(props.betas)) {
Navigation.dismissModal();
return null;
}
return (
<ScreenWrapper shouldEnableKeyboardAvoidingView={false}>
<FullPageNotFoundView
shouldShow={!isAllowedToCreateTask}
onBackButtonPress={() => Task.dismissModalAndClearOutTaskInfo()}
shouldShowLink={false}
>
<HeaderWithBackButton
title={props.translate('newTaskPage.confirmTask')}
onCloseButtonPress={() => Task.dismissModalAndClearOutTaskInfo()}
shouldShowBackButton
onBackButtonPress={() => {
Navigation.goBack(ROUTES.NEW_TASK_DETAILS);
}}
/>
<View style={[styles.containerWithSpaceBetween]}>
<View style={styles.mb5}>
<MenuItemWithTopDescription
description={props.translate('task.title')}
title={title}
onPress={() => Navigation.navigate(ROUTES.NEW_TASK_TITLE)}
shouldShowRightIcon
/>
<MenuItemWithTopDescription
description={props.translate('task.description')}
title={description}
onPress={() => Navigation.navigate(ROUTES.NEW_TASK_DESCRIPTION)}
shouldShowRightIcon
shouldParseTitle
numberOfLinesTitle={2}
titleStyle={styles.flex1}
/>
<MenuItem
label={assignee.displayName ? props.translate('task.assignee') : ''}
title={assignee.displayName || ''}
description={assignee.displayName ? LocalePhoneNumber.formatPhoneNumber(assignee.subtitle) : props.translate('task.assignee')}
icon={assignee.icons}
onPress={() => Navigation.navigate(ROUTES.NEW_TASK_ASSIGNEE)}
shouldShowRightIcon
/>
<MenuItem
label={shareDestination.displayName ? props.translate('newTaskPage.shareSomewhere') : ''}
title={shareDestination.displayName || ''}
description={shareDestination.displayName ? shareDestination.subtitle : props.translate('newTaskPage.shareSomewhere')}
icon={shareDestination.icons}
onPress={() => Navigation.navigate(ROUTES.NEW_TASK_SHARE_DESTINATION)}
interactive={!props.task.parentReportID}
shouldShowRightIcon={!props.task.parentReportID}
/>
</View>
<FormAlertWithSubmitButton
isAlertVisible={!_.isEmpty(errorMessage)}
message={errorMessage}
onSubmit={() => onSubmit()}
enabledWhenOffline
buttonText={props.translate('newTaskPage.confirmTask')}
containerStyles={[styles.mh0, styles.mt5, styles.flex1, styles.ph5]}
/>
</View>
</FullPageNotFoundView>
</ScreenWrapper>
);
}
NewTaskPage.displayName = 'NewTaskPage';
NewTaskPage.propTypes = propTypes;
NewTaskPage.defaultProps = defaultProps;
export default compose(
withOnyx({
betas: {
key: ONYXKEYS.BETAS,
},
task: {
key: ONYXKEYS.TASK,
},
reports: {
key: ONYXKEYS.COLLECTION.REPORT,
},
personalDetails: {
key: ONYXKEYS.PERSONAL_DETAILS_LIST,
},
}),
withLocalize,
)(NewTaskPage);