1
- import { type ManagedCollectionFieldInput , framer , type ManagedCollection } from "framer-plugin"
1
+ import { type ManagedCollectionFieldInput , framer , type ManagedCollection , useIsAllowedTo } from "framer-plugin"
2
2
import { useEffect , useState } from "react"
3
- import { type DataSource , dataSourceOptions , mergeFieldsWithExistingFields , syncCollection } from "./data"
3
+ import { type DataSource , dataSourceOptions , mergeFieldsWithExistingFields , syncCollection , syncMethods } from "./data"
4
4
5
5
interface FieldMappingRowProps {
6
6
field : ManagedCollectionFieldInput
7
7
originalFieldName : string | undefined
8
8
isIgnored : boolean
9
9
onToggleDisabled : ( fieldId : string ) => void
10
10
onNameChange : ( fieldId : string , name : string ) => void
11
+ disabled : boolean
11
12
}
12
13
13
14
function FieldMappingRow ( {
@@ -16,13 +17,15 @@ function FieldMappingRow({
16
17
isIgnored,
17
18
onToggleDisabled,
18
19
onNameChange,
20
+ disabled,
19
21
} : FieldMappingRowProps ) {
20
22
return (
21
23
< >
22
24
< button
23
25
type = "button"
24
26
className = { `source-field ${ isIgnored ? "ignored" : "" } ` }
25
27
onClick = { ( ) => onToggleDisabled ( field . id ) }
28
+ disabled = { disabled }
26
29
>
27
30
< input type = "checkbox" checked = { ! isIgnored } tabIndex = { - 1 } readOnly />
28
31
< span > { originalFieldName ?? field . id } </ span >
@@ -39,7 +42,7 @@ function FieldMappingRow({
39
42
</ svg >
40
43
< input
41
44
type = "text"
42
- disabled = { isIgnored }
45
+ disabled = { isIgnored || disabled }
43
46
placeholder = { field . id }
44
47
value = { field . name }
45
48
onChange = { event => onNameChange ( field . id , event . target . value ) }
@@ -135,6 +138,8 @@ export function FieldMapping({ collection, dataSource, initialSlugFieldId }: Fie
135
138
} )
136
139
}
137
140
141
+ const isAllowedToManage = useIsAllowedTo ( "ManagedCollection.setFields" , ...syncMethods )
142
+
138
143
const handleSubmit = async ( event : React . FormEvent < HTMLFormElement > ) => {
139
144
event . preventDefault ( )
140
145
@@ -149,8 +154,11 @@ export function FieldMapping({ collection, dataSource, initialSlugFieldId }: Fie
149
154
try {
150
155
setStatus ( "syncing-collection" )
151
156
152
- const fieldsToSync = fields . filter ( field => ! ignoredFieldIds . has ( field . id ) )
157
+ const fieldsToSync = fields
158
+ . filter ( field => ! ignoredFieldIds . has ( field . id ) )
159
+ . map ( field => ( { ...field , name : field . name . trim ( ) || field . id } ) )
153
160
161
+ await collection . setFields ( fieldsToSync )
154
162
await syncCollection ( collection , dataSource , fieldsToSync , selectedSlugField )
155
163
await framer . closePlugin ( "Synchronization successful" , { variant : "success" } )
156
164
} catch ( error ) {
@@ -188,6 +196,7 @@ export function FieldMapping({ collection, dataSource, initialSlugFieldId }: Fie
188
196
if ( ! selectedField ) return
189
197
setSelectedSlugField ( selectedField )
190
198
} }
199
+ disabled = { ! isAllowedToManage }
191
200
>
192
201
{ possibleSlugFields . map ( possibleSlugField => {
193
202
return (
@@ -210,13 +219,17 @@ export function FieldMapping({ collection, dataSource, initialSlugFieldId }: Fie
210
219
isIgnored = { ignoredFieldIds . has ( field . id ) }
211
220
onToggleDisabled = { toggleFieldDisabledState }
212
221
onNameChange = { changeFieldName }
222
+ disabled = { ! isAllowedToManage }
213
223
/>
214
224
) ) }
215
225
</ div >
216
226
217
227
< footer >
218
228
< hr />
219
- < button disabled = { isSyncing } >
229
+ < button
230
+ disabled = { isSyncing || ! isAllowedToManage }
231
+ title = { isAllowedToManage ? undefined : "Insufficient permissions" }
232
+ >
220
233
{ isSyncing ? < div className = "framer-spinner" /> : `Import ${ dataSourceName } ` }
221
234
</ button >
222
235
</ footer >
0 commit comments