-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
169 lines (165 loc) · 4.93 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
import { CollectionConfig, Config, FieldAffectingData, GlobalConfig } from 'payload';
import { Field, UnnamedTab } from 'payload';
import { setAuthorsData } from './hooks/setAuthorsData.js';
import { AuthorsInfoPluginConfig, IncomingCollectionVersions } from './types.js';
const defaultConfig: Required<AuthorsInfoPluginConfig> = {
excludedCollections: [],
excludedGlobals: [],
usernameField: 'name',
};
export const addAuthorsFields =
(pluginConfig: AuthorsInfoPluginConfig = {}) =>
(config: Config): Config => {
const mergedConfig: Required<AuthorsInfoPluginConfig> = { ...defaultConfig, ...pluginConfig };
const usersSlug = config.admin?.user;
if (usersSlug === undefined) {
throw new Error('[addAuthorsFields] admin.user field is undefined');
}
if (config.collections !== undefined) {
config.collections
.filter((x) => !mergedConfig.excludedCollections.includes(x.slug))
.forEach((currentCollection) => {
const fields: Field[] = (currentCollection as CollectionConfig).fields;
currentCollection.fields = processFields(
fields,
((currentCollection as CollectionConfig)?.versions as IncomingCollectionVersions)
?.drafts as boolean,
);
currentCollection.hooks = {
...currentCollection.hooks,
beforeChange: [
...((currentCollection.hooks && currentCollection.hooks.beforeChange) || []),
setAuthorsData(
'updator',
'creator',
'publisher',
'publishDate',
mergedConfig.usernameField,
),
],
};
});
}
if (config.globals !== undefined) {
config.globals
.filter((globalConfig) => !mergedConfig.excludedGlobals.includes(globalConfig.slug))
.forEach((globalConfig) => {
globalConfig.hooks = {
...globalConfig.hooks,
beforeChange: [
...((globalConfig.hooks && globalConfig.hooks.beforeChange) || []),
setAuthorsData(
'updator',
'creator',
'publisher',
'publishDate',
mergedConfig.usernameField,
true,
),
],
};
globalConfig.fields = processFields(
globalConfig.fields,
((globalConfig as GlobalConfig)?.versions as IncomingCollectionVersions)
?.drafts as boolean,
);
});
}
return config;
};
const processFields = (fields: Field[], hasDraft: boolean): Field[] => {
if (fields.filter((field) => 'name' in field && field.name == 'createdAt').length == 0) {
fields.push({
name: 'createdAt',
type: 'date',
admin: {
disableBulkEdit: true,
hidden: true,
components: {
Cell: '@rikifrank/authors-info/client#CreatedAtCell',
},
},
// The default sort for list view is createdAt. Thus, enabling indexing by default, is a major performance improvement, especially for large or a large amount of collections.
index: true,
label: ({ t }) => t('general:createdAt'),
});
}
if (fields.filter((field) => 'name' in field && field?.name == 'updatedAt').length == 0) {
fields.push({
name: 'updatedAt',
type: 'date',
admin: {
disableBulkEdit: true,
hidden: true,
components: {
Cell: '@rikifrank/authors-info/client#CreatedAtCell',
},
},
label: ({ t }) => t('general:updatedAt'),
});
}
const authorFields: Field[] = [
{
name: 'creator',
label: 'Created By',
type: 'text',
admin: {
readOnly: true,
},
},
{
name: 'updator',
label: 'Updated By',
type: 'text',
admin: {
readOnly: true,
},
},
];
if (hasDraft) {
authorFields.push(
{
name: 'publishDate',
type: 'date',
admin: {
date: {
pickerAppearance: 'dayAndTime',
displayFormat: 'd MMM yyy: ,h:mm:ss a',
},
components: { Cell: '@rikifrank/authors-info/client#CreatedAtCell' },
},
},
{
name: 'publisher',
label: 'Published By',
type: 'text',
admin: {
readOnly: true,
},
},
);
}
const authorTab: UnnamedTab = {
label: 'Author Data',
fields: authorFields,
};
const hiddenFields = fields.filter(
(field) => (field as FieldAffectingData).admin?.hidden === true,
);
if (fields[0].type == 'tabs') {
fields[0].tabs.push(authorTab);
} else {
const contentTab: UnnamedTab = {
label: 'Content',
fields: [...fields.filter((field) => (field as FieldAffectingData).admin?.hidden !== true)],
};
fields = [
{
type: 'tabs',
tabs: [contentTab, authorTab],
},
...hiddenFields,
];
}
return fields;
};