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: field appending on duplicate should ignore non string values #11621

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
33 changes: 18 additions & 15 deletions packages/payload/src/fields/setDefaultBeforeDuplicate.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
// @ts-strict-ignore
// default beforeDuplicate hook for required and unique fields
import { type FieldAffectingData, type FieldHook, fieldShouldBeLocalized } from './config/types.js'
import { handleUniqueFields } from './utilities/handleUniqueFields.ts.js'

const unique: FieldHook = ({ value }) => (typeof value === 'string' ? `${value} - Copy` : undefined)
const hasValue = (value) => typeof value === 'string' && value.trim() !== ''
const unique: FieldHook = ({ value }) => (hasValue(value) ? `${value} - Copy` : undefined)
const localizedUnique: FieldHook = ({ req, value }) =>
value ? `${value} - ${req?.t('general:copy') ?? 'Copy'}` : undefined
const uniqueRequired: FieldHook = ({ value }) => `${value} - Copy`
const localizedUniqueRequired: FieldHook = ({ req, value }) =>
`${value} - ${req?.t('general:copy') ?? 'Copy'}`
hasValue(value) ? `${value} - ${req?.t('general:copy') ?? 'Copy'}` : undefined
const numberUnique: FieldHook = ({ value }) => (value ? `${value + 1}` : undefined)
const resetUnique: FieldHook = () => undefined

export const setDefaultBeforeDuplicate = (
field: FieldAffectingData,
Expand All @@ -18,16 +19,18 @@ export const setDefaultBeforeDuplicate = (
(!field.hooks?.beforeDuplicate ||
(Array.isArray(field.hooks.beforeDuplicate) && field.hooks.beforeDuplicate.length === 0))
) {
if ((field.type === 'text' || field.type === 'textarea') && field.required && field.unique) {
field.hooks.beforeDuplicate = [
fieldShouldBeLocalized({ field, parentIsLocalized })
? localizedUniqueRequired
: uniqueRequired,
]
} else if (field.unique) {
field.hooks.beforeDuplicate = [
fieldShouldBeLocalized({ field, parentIsLocalized }) ? localizedUnique : unique,
]
if (field.unique) {
const updateStrategy = handleUniqueFields(field.type)

const strategyHooks = {
append: [fieldShouldBeLocalized({ field, parentIsLocalized }) ? localizedUnique : unique],
numericallyAppend: [numberUnique],
undefined: [resetUnique],
}

if (strategyHooks[updateStrategy]) {
field.hooks.beforeDuplicate = strategyHooks[updateStrategy]
}
}
}
}
29 changes: 29 additions & 0 deletions packages/payload/src/fields/utilities/handleUniqueFields.ts.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Fields that can have unique values:
// Json - can append
// Code - can append
// Email - cant do anything, return undefined
// Number - can append with number + 1
// Point - cant do anything, return undefined
// Relationship - cant do anything, return undefined
// Select - cant do anything, return undefined
// Text - can append
// Textarea - can append
// Upload - cant do anything, return undefined

type updateOptions = 'append' | 'numericallyAppend' | 'undefined'

const updateStrategies: Record<updateOptions, Set<string>> = {
append: new Set(['code', 'json', 'text', 'textarea']),
numericallyAppend: new Set(['number']),
undefined: new Set(['email', 'point', 'relationship', 'select', 'upload']),
}

export const handleUniqueFields = (fieldType: string): updateOptions => {
if (updateStrategies.append.has(fieldType)) {
return 'append'
}
if (updateStrategies.numericallyAppend.has(fieldType)) {
return 'numericallyAppend'
}
return 'undefined'
}