fix: duplicate with orderable (#12274)
Previously, duplication with orderable collections worked incorrectly,
for example
Document 1 is created - `_order: 'a5'`
Document 2 is duplicated from 1, - `_order: 'a5 - copy'` (result from
47a1eee765/packages/payload/src/fields/setDefaultBeforeDuplicate.ts (L6))
Now, the `_order` value is re-calculated properly.
This commit is contained in:
@@ -83,6 +83,13 @@ export const addOrderableFieldsAndHook = (
|
||||
hidden: true,
|
||||
readOnly: true,
|
||||
},
|
||||
hooks: {
|
||||
beforeDuplicate: [
|
||||
({ siblingData }) => {
|
||||
delete siblingData[orderableFieldName]
|
||||
},
|
||||
],
|
||||
},
|
||||
index: true,
|
||||
required: true,
|
||||
// override the schema to make order fields optional for payload.create()
|
||||
@@ -275,5 +282,6 @@ export const addOrderableEndpoint = (config: SanitizedConfig) => {
|
||||
if (!config.endpoints) {
|
||||
config.endpoints = []
|
||||
}
|
||||
|
||||
config.endpoints.push(reorderEndpoint)
|
||||
}
|
||||
|
||||
@@ -444,6 +444,43 @@ describe('Sort', () => {
|
||||
parseInt(ordered.docs[1]._order, 16),
|
||||
)
|
||||
})
|
||||
|
||||
it('should allow to duplicate with reordable', async () => {
|
||||
const doc = await payload.create({
|
||||
collection: 'orderable',
|
||||
data: { title: 'new document' },
|
||||
})
|
||||
|
||||
const docDuplicated = await payload.create({
|
||||
duplicateFromID: doc.id,
|
||||
collection: 'orderable',
|
||||
data: {},
|
||||
})
|
||||
expect(docDuplicated.title).toBe('new document')
|
||||
expect(parseInt(doc._order!, 16)).toBeLessThan(parseInt(docDuplicated._order!, 16))
|
||||
|
||||
await restClient.POST('/reorder', {
|
||||
body: JSON.stringify({
|
||||
collectionSlug: orderableSlug,
|
||||
docsToMove: [doc.id],
|
||||
newKeyWillBe: 'greater',
|
||||
orderableFieldName: '_order',
|
||||
target: {
|
||||
id: docDuplicated.id,
|
||||
key: docDuplicated._order,
|
||||
},
|
||||
}),
|
||||
})
|
||||
|
||||
const docAfterReorder = await payload.findByID({ collection: 'orderable', id: doc.id })
|
||||
const docDuplicatedAfterReorder = await payload.findByID({
|
||||
collection: 'orderable',
|
||||
id: docDuplicated.id,
|
||||
})
|
||||
expect(parseInt(docAfterReorder._order!, 16)).toBeGreaterThan(
|
||||
parseInt(docDuplicatedAfterReorder._order!, 16),
|
||||
)
|
||||
})
|
||||
})
|
||||
|
||||
describe('Orderable join', () => {
|
||||
|
||||
Reference in New Issue
Block a user