-
Notifications
You must be signed in to change notification settings - Fork 2.9k
/
NewTaskDetailsPage.tsx
153 lines (138 loc) · 6.57 KB
/
NewTaskDetailsPage.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
import type {StackScreenProps} from '@react-navigation/stack';
import ExpensiMark from 'expensify-common/lib/ExpensiMark';
import React, {useEffect, useState} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
import FormProvider from '@components/Form/FormProvider';
import InputWrapper from '@components/Form/InputWrapper';
import type {FormInputErrors, FormOnyxValues} from '@components/Form/types';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import TextInput from '@components/TextInput';
import useAutoFocusInput from '@hooks/useAutoFocusInput';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import * as ErrorUtils from '@libs/ErrorUtils';
import Navigation from '@libs/Navigation/Navigation';
import type {NewTaskNavigatorParamList} from '@libs/Navigation/types';
import playSound, {SOUNDS} from '@libs/Sound';
import variables from '@styles/variables';
import * as TaskActions from '@userActions/Task';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import ROUTES from '@src/ROUTES';
import type SCREENS from '@src/SCREENS';
import INPUT_IDS from '@src/types/form/NewTaskForm';
import type {Task} from '@src/types/onyx';
type NewTaskDetailsPageOnyxProps = {
/** Task Creation Data */
task: OnyxEntry<Task>;
};
type NewTaskDetailsPageProps = NewTaskDetailsPageOnyxProps & StackScreenProps<NewTaskNavigatorParamList, typeof SCREENS.NEW_TASK.DETAILS>;
const parser = new ExpensiMark();
function NewTaskDetailsPage({task}: NewTaskDetailsPageProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const [taskTitle, setTaskTitle] = useState(task?.title ?? '');
const [taskDescription, setTaskDescription] = useState(task?.description ?? '');
const {inputCallbackRef} = useAutoFocusInput();
const skipConfirmation = task?.skipConfirmation && task?.assigneeAccountID && task?.parentReportID;
const buttonText = skipConfirmation ? translate('newTaskPage.assignTask') : translate('common.next');
useEffect(() => {
setTaskTitle(task?.title ?? '');
setTaskDescription(parser.htmlToMarkdown(parser.replace(task?.description ?? '')));
}, [task]);
const validate = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.NEW_TASK_FORM>): FormInputErrors<typeof ONYXKEYS.FORMS.NEW_TASK_FORM> => {
const errors = {};
if (!values.taskTitle) {
// We error if the user doesn't enter a task name
ErrorUtils.addErrorMessage(errors, 'taskTitle', 'newTaskPage.pleaseEnterTaskName');
} else if (values.taskTitle.length > CONST.TITLE_CHARACTER_LIMIT) {
ErrorUtils.addErrorMessage(errors, 'taskTitle', ['common.error.characterLimitExceedCounter', {length: values.taskTitle.length, limit: CONST.TITLE_CHARACTER_LIMIT}]);
}
if (values.taskDescription.length > CONST.DESCRIPTION_LIMIT) {
ErrorUtils.addErrorMessage(errors, 'taskDescription', ['common.error.characterLimitExceedCounter', {length: values.taskDescription.length, limit: CONST.DESCRIPTION_LIMIT}]);
}
return errors;
};
// On submit, we want to call the assignTask function and wait to validate
// the response
const onSubmit = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.NEW_TASK_FORM>) => {
TaskActions.setDetailsValue(values.taskTitle, values.taskDescription);
if (skipConfirmation) {
TaskActions.setShareDestinationValue(task?.parentReportID ?? '');
playSound(SOUNDS.DONE);
TaskActions.createTaskAndNavigate(
task?.parentReportID ?? '',
values.taskTitle,
values.taskDescription ?? '',
task?.assignee ?? '',
task.assigneeAccountID,
task.assigneeChatReport,
);
} else {
Navigation.navigate(ROUTES.NEW_TASK);
}
};
return (
<ScreenWrapper
includeSafeAreaPaddingBottom={false}
shouldEnableMaxHeight
testID={NewTaskDetailsPage.displayName}
>
<HeaderWithBackButton
title={translate('newTaskPage.assignTask')}
onCloseButtonPress={() => TaskActions.dismissModalAndClearOutTaskInfo()}
shouldShowBackButton
onBackButtonPress={() => TaskActions.dismissModalAndClearOutTaskInfo()}
/>
<FormProvider
formID={ONYXKEYS.FORMS.NEW_TASK_FORM}
submitButtonText={buttonText}
style={[styles.mh5, styles.flexGrow1]}
validate={validate}
onSubmit={onSubmit}
enabledWhenOffline
>
<View style={styles.mb5}>
<InputWrapper
InputComponent={TextInput}
ref={inputCallbackRef}
valueType="string"
role={CONST.ROLE.PRESENTATION}
inputID={INPUT_IDS.TASK_TITLE}
label={translate('task.title')}
accessibilityLabel={translate('task.title')}
value={taskTitle}
onValueChange={setTaskTitle}
autoCorrect={false}
/>
</View>
<View style={styles.mb5}>
<InputWrapper
valueType="string"
InputComponent={TextInput}
role={CONST.ROLE.PRESENTATION}
inputID={INPUT_IDS.TASK_DESCRIPTION}
label={translate('newTaskPage.descriptionOptional')}
accessibilityLabel={translate('newTaskPage.descriptionOptional')}
autoGrowHeight
maxAutoGrowHeight={variables.textInputAutoGrowMaxHeight}
shouldSubmitForm
defaultValue={parser.htmlToMarkdown(parser.replace(taskDescription))}
value={taskDescription}
onValueChange={setTaskDescription}
isMarkdownEnabled
/>
</View>
</FormProvider>
</ScreenWrapper>
);
}
NewTaskDetailsPage.displayName = 'NewTaskDetailsPage';
export default withOnyx<NewTaskDetailsPageProps, NewTaskDetailsPageOnyxProps>({
task: {
key: ONYXKEYS.TASK,
},
})(NewTaskDetailsPage);