@@ -4,15 +4,30 @@ import { Field } from "@strapi/design-system";
4
4
import { Modal } from "@strapi/design-system" ;
5
5
import React , { useEffect , useRef , useState } from "react" ;
6
6
import { PLUGIN_ID } from "../pluginId" ;
7
- import { getJwtToken , BASE_URL } from "../utils/helpers" ;
8
-
7
+ import { getFetchClient } from '@strapi/strapi/admin' ;
9
8
interface CollectionType {
10
9
uid : string ;
11
10
singularName : string ;
12
11
displayName : string ;
13
12
}
14
13
15
- export default function CollectionTypeModal ( { isOpen, setModalOpen, setNewCollectionTypeAdded, typeToEdit = null , setTypeToEdit, editID = '' , setEditID} : { isOpen : boolean , setModalOpen : React . Dispatch < React . SetStateAction < boolean > > , setNewCollectionTypeAdded : React . Dispatch < React . SetStateAction < boolean > > , typeToEdit : any | null , setTypeToEdit : React . Dispatch < React . SetStateAction < string > > , editID : string , setEditID : React . Dispatch < React . SetStateAction < string > > } ) {
14
+ export default function CollectionTypeModal ( {
15
+ isOpen,
16
+ setModalOpen,
17
+ setNewCollectionTypeAdded,
18
+ typeToEdit = null ,
19
+ setTypeToEdit,
20
+ editID = '' ,
21
+ setEditID,
22
+ } : {
23
+ isOpen : boolean ;
24
+ setModalOpen : React . Dispatch < React . SetStateAction < boolean > > ;
25
+ setNewCollectionTypeAdded : React . Dispatch < React . SetStateAction < boolean > > ;
26
+ typeToEdit : any | null ;
27
+ setTypeToEdit : React . Dispatch < React . SetStateAction < string > > ;
28
+ editID : string ;
29
+ setEditID : React . Dispatch < React . SetStateAction < string > > ;
30
+ } ) {
16
31
const [ type , setType ] = useState ( '' ) ;
17
32
const [ langcode , setLangcode ] = useState ( '' ) ;
18
33
const [ pattern , setPattern ] = useState ( '' ) ;
@@ -31,12 +46,14 @@ export default function CollectionTypeModal({isOpen, setModalOpen, setNewCollect
31
46
const frequencyRef = useRef < HTMLInputElement > ( null ) ;
32
47
const lastModifiedRef = useRef < HTMLInputElement > ( null ) ;
33
48
49
+ const { get, put, post } = getFetchClient ( ) ;
50
+
34
51
const handleInputChange = ( setter : React . Dispatch < React . SetStateAction < string > > ) => ( event : React . ChangeEvent < HTMLInputElement > ) => {
35
- setter ( event . target . value ) ;
36
- } ;
52
+ setter ( event . target . value ) ;
53
+ } ;
37
54
const handleSelectChange = ( setter : React . Dispatch < React . SetStateAction < string > > ) => ( event : string ) => {
38
- setter ( event ) ;
39
- } ;
55
+ setter ( event ) ;
56
+ } ;
40
57
const validateFields = ( ) => {
41
58
if ( ! type ) {
42
59
typeRef . current ?. focus ( ) ;
@@ -63,40 +80,37 @@ export default function CollectionTypeModal({isOpen, setModalOpen, setNewCollect
63
80
64
81
try {
65
82
if ( typeToEdit && editID ) {
66
- response = await fetch ( `${ BASE_URL } /${ PLUGIN_ID } /admin` , {
67
- method : "PUT" ,
68
- headers : {
69
- "Content-Type" : "application/json" ,
70
- 'Authorization' : 'Bearer ' + getJwtToken ( ) ,
71
- } ,
72
- body : JSON . stringify ( { type, langcode, pattern, priority, frequency, lastModified, id : editID } ) ,
83
+ response = await put ( `/${ PLUGIN_ID } /admin` , {
84
+ type,
85
+ langcode,
86
+ pattern,
87
+ priority,
88
+ frequency,
89
+ lastModified,
90
+ id : editID ,
73
91
} ) ;
74
92
} else {
75
- response = await fetch ( ` ${ process . env . STRAPI_ADMIN_BACKEND_URL } /${ PLUGIN_ID } /admin`, {
76
- method : "POST" ,
77
- headers : {
78
- "Content-Type" : "application/json" ,
79
- 'Authorization' : 'Bearer ' + getJwtToken ( ) ,
80
- } ,
81
- body : JSON . stringify ( { type , langcode , pattern , priority , lastModified , frequency} ) ,
93
+ response = await post ( ` /${ PLUGIN_ID } /admin`, {
94
+ type ,
95
+ langcode ,
96
+ pattern ,
97
+ priority ,
98
+ lastModified ,
99
+ frequency,
82
100
} ) ;
83
101
}
84
102
85
- if ( response . ok ) {
86
- const data = await response . json ( ) ;
87
- setNewCollectionTypeAdded ( true ) ;
88
- setType ( '' ) ;
89
- setLangcode ( '' ) ;
90
- setPattern ( '' ) ;
91
- setPriority ( '' ) ;
92
- setFrequency ( '' ) ;
93
- setLastModified ( 'false' ) ;
94
- setEditID ( '' ) ;
95
- setTypeToEdit ( '' ) ;
96
- setModalOpen ( false ) ;
97
- } else {
98
- alert ( 'Error while saving!' ) ;
99
- }
103
+ const data = response . data ;
104
+ setNewCollectionTypeAdded ( true ) ;
105
+ setType ( '' ) ;
106
+ setLangcode ( '' ) ;
107
+ setPattern ( '' ) ;
108
+ setPriority ( '' ) ;
109
+ setFrequency ( '' ) ;
110
+ setLastModified ( 'false' ) ;
111
+ setEditID ( '' ) ;
112
+ setTypeToEdit ( '' ) ;
113
+ setModalOpen ( false ) ;
100
114
} catch ( err ) {
101
115
console . error ( err ) ;
102
116
alert ( 'An unexpected error occurred.' ) ;
@@ -122,43 +136,24 @@ export default function CollectionTypeModal({isOpen, setModalOpen, setNewCollect
122
136
}
123
137
} , [ typeToEdit ] ) ;
124
138
125
-
126
139
useEffect ( ( ) => {
127
140
const getContentTypes = async ( ) => {
128
- const response = await fetch ( `${ BASE_URL } /${ PLUGIN_ID } /admin-get-content-types` , {
129
- method : 'GET' ,
130
- headers : {
131
- 'Content-Type' : 'application/json' ,
132
- 'Authorization' : 'Bearer ' + getJwtToken ( ) ,
133
- } ,
134
- } ) ;
135
-
136
- if ( response . ok ) {
137
- const data = await response . json ( ) ;
141
+ try {
142
+ const { data } = await get ( `/${ PLUGIN_ID } /admin-get-content-types` ) ;
138
143
setCollectionTypes ( data . collectionTypes ) ;
139
- }
140
- else {
144
+ } catch ( error ) {
141
145
alert ( 'Error while loading!' ) ;
142
146
}
143
- }
147
+ } ;
144
148
145
149
const getLocales = async ( ) => {
146
- const response = await fetch ( `${ BASE_URL } /${ PLUGIN_ID } /admin-get-locales` , {
147
- method : "GET" ,
148
- headers : {
149
- "Content-Type" : "application/json" ,
150
- 'Authorization' : 'Bearer ' + getJwtToken ( ) ,
151
- } ,
152
- } ) ;
153
-
154
- if ( response . ok ) {
155
- const data = await response . json ( ) ;
150
+ try {
151
+ const { data } = await get ( `/${ PLUGIN_ID } /admin-get-locales` ) ;
156
152
setLocales ( data ) ;
157
- }
158
- else {
153
+ } catch ( error ) {
159
154
alert ( 'Error while loading!' ) ;
160
155
}
161
- }
156
+ } ;
162
157
163
158
getContentTypes ( ) ;
164
159
getLocales ( ) ;
@@ -167,21 +162,13 @@ export default function CollectionTypeModal({isOpen, setModalOpen, setNewCollect
167
162
useEffect ( ( ) => {
168
163
if ( type ) {
169
164
const getAllowedFields = async ( ) => {
170
- const response = await fetch ( `${ BASE_URL } /${ PLUGIN_ID } /admin-allowed-fields?type=${ type } ` , {
171
- method : 'GET' ,
172
- headers : {
173
- 'Content-Type' : 'application/json' ,
174
- 'Authorization' : 'Bearer ' + getJwtToken ( ) ,
175
- } ,
176
- } ) ;
177
-
178
- const data = await response . json ( ) ;
165
+ const { data } = await get ( `/${ PLUGIN_ID } /admin-allowed-fields?type=${ type } ` ) ;
179
166
180
167
setPatternHint ( 'Possible fields: ' + data . allowedFields . map ( ( field : string ) => `[${ field } ]` ) . join ( ', ' ) ) ;
181
168
if ( pattern === '' ) {
182
169
setPattern ( '/' + data . slug + '/' ) ;
183
170
}
184
- }
171
+ } ;
185
172
getAllowedFields ( ) ;
186
173
}
187
174
} , [ type ] ) ;
@@ -194,8 +181,6 @@ export default function CollectionTypeModal({isOpen, setModalOpen, setNewCollect
194
181
}
195
182
} ;
196
183
197
-
198
-
199
184
return (
200
185
< Modal . Root open = { isOpen } onOpenChange = { ( e : boolean ) => handleOnOpenChange ( e ) } >
201
186
< Modal . Content >
0 commit comments