Skip to content

Commit

Permalink
fix: metadata resetted
Browse files Browse the repository at this point in the history
  • Loading branch information
blushi committed Jan 22, 2025
1 parent 2c57923 commit 1a8d9b7
Showing 1 changed file with 2 additions and 22 deletions.
24 changes: 2 additions & 22 deletions web-marketplace/src/components/molecules/MetadataJSONField.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import React, { useEffect, useState } from 'react';
import { Trans } from '@lingui/macro';
import { Box } from '@mui/material';
import { Field, useFormikContext } from 'formik';
Expand All @@ -9,10 +8,6 @@ import { Body } from 'web-components/src/components/typography';

import { CreditBasicsFormValues } from '../../features/ecocredit/CreateBatchBySteps/CreateBatchMultiStepForm/CreditBasics';

// TODO
// Make this component more generic, so that it doesn't depend
// on specific types/interfaces of a particular form.

interface FieldProps {
name?: string;
required?: boolean;
Expand All @@ -26,27 +21,12 @@ export function MetadataJSONField({
...props
}: FieldProps): JSX.Element {
const {
setFieldValue,
setFieldTouched,
values: { metadata },
} = useFormikContext<CreditBasicsFormValues>();
const [formikName, setFormikName] = useState('loading');

useEffect(() => {
// reset metadata if classId changes and still in Object form
if (typeof metadata !== 'string' || formikName === 'loading') {
setFieldValue(name, '');
// using this timeout fixes a timing bug when transitioning from metadata Object
setTimeout(() => {
setFormikName(name);
setFieldTouched(name, false);
}, 700);
}
}, [classId, formikName, metadata, name, setFieldTouched, setFieldValue]);

return (
<Box sx={{ mt: 10 }}>
{formikName === name && typeof metadata === 'string' ? (
{typeof metadata === 'string' ? (
<>
<InputLabel>
<Trans>Metadata</Trans>
Expand All @@ -59,7 +39,7 @@ export function MetadataJSONField({
</Body>
<Field
{...props}
name={formikName}
name={name}
component={ControlledTextField}
multiline
minRows={6}
Expand Down

0 comments on commit 1a8d9b7

Please sign in to comment.