-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
Copy pathRoomDescriptionPage.tsx
110 lines (101 loc) · 4.88 KB
/
RoomDescriptionPage.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
import {useFocusEffect} from '@react-navigation/native';
import React, {useCallback, useRef, useState} from 'react';
import {View} from 'react-native';
import type {OnyxCollection} from 'react-native-onyx';
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView';
import FormProvider from '@components/Form/FormProvider';
import InputWrapper from '@components/Form/InputWrapper';
import HeaderWithBackButton from '@components/HeaderWithBackButton';
import ScreenWrapper from '@components/ScreenWrapper';
import Text from '@components/Text';
import TextInput from '@components/TextInput';
import type {BaseTextInputRef} from '@components/TextInput/BaseTextInput/types';
import useLocalize from '@hooks/useLocalize';
import useThemeStyles from '@hooks/useThemeStyles';
import Parser from '@libs/Parser';
import * as ReportUtils from '@libs/ReportUtils';
import updateMultilineInputRange from '@libs/updateMultilineInputRange';
import variables from '@styles/variables';
import * as Report from '@userActions/Report';
import CONST from '@src/CONST';
import ONYXKEYS from '@src/ONYXKEYS';
import INPUT_IDS from '@src/types/form/ReportDescriptionForm';
import type * as OnyxTypes from '@src/types/onyx';
type RoomDescriptionPageProps = {
/** Policy for the current report */
policies: OnyxCollection<OnyxTypes.Policy>;
/** The report currently being looked at */
report: OnyxTypes.Report;
};
function RoomDescriptionPage({report, policies}: RoomDescriptionPageProps) {
const styles = useThemeStyles();
const [description, setDescription] = useState(() => Parser.htmlToMarkdown(report?.description ?? ''));
const reportDescriptionInputRef = useRef<BaseTextInputRef | null>(null);
const focusTimeoutRef = useRef<ReturnType<typeof setTimeout> | null>(null);
const {translate} = useLocalize();
const policy = policies?.[`${ONYXKEYS.COLLECTION.POLICY}${report?.policyID}`];
const handleReportDescriptionChange = useCallback((value: string) => {
setDescription(value);
}, []);
const submitForm = useCallback(() => {
Report.updateDescription(report.reportID, report?.description ?? '', description.trim());
}, [report.reportID, report.description, description]);
useFocusEffect(
useCallback(() => {
focusTimeoutRef.current = setTimeout(() => {
reportDescriptionInputRef.current?.focus();
return () => {
if (!focusTimeoutRef.current) {
return;
}
clearTimeout(focusTimeoutRef.current);
};
}, CONST.ANIMATED_TRANSITION);
}, []),
);
return (
<ScreenWrapper
shouldEnableMaxHeight
includeSafeAreaPaddingBottom={false}
testID={RoomDescriptionPage.displayName}
>
<FullPageNotFoundView shouldShow={!ReportUtils.canEditReportDescription(report, policy)}>
<HeaderWithBackButton title={translate('reportDescriptionPage.roomDescription')} />
<FormProvider
style={[styles.flexGrow1, styles.ph5]}
formID={ONYXKEYS.FORMS.REPORT_DESCRIPTION_FORM}
onSubmit={submitForm}
submitButtonText={translate('common.save')}
enabledWhenOffline
>
<Text style={[styles.mb5]}>{translate('reportDescriptionPage.explainerText')}</Text>
<View style={[styles.mb6]}>
<InputWrapper
InputComponent={TextInput}
inputID={INPUT_IDS.REPORT_DESCRIPTION}
label={translate('reportDescriptionPage.roomDescription')}
accessibilityLabel={translate('reportDescriptionPage.roomDescription')}
role={CONST.ROLE.PRESENTATION}
autoGrowHeight
maxAutoGrowHeight={variables.textInputAutoGrowMaxHeight}
maxLength={CONST.REPORT_DESCRIPTION.MAX_LENGTH}
ref={(el: BaseTextInputRef | null): void => {
if (!el) {
return;
}
reportDescriptionInputRef.current = el;
updateMultilineInputRange(el);
}}
value={description}
onChangeText={handleReportDescriptionChange}
autoCapitalize="none"
isMarkdownEnabled
/>
</View>
</FormProvider>
</FullPageNotFoundView>
</ScreenWrapper>
);
}
RoomDescriptionPage.displayName = 'RoomDescriptionPage';
export default RoomDescriptionPage;