Skip to content

Commit

Permalink
fix: memoize task desciption default value
Browse files Browse the repository at this point in the history
  • Loading branch information
aswin-s committed Mar 22, 2024
1 parent b9f3906 commit 2c9910c
Show file tree
Hide file tree
Showing 3 changed files with 10 additions and 7 deletions.
5 changes: 3 additions & 2 deletions src/pages/tasks/NewTaskDescriptionPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {StackScreenProps} from '@react-navigation/stack';
import ExpensiMark from 'expensify-common/lib/ExpensiMark';
import React from 'react';
import React, {useMemo} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
Expand Down Expand Up @@ -38,6 +38,7 @@ function NewTaskDescriptionPage({task}: NewTaskDescriptionPageProps) {
const styles = useThemeStyles();
const {translate} = useLocalize();
const {inputCallbackRef} = useAutoFocusInput();
const defaultDescriptionValue = useMemo(() => parser.htmlToMarkdown(parser.replace(task?.description ?? '')), [task?.description]);

const onSubmit = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.NEW_TASK_FORM>) => {
TaskActions.setDescriptionValue(values.taskDescription);
Expand Down Expand Up @@ -77,7 +78,7 @@ function NewTaskDescriptionPage({task}: NewTaskDescriptionPageProps) {
<View style={styles.mb5}>
<InputWrapperWithRef
InputComponent={TextInput}
defaultValue={parser.htmlToMarkdown(parser.replace(task?.description ?? ''))}
defaultValue={defaultDescriptionValue}
inputID={INPUT_IDS.TASK_DESCRIPTION}
label={translate('newTaskPage.descriptionOptional')}
accessibilityLabel={translate('newTaskPage.descriptionOptional')}
Expand Down
7 changes: 4 additions & 3 deletions src/pages/tasks/NewTaskDetailsPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type {StackScreenProps} from '@react-navigation/stack';
import ExpensiMark from 'expensify-common/lib/ExpensiMark';
import React, {useEffect, useState} from 'react';
import React, {useEffect, useMemo, useState} from 'react';
import {View} from 'react-native';
import {withOnyx} from 'react-native-onyx';
import type {OnyxEntry} from 'react-native-onyx';
Expand Down Expand Up @@ -40,11 +40,12 @@ function NewTaskDetailsPage({task}: NewTaskDetailsPageProps) {
const [taskDescription, setTaskDescription] = useState(task?.description ?? '');

const {inputCallbackRef} = useAutoFocusInput();
const defaultDescriptionValue = useMemo(() => parser.htmlToMarkdown(parser.replace(taskDescription)), [taskDescription]);

useEffect(() => {
setTaskTitle(task?.title ?? '');
setTaskDescription(parser.htmlToMarkdown(parser.replace(task?.description ?? '')));
}, [task]);
}, [task?.description, task?.title]);

const validate = (values: FormOnyxValues<typeof ONYXKEYS.FORMS.NEW_TASK_FORM>): FormInputErrors<typeof ONYXKEYS.FORMS.NEW_TASK_FORM> => {
const errors = {};
Expand Down Expand Up @@ -114,7 +115,7 @@ function NewTaskDetailsPage({task}: NewTaskDetailsPageProps) {
autoGrowHeight
shouldSubmitForm
containerStyles={styles.autoGrowHeightMultilineInput}
defaultValue={parser.htmlToMarkdown(parser.replace(taskDescription))}
defaultValue={defaultDescriptionValue}
value={taskDescription}
onValueChange={setTaskDescription}
/>
Expand Down
5 changes: 3 additions & 2 deletions src/pages/tasks/TaskDescriptionPage.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {useFocusEffect} from '@react-navigation/native';
import ExpensiMark from 'expensify-common/lib/ExpensiMark';
import React, {useCallback, useRef} from 'react';
import React, {useCallback, useMemo, useRef} from 'react';
import {View} from 'react-native';
import FullPageNotFoundView from '@components/BlockingViews/FullPageNotFoundView';
import FormProvider from '@components/Form/FormProvider';
Expand Down Expand Up @@ -70,6 +70,7 @@ function TaskDescriptionPage({report, currentUserPersonalDetails}: TaskDescripti
const isOpen = ReportUtils.isOpenTaskReport(report);
const canModifyTask = Task.canModifyTask(report, currentUserPersonalDetails.accountID);
const isTaskNonEditable = ReportUtils.isTaskReport(report) && (!canModifyTask || !isOpen);
const defaultDescriptionValue = useMemo(() => parser.htmlToMarkdown(parser.replace(report?.description ?? '') || ''), [report?.description]);

useFocusEffect(
useCallback(() => {
Expand Down Expand Up @@ -111,7 +112,7 @@ function TaskDescriptionPage({report, currentUserPersonalDetails}: TaskDescripti
name={INPUT_IDS.DESCRIPTION}
label={translate('newTaskPage.descriptionOptional')}
accessibilityLabel={translate('newTaskPage.descriptionOptional')}
defaultValue={parser.htmlToMarkdown((report && parser.replace(report?.description ?? '')) || '')}
defaultValue={defaultDescriptionValue}
ref={(element: AnimatedTextInputRef) => {
if (!element) {
return;
Expand Down

0 comments on commit 2c9910c

Please sign in to comment.