@@ -9,8 +9,9 @@ import LogoPlaceholder from "../../../assets/Images/logo_placeholder.svg";
9
9
import Breadcrumbs from "../../../Components/Breadcrumbs" ;
10
10
// Utils
11
11
import { useTheme } from "@emotion/react" ;
12
- import { useState } from "react" ;
12
+ import { useState , useEffect } from "react" ;
13
13
import { useParams } from "react-router-dom" ;
14
+ import { useStatusPageFetch } from "../../StatusPage/Status/Hooks/useStatusPageFetch" ;
14
15
import { useCreateStatusPage } from "../../StatusPage/Create/Hooks/useCreateStatusPage" ;
15
16
import { useLocation } from "react-router-dom" ;
16
17
import { statusPageValidation } from "../../../Validation/validation" ;
@@ -20,20 +21,25 @@ import { useNavigate } from "react-router-dom";
20
21
21
22
const CreateStatus = ( ) => {
22
23
const theme = useTheme ( ) ;
23
- const { monitorId } = useParams ( ) ;
24
+ const { monitorId, url } = useParams ( ) ;
24
25
const navigate = useNavigate ( ) ;
25
26
const location = useLocation ( ) ;
26
- const isCreate = location . pathname . startsWith ( "/distributed-uptime/ status/create" ) ;
27
+ const isCreate = location . pathname . startsWith ( "/status/distributed /create" ) ;
27
28
const [ createStatusPage , isLoading , networkError ] = useCreateStatusPage ( isCreate ) ;
29
+
30
+ const [ statusPage , statusPageMonitors , statusPageIsLoading , statusPageNetworkError ] =
31
+ useStatusPageFetch ( isCreate , url ) ;
32
+
28
33
const BREADCRUMBS = [
29
34
{ name : "distributed uptime" , path : "/distributed-uptime" } ,
30
35
{ name : "details" , path : `/distributed-uptime/${ monitorId } ` } ,
31
- { name : "create status page" , path : `` } ,
36
+ { name : isCreate ? "create status page" : "edit status page", path : `` } ,
32
37
] ;
33
38
// Local state
34
39
const [ form , setForm ] = useState ( {
40
+ type : "distributed" ,
35
41
isPublished : false ,
36
- url : Math . floor ( Math . random ( ) * 1000000 ) . toFixed ( 0 ) ,
42
+ url : url ?? Math . floor ( Math . random ( ) * 1000000 ) . toFixed ( 0 ) ,
37
43
logo : undefined ,
38
44
companyName : "" ,
39
45
monitors : [ monitorId ] ,
@@ -87,8 +93,9 @@ const CreateStatus = () => {
87
93
if ( typeof error === "undefined" ) {
88
94
const success = await createStatusPage ( { form : formToSubmit } ) ;
89
95
if ( success ) {
90
- createToast ( { body : "Status page created successfully" } ) ;
91
- navigate ( `/distributed-uptime/status/${ form . url } ` ) ;
96
+ const verb = isCreate ? "created" : "updated" ;
97
+ createToast ( { body : `Status page ${ verb } successfully` } ) ;
98
+ navigate ( `/status/distributed/${ form . url } ` ) ;
92
99
}
93
100
return ;
94
101
}
@@ -99,6 +106,36 @@ const CreateStatus = () => {
99
106
setErrors ( ( prev ) => ( { ...prev , ...newErrors } ) ) ;
100
107
} ;
101
108
109
+ // If we are configuring, populate fields
110
+ useEffect ( ( ) => {
111
+ if ( isCreate ) return ;
112
+ if ( typeof statusPage === "undefined" ) {
113
+ return ;
114
+ }
115
+
116
+ let newLogo = undefined ;
117
+ if ( statusPage . logo ) {
118
+ newLogo = {
119
+ src : `data:${ statusPage . logo . contentType } ;base64,${ statusPage . logo . data } ` ,
120
+ name : "logo" ,
121
+ type : statusPage . logo . contentType ,
122
+ size : null ,
123
+ } ;
124
+ }
125
+
126
+ setForm ( ( prev ) => {
127
+ return {
128
+ ...prev ,
129
+ companyName : statusPage ?. companyName ,
130
+ isPublished : statusPage ?. isPublished ,
131
+ timezone : statusPage ?. timezone ,
132
+ monitors : statusPageMonitors . map ( ( monitor ) => monitor . _id ) ,
133
+ color : statusPage ?. color ,
134
+ logo : newLogo ,
135
+ } ;
136
+ } ) ;
137
+ } , [ isCreate , statusPage , statusPageMonitors ] ) ;
138
+
102
139
return (
103
140
< Stack gap = { theme . spacing ( 10 ) } >
104
141
< Breadcrumbs list = { BREADCRUMBS } />
@@ -159,6 +196,7 @@ const CreateStatus = () => {
159
196
name = "url"
160
197
type = "url"
161
198
label = "Your status page address"
199
+ disabled = { ! isCreate }
162
200
value = { form . url }
163
201
onChange = { handleFormChange }
164
202
helperText = { errors [ "url" ] }
0 commit comments