Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: update form with storage field should render with existing files #993

Merged
merged 2 commits into from
May 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -25161,41 +25161,43 @@ export default function UpdateProductForm(props) {
isRequired={false}
isReadOnly={false}
>
<StorageManager
defaultFiles={imgKeys.map((key) => ({ key }))}
onUploadSuccess={({ key }) => {
setImgKeys((prev) => {
let value = [...prev, key];
if (onChange) {
const modelFields = {
name,
imgKeys: value,
};
const result = onChange(modelFields);
value = result?.imgKeys ?? value;
}
return value;
});
}}
onFileRemove={({ key }) => {
setImgKeys((prev) => {
let value = prev.filter((f) => f !== key);
if (onChange) {
const modelFields = {
name,
imgKeys: value,
};
const result = onChange(modelFields);
value = result?.imgKeys ?? value;
}
return value;
});
}}
accessLevel={\\"private\\"}
acceptedFileTypes={[\\".doc\\", \\".pdf\\"]}
isResumable={false}
showThumbnails={true}
></StorageManager>
{productRecord && (
bombguy marked this conversation as resolved.
Show resolved Hide resolved
<StorageManager
defaultFiles={productRecord.imgKeys.map((key) => ({ key }))}
onUploadSuccess={({ key }) => {
setImgKeys((prev) => {
let value = [...prev, key];
if (onChange) {
const modelFields = {
name,
imgKeys: value,
};
const result = onChange(modelFields);
value = result?.imgKeys ?? value;
}
return value;
});
}}
onFileRemove={({ key }) => {
setImgKeys((prev) => {
let value = prev.filter((f) => f !== key);
if (onChange) {
const modelFields = {
name,
imgKeys: value,
};
const result = onChange(modelFields);
value = result?.imgKeys ?? value;
}
return value;
});
}}
accessLevel={\\"private\\"}
acceptedFileTypes={[\\".doc\\", \\".pdf\\"]}
isResumable={false}
showThumbnails={true}
></StorageManager>
)}
</Field>
<Flex
justifyContent=\\"space-between\\"
Expand Down Expand Up @@ -25389,46 +25391,49 @@ export default function UpdateProductForm(props) {
errorMessage={errors.singleImgKey?.errorMessage}
hasError={errors.singleImgKey?.hasError}
label={\\"Single Image\\"}
descriptiveText={\\"Limited to One Image\\"}
isRequired={false}
isReadOnly={false}
>
<StorageManager
defaultFiles={singleImgKey ? [{ key: singleImgKey }] : undefined}
onUploadSuccess={({ key }) => {
setSingleImgKey((prev) => {
let value = key;
if (onChange) {
const modelFields = {
name,
singleImgKey: value,
};
const result = onChange(modelFields);
value = result?.singleImgKey ?? value;
}
return value;
});
}}
onFileRemove={({ key }) => {
setSingleImgKey((prev) => {
let value = initialValues?.singleImgKey;
if (onChange) {
const modelFields = {
name,
singleImgKey: value,
};
const result = onChange(modelFields);
value = result?.singleImgKey ?? value;
}
return value;
});
}}
accessLevel={\\"protected\\"}
acceptedFileTypes={[\\".txt\\", \\".pdf\\"]}
isResumable={true}
showThumbnails={false}
maxFileCount={1}
maxSize={1024}
></StorageManager>
{productRecord && (
<StorageManager
defaultFiles={[{ key: productRecord.singleImgKey }]}
onUploadSuccess={({ key }) => {
setSingleImgKey((prev) => {
let value = key;
if (onChange) {
const modelFields = {
name,
singleImgKey: value,
};
const result = onChange(modelFields);
value = result?.singleImgKey ?? value;
}
return value;
});
}}
onFileRemove={({ key }) => {
setSingleImgKey((prev) => {
let value = initialValues?.singleImgKey;
if (onChange) {
const modelFields = {
name,
singleImgKey: value,
};
const result = onChange(modelFields);
value = result?.singleImgKey ?? value;
}
return value;
});
}}
accessLevel={\\"protected\\"}
acceptedFileTypes={[\\".txt\\", \\".pdf\\"]}
isResumable={true}
showThumbnails={false}
maxFileCount={1}
maxSize={1024}
></StorageManager>
)}
</Field>
<Flex
justifyContent=\\"space-between\\"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,19 @@ import {
ComponentMetadata,
isValidVariableName,
} from '@aws-amplify/codegen-ui';
import { factory, JsxAttribute, JsxAttributeLike, JsxElement, JsxExpression, SyntaxKind } from 'typescript';
import { factory, JsxAttribute, JsxAttributeLike, JsxChild, JsxElement, JsxExpression, SyntaxKind } from 'typescript';
import { getDecoratedLabel } from '../../forms/form-renderer-helper';
import { buildStorageManagerOnChangeStatement } from '../../forms/form-renderer-helper/event-handler-props';
import { propertyToExpression } from '../../react-component-render-helper';
import { STORAGE_FILE_KEY } from '../constants';
import { lowerCaseFirst } from '../../helpers';

const fieldKeys = new Set<keyof FormDefinitionStorageFieldElement['props']>(['label', 'isRequired', 'isReadOnly']);
const fieldKeys = new Set<keyof FormDefinitionStorageFieldElement['props']>([
'label',
'isRequired',
'isReadOnly',
'descriptiveText',
]);

const storageManagerKeys = new Set<keyof FormDefinitionStorageFieldElement['props']>([
'accessLevel',
Expand All @@ -54,8 +60,11 @@ export const renderStorageFieldComponent = (
fieldConfigs: Record<string, FieldConfigMetadata>,
labelDecorator?: LabelDecorator,
isRequired?: boolean,
): JsxElement => {
) => {
const { name: componentName } = component;
const dataTypeName = componentMetadata.formMetadata?.dataType.dataTypeName || '';
const lowerCaseDataTypeName = lowerCaseFirst(dataTypeName);
const lowerCaseDataTypeNameRecord = `${lowerCaseDataTypeName}Record`;
const storageManagerComponentName = factory.createIdentifier('StorageManager');
const storageManagerAttributes: JsxAttribute[] = [];
const fieldAttributes: JsxAttribute[] = [];
Expand All @@ -78,7 +87,10 @@ export const renderStorageFieldComponent = (
undefined,
factory.createCallExpression(
factory.createPropertyAccessExpression(
factory.createIdentifier(componentName),
factory.createPropertyAccessExpression(
factory.createIdentifier(lowerCaseDataTypeNameRecord),
factory.createIdentifier(componentName),
),
factory.createIdentifier('map'),
),
undefined,
Expand Down Expand Up @@ -118,25 +130,22 @@ export const renderStorageFieldComponent = (
)
: factory.createJsxExpression(
undefined,
factory.createConditionalExpression(
factory.createIdentifier(componentName),
factory.createToken(SyntaxKind.QuestionToken),
factory.createArrayLiteralExpression(
[
factory.createObjectLiteralExpression(
[
factory.createPropertyAssignment(
factory.createIdentifier('key'),
factory.createArrayLiteralExpression(
[
factory.createObjectLiteralExpression(
[
factory.createPropertyAssignment(
factory.createIdentifier('key'),
factory.createPropertyAccessExpression(
factory.createIdentifier(lowerCaseDataTypeNameRecord),
factory.createIdentifier(componentName),
),
],
false,
),
],
false,
),
factory.createToken(SyntaxKind.ColonToken),
factory.createIdentifier('undefined'),
),
],
false,
),
],
false,
),
);

Expand All @@ -147,6 +156,7 @@ export const renderStorageFieldComponent = (

storageManagerAttributes.push(buildStorageManagerOnChangeStatement(component, fieldConfigs, 'onUploadSuccess'));
storageManagerAttributes.push(buildStorageManagerOnChangeStatement(component, fieldConfigs, 'onFileRemove'));

fieldAttributes.push(
factory.createJsxAttribute(
factory.createIdentifier('errorMessage'),
Expand Down Expand Up @@ -215,10 +225,21 @@ export const renderStorageFieldComponent = (
factory.createJsxClosingElement(storageManagerComponentName),
);

return renderFieldWrapper(fieldAttributes, storageManager);
const wrappedStorageManagerBlock = factory.createJsxExpression(
undefined,
factory.createBinaryExpression(
factory.createIdentifier(lowerCaseDataTypeNameRecord),
factory.createToken(SyntaxKind.AmpersandAmpersandToken),
factory.createParenthesizedExpression(storageManager),
),
);
return renderFieldWrapper(
fieldAttributes,
componentMetadata.formMetadata?.formActionType === 'update' ? wrappedStorageManagerBlock : storageManager,
);
};

export const renderFieldWrapper = (attributes: JsxAttributeLike[], storageManagerComponent: JsxElement): JsxElement => {
export const renderFieldWrapper = (attributes: JsxAttributeLike[], storageManagerComponent: JsxChild): JsxElement => {
const storageManagerComponentName = factory.createIdentifier('Field');

return factory.createJsxElement(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"label": "Single Image",
"inputType": {
"type": "StorageField",
"descriptiveText": "Limited to One Image",
"fileUploaderConfig": {
"accessLevel": "protected",
"acceptedFileTypes": [".txt", ".pdf"],
Expand Down