Skip to content

Commit

Permalink
Make DAM license fields optional for ROYALTY_FREE (#1526)
Browse files Browse the repository at this point in the history
Make all DAM license fields optional if `LicenseType` is `ROYALTY_FREE`
even if `requireLicense` is true in `DamConfig`.

Closes COM-250
  • Loading branch information
thomasdax98 authored Dec 21, 2023
1 parent 985868a commit dcaf750
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 14 deletions.
5 changes: 5 additions & 0 deletions .changeset/tricky-adults-laugh.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@comet/cms-admin": minor
---

Make all DAM license fields optional if `LicenseType` is `ROYALTY_FREE` even if `requireLicense` is true in `DamConfig`
1 change: 0 additions & 1 deletion packages/admin/cms-admin/src/dam/FileForm/EditFile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -195,7 +195,6 @@ const EditFileInner = ({ file, id }: EditFileInnerProps) => {
// override default onAfterSubmit because default is stackApi.goBack()
// https://github.com/vivid-planet/comet/blob/master/packages/admin/src/FinalForm.tsx#L53
}}
validateOnBlur
>
{({ pristine, hasValidationErrors, submitting, handleSubmit }) => (
<>
Expand Down
42 changes: 29 additions & 13 deletions packages/admin/cms-admin/src/dam/FileForm/FileSettingsFields.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,18 @@ export const FileSettingsFields = ({ isImage, folderId }: SettingsFormProps): Re
[apollo, folderId, scope],
);

const requiredValidator = React.useCallback(
(value: unknown, allValues: object) => {
const type = (allValues as EditFileFormValues).license?.type;
const isRequired = type === "ROYALTY_FREE" ? false : damConfig.requireLicense;

if (isRequired && !value) {
return <FormattedMessage id="comet.form.required" defaultMessage="Required" />;
}
},
[damConfig.requireLicense],
);

return (
<div>
<FormSection title="General">
Expand Down Expand Up @@ -122,10 +134,9 @@ export const FileSettingsFields = ({ isImage, folderId }: SettingsFormProps): Re
}
}}
shouldShowError={() => true}
validateFields={["license.durationTo"]}
/>
<Field name="license.type">
{({ input: { value } }) => {
{({ input: { value: licenseType } }) => {
return (
<>
<Field
Expand All @@ -135,24 +146,23 @@ export const FileSettingsFields = ({ isImage, folderId }: SettingsFormProps): Re
multiline
minRows={3}
fullWidth
disabled={value === "NO_LICENSE"}
required={damConfig.requireLicense}
disabled={licenseType === "NO_LICENSE"}
validate={requiredValidator}
shouldShowError={() => true}
/>
<Field
label={<FormattedMessage id="comet.dam.file.creatorOrAuthor" defaultMessage="Creator/Author" />}
name="license.author"
component={FinalFormInput}
fullWidth
disabled={value === "NO_LICENSE"}
required={damConfig.requireLicense}
disabled={licenseType === "NO_LICENSE"}
validate={requiredValidator}
shouldShowError={() => true}
/>
<FieldContainer
label={<FormattedMessage id="comet.dam.file.licenseDuration" defaultMessage="License duration" />}
fullWidth
disabled={value === "NO_LICENSE"}
required={damConfig.requireLicense}
disabled={licenseType === "NO_LICENSE"}
>
<DurationFieldWrapper>
<Field
Expand All @@ -166,8 +176,9 @@ export const FileSettingsFields = ({ isImage, folderId }: SettingsFormProps): Re
<Calendar />
</InputAdornment>
}
disabled={value === "NO_LICENSE"}
required={damConfig.requireLicense}
validateFields={["license.durationTo"]}
disabled={licenseType === "NO_LICENSE"}
validate={requiredValidator}
shouldShowError={() => true}
/>
<Field
Expand All @@ -182,7 +193,13 @@ export const FileSettingsFields = ({ isImage, folderId }: SettingsFormProps): Re
</InputAdornment>
}
validate={(value: Date | undefined, allValues) => {
if (value && allValues && value < (allValues as EditFileFormValues).license?.durationFrom) {
const requiredError = requiredValidator(value, allValues);
if (requiredError) {
return requiredError;
}

const durationFrom = (allValues as EditFileFormValues).license?.durationFrom;
if (value && durationFrom && value < durationFrom) {
return (
<FormattedMessage
id="comet.dam.file.error.durationTo"
Expand All @@ -191,8 +208,7 @@ export const FileSettingsFields = ({ isImage, folderId }: SettingsFormProps): Re
);
}
}}
disabled={value === "NO_LICENSE"}
required={value === "ROYALTY_FREE" ? false : damConfig.requireLicense}
disabled={licenseType === "NO_LICENSE"}
shouldShowError={() => true}
/>
</DurationFieldWrapper>
Expand Down

0 comments on commit dcaf750

Please sign in to comment.