@@ -18,18 +18,23 @@ import {
18
18
import { alpha } from "@mui/material/styles" ;
19
19
import { useEffect , useReducer , useState } from "react" ;
20
20
import CloseRoundedIcon from "@mui/icons-material/CloseRounded" ;
21
+ import { useSelector } from "react-redux" ;
22
+
21
23
import {
22
24
useBulkCreateContentModelFieldMutation ,
23
25
useCreateContentModelMutation ,
24
26
useGetContentModelFieldsQuery ,
25
27
useGetContentModelsQuery ,
28
+ useCreateContentItemMutation ,
26
29
} from "../../../../../shell/services/instance" ;
27
- import { ContentModel } from "../../../../../shell/services/types" ;
30
+ import { ContentModel , User } from "../../../../../shell/services/types" ;
28
31
import { notify } from "../../../../../shell/store/notifications" ;
29
32
import { useDispatch } from "react-redux" ;
30
33
import { LoadingButton } from "@mui/lab" ;
31
34
import { useHistory } from "react-router" ;
32
35
import { modelIconMap , modelNameMap } from "../utils" ;
36
+ import { formatPathPart } from "../../../../../utility/formatPathPart" ;
37
+ import { AppState } from "../../../../../shell/store/types" ;
33
38
34
39
interface Props {
35
40
onClose : ( ) => void ;
@@ -80,40 +85,49 @@ export const DuplicateModelDialogue = ({ onClose, model }: Props) => {
80
85
error : createFieldsError ,
81
86
} ,
82
87
] = useBulkCreateContentModelFieldMutation ( ) ;
88
+ const [
89
+ createContentItem ,
90
+ {
91
+ isLoading : isCreatingContentItem ,
92
+ isSuccess : isContentItemCreated ,
93
+ error : createContentItemError ,
94
+ } ,
95
+ ] = useCreateContentItemMutation ( ) ;
96
+ const user : User = useSelector ( ( state : AppState ) => state . user ) ;
83
97
84
- const error = createModelError || createFieldsError ;
98
+ const error = createModelError || createFieldsError || createContentItemError ;
85
99
86
100
useEffect ( ( ) => {
87
101
if ( createModelIsSuccess && createModelData ) {
88
- // If no fields to duplicate just redirect to the new model
89
- if ( fields ?. length ) {
90
- const newFields = fields
91
- . filter ( ( field ) => ! field ?. deletedAt )
92
- . map ( ( field ) => {
93
- const { ZUID , settings, ...rest } = field ;
94
- return {
95
- ...rest ,
96
- settings : {
97
- ...settings ,
98
- list : settings ?. list || false ,
99
- } ,
100
- } ;
101
- } ) ;
102
- createFields ( {
103
- modelZUID : createModelData . data . ZUID ,
104
- fields : newFields ,
105
- } ) ;
102
+ if ( newModel . type === "templateset" ) {
103
+ // Create initial content item for templateset
104
+ createInitialTemplatesetContent ( ) ;
106
105
} else {
107
- history . push ( `/schema/${ createModelData . data . ZUID } ` ) ;
108
- onClose ( ) ;
106
+ // For other model types, immediately just check if there are fields to duplicate
107
+ if ( fields ?. length ) {
108
+ duplicateFields ( ) ;
109
+ } else {
110
+ navigateToModelSchema ( ) ;
111
+ }
109
112
}
110
113
}
111
114
} , [ createModelIsSuccess ] ) ;
112
115
116
+ useEffect ( ( ) => {
117
+ if ( isContentItemCreated ) {
118
+ // Flow only applies to templateset model types
119
+ // Duplicate fields if there any
120
+ if ( fields ?. length ) {
121
+ duplicateFields ( ) ;
122
+ } else {
123
+ navigateToModelSchema ( ) ;
124
+ }
125
+ }
126
+ } , [ isContentItemCreated ] ) ;
127
+
113
128
useEffect ( ( ) => {
114
129
if ( createFieldsIsSuccess ) {
115
- history . push ( `/schema/${ createModelData . data . ZUID } ` ) ;
116
- onClose ( ) ;
130
+ navigateToModelSchema ( ) ;
117
131
}
118
132
} , [ createFieldsIsSuccess ] ) ;
119
133
@@ -129,6 +143,50 @@ export const DuplicateModelDialogue = ({ onClose, model }: Props) => {
129
143
}
130
144
} , [ error ] ) ;
131
145
146
+ const navigateToModelSchema = ( ) => {
147
+ history . push ( `/schema/${ createModelData ?. data . ZUID } ` ) ;
148
+ onClose ( ) ;
149
+ } ;
150
+
151
+ const createInitialTemplatesetContent = ( ) => {
152
+ createContentItem ( {
153
+ modelZUID : createModelData . data . ZUID ,
154
+ body : {
155
+ web : {
156
+ pathPart : formatPathPart ( newModel . label ) ,
157
+ canonicalTagMode : 1 ,
158
+ metaLinkText : newModel . label ,
159
+ metaTitle : newModel . label ,
160
+ parentZUID : "0" ,
161
+ } ,
162
+ meta : {
163
+ contentModelZUID : createModelData . data . ZUID ,
164
+ createdByUserZUID : user . ZUID ,
165
+ } ,
166
+ } ,
167
+ } ) ;
168
+ } ;
169
+
170
+ const duplicateFields = ( ) => {
171
+ const newFields = fields
172
+ . filter ( ( field ) => ! field ?. deletedAt )
173
+ . map ( ( field ) => {
174
+ const { ZUID , settings, ...rest } = field ;
175
+ return {
176
+ ...rest ,
177
+ settings : {
178
+ ...settings ,
179
+ list : settings ?. list || false ,
180
+ } ,
181
+ } ;
182
+ } ) ;
183
+
184
+ createFields ( {
185
+ modelZUID : createModelData . data . ZUID ,
186
+ fields : newFields ,
187
+ } ) ;
188
+ } ;
189
+
132
190
return (
133
191
< Dialog
134
192
open
@@ -268,7 +326,11 @@ export const DuplicateModelDialogue = ({ onClose, model }: Props) => {
268
326
< LoadingButton
269
327
variant = "contained"
270
328
disabled = { ! newModel . name || ! newModel . label }
271
- loading = { ! ! createModelIsLoading || ! ! createFieldsIsLoading }
329
+ loading = {
330
+ ! ! createModelIsLoading ||
331
+ ! ! createFieldsIsLoading ||
332
+ ! ! isCreatingContentItem
333
+ }
272
334
onClick = { ( ) =>
273
335
createModel ( {
274
336
...newModel ,
0 commit comments