Skip to content
This repository was archived by the owner on Jan 16, 2024. It is now read-only.

Commit 2075c0e

Browse files
committed
fix: existing prefix field scenario
1 parent ad68a58 commit 2075c0e

1 file changed

Lines changed: 25 additions & 6 deletions

File tree

src/fields/getFields.ts

Lines changed: 25 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,15 @@ export const getFields = ({
2727
},
2828
}
2929

30+
const basePrefixField: Field = {
31+
name: 'prefix',
32+
type: 'text',
33+
admin: {
34+
readOnly: true,
35+
disabled: true,
36+
},
37+
}
38+
3039
const fields = [...collection.fields]
3140

3241
// If Payload access control is disabled,
@@ -115,14 +124,24 @@ export const getFields = ({
115124

116125
// If prefix is enabled, save it to db
117126
if (prefix) {
127+
let existingPrefixFieldIndex = -1
128+
129+
const existingPrefixField = fields.find((existingField, i) => {
130+
if ('name' in existingField && existingField.name === 'prefix') {
131+
existingPrefixFieldIndex = i
132+
return true
133+
}
134+
return false
135+
}) as TextField
136+
137+
if (existingPrefixFieldIndex > -1) {
138+
fields.splice(existingPrefixFieldIndex, 1)
139+
}
140+
118141
fields.push({
119-
name: 'prefix',
120-
type: 'text',
142+
...basePrefixField,
143+
...(existingPrefixField || {}),
121144
defaultValue: path.posix.join(prefix),
122-
admin: {
123-
readOnly: true,
124-
disabled: true,
125-
},
126145
})
127146
}
128147

0 commit comments

Comments
 (0)