feat: calc label field on copy record
This commit is contained in:
parent
d02a07541b
commit
d56e1eb5ea
@ -1,8 +1,54 @@
|
|||||||
import { CollectionConfig } from "payload";
|
import { CollectionAfterReadHook, CollectionConfig } from "payload";
|
||||||
|
|
||||||
|
const afterReadHook: CollectionAfterReadHook = async ({ doc, req }) => {
|
||||||
|
if (doc.label) return doc;
|
||||||
|
|
||||||
|
let bookName;
|
||||||
|
if (doc.book) {
|
||||||
|
const relatedBook = await req.payload.findByID({
|
||||||
|
req,
|
||||||
|
collection: 'books',
|
||||||
|
id: doc.book,
|
||||||
|
depth: 2,
|
||||||
|
})
|
||||||
|
bookName = relatedBook.title
|
||||||
|
} else return doc
|
||||||
|
|
||||||
|
let repositoryName;
|
||||||
|
if (doc.repository) {
|
||||||
|
const relatedRepo = await req.payload.findByID({
|
||||||
|
req,
|
||||||
|
collection: 'repositories',
|
||||||
|
id: doc.repository,
|
||||||
|
depth: 2,
|
||||||
|
})
|
||||||
|
repositoryName = relatedRepo.abbreviation || relatedRepo.name
|
||||||
|
} else return doc
|
||||||
|
|
||||||
|
return { ...doc, label: `[${repositoryName}] ${bookName}` }
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
export const Copies: CollectionConfig = {
|
export const Copies: CollectionConfig = {
|
||||||
slug: 'copies',
|
slug: 'copies',
|
||||||
|
access: {
|
||||||
|
read: () => true,
|
||||||
|
update: () => true,
|
||||||
|
create: () => true,
|
||||||
|
admin: () => true,
|
||||||
|
},
|
||||||
|
admin: {
|
||||||
|
useAsTitle: 'label',
|
||||||
|
pagination: {
|
||||||
|
limits: [10, 20, 50, 100, 250],
|
||||||
|
},
|
||||||
|
},
|
||||||
fields: [
|
fields: [
|
||||||
|
{
|
||||||
|
name: 'label',
|
||||||
|
type: 'text',
|
||||||
|
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: 'condition',
|
name: 'condition',
|
||||||
label: 'Condition out of 5',
|
label: 'Condition out of 5',
|
||||||
@ -26,5 +72,8 @@ export const Copies: CollectionConfig = {
|
|||||||
name: 'notes',
|
name: 'notes',
|
||||||
type: 'richText'
|
type: 'richText'
|
||||||
}
|
}
|
||||||
]
|
],
|
||||||
|
hooks: {
|
||||||
|
afterRead: [afterReadHook]
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
@ -254,6 +254,7 @@ export interface Repository {
|
|||||||
*/
|
*/
|
||||||
export interface Copy {
|
export interface Copy {
|
||||||
id: number;
|
id: number;
|
||||||
|
label?: string | null;
|
||||||
condition?: number | null;
|
condition?: number | null;
|
||||||
book?: (number | null) | Book;
|
book?: (number | null) | Book;
|
||||||
repository?: (number | null) | Repository;
|
repository?: (number | null) | Repository;
|
||||||
@ -461,6 +462,7 @@ export interface RepositoriesSelect<T extends boolean = true> {
|
|||||||
* via the `definition` "copies_select".
|
* via the `definition` "copies_select".
|
||||||
*/
|
*/
|
||||||
export interface CopiesSelect<T extends boolean = true> {
|
export interface CopiesSelect<T extends boolean = true> {
|
||||||
|
label?: T;
|
||||||
condition?: T;
|
condition?: T;
|
||||||
book?: T;
|
book?: T;
|
||||||
repository?: T;
|
repository?: T;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user