Files
payload/test/helpers/e2e/upsertPrefs.ts
Jacob Fletcher f7172b5b2c fix(ui): refreshes column state during hmr and respects admin.disableListColumn despite preferences (#9846)
Partial fix for #9774. When `admin.disableListColumn` is set
retroactively, it continues to appear in column state, but shouldn't.
This was because the table column context was not refreshing after HMR
runs, and would instead hold onto these stale columns until the page
itself refreshes. Similarly, this was also a problem when the user had
saved any of these columns to their list preferences, where those prefs
would take precedence despite these properties being set on the
underlying fields. The fix is to filter these columns from all requests
that send them, and ensure local component state properly refreshes
itself.
2024-12-10 15:11:44 -05:00

60 lines
1.3 KiB
TypeScript

import type { PayloadTestSDK } from 'helpers/sdk/index.js'
import type { GeneratedTypes } from 'helpers/sdk/types.js'
import type { TypedUser } from 'payload'
export const upsertPrefs = async <
TConfig extends GeneratedTypes<any>,
TGeneratedTypes extends GeneratedTypes<any>,
>({
payload,
user,
value,
}: {
payload: PayloadTestSDK<TConfig>
user: TypedUser
value: Record<string, any>
}): Promise<TGeneratedTypes['collections']['payload-preferences']> => {
let prefs = await payload
.find({
collection: 'payload-preferences',
depth: 0,
limit: 1,
where: {
and: [
{ key: { equals: 'text-fields-list' } },
{ 'user.value': { equals: user.id } },
{ 'user.relationTo': { equals: user.collection } },
],
},
})
?.then((res) => res.docs?.[0])
if (!prefs) {
prefs = await payload.create({
collection: 'payload-preferences',
depth: 0,
data: {
key: 'text-fields-list',
user: {
relationTo: user.collection,
value: user.id,
},
value,
},
})
} else {
prefs = await payload.update({
collection: 'payload-preferences',
id: prefs.id,
data: {
value: {
...(prefs?.value ?? {}),
...value,
},
},
})
}
return prefs
}