|
| 1 | +import React, { useState, useEffect } from "react"; |
| 2 | +import { |
| 3 | + Toggle, |
| 4 | + TextArea, |
| 5 | + Button, |
| 6 | + InlineNotification, |
| 7 | + Loading, |
| 8 | + Form, |
| 9 | + Stack, |
| 10 | +} from "@carbon/react"; |
| 11 | +import { getMaintenanceStatus, updateMaintenanceConfig } from "../services/request"; |
| 12 | +import "../styles/maintenance-config.scss"; |
| 13 | + |
| 14 | +const MaintenanceConfig = () => { |
| 15 | + const [enabled, setEnabled] = useState(false); |
| 16 | + const [startDate, setStartDate] = useState(""); |
| 17 | + const [endDate, setEndDate] = useState(""); |
| 18 | + const [message, setMessage] = useState(""); |
| 19 | + const [loading, setLoading] = useState(true); |
| 20 | + const [saving, setSaving] = useState(false); |
| 21 | + const [notification, setNotification] = useState(null); |
| 22 | + |
| 23 | + // Helper function to format UTC date for datetime-local input |
| 24 | + const formatDateForInput = (isoString) => { |
| 25 | + if (!isoString) return ""; |
| 26 | + // datetime-local expects format: YYYY-MM-DDTHH:mm |
| 27 | + // We keep it in UTC by using the ISO string directly |
| 28 | + return isoString.slice(0, 16); |
| 29 | + }; |
| 30 | + |
| 31 | + // Helper function to convert datetime-local value to UTC ISO string |
| 32 | + const convertToUTC = (dateTimeLocal) => { |
| 33 | + if (!dateTimeLocal) return null; |
| 34 | + // Treat the input as UTC time |
| 35 | + return new Date(dateTimeLocal + ':00Z').toISOString(); |
| 36 | + }; |
| 37 | + |
| 38 | + // Fetch current configuration |
| 39 | + useEffect(() => { |
| 40 | + fetchConfig(); |
| 41 | + }, []); |
| 42 | + |
| 43 | + const fetchConfig = async () => { |
| 44 | + setLoading(true); |
| 45 | + try { |
| 46 | + const response = await getMaintenanceStatus(); |
| 47 | + if (response.type === "GET_MAINTENANCE_STATUS" && response.payload) { |
| 48 | + const config = response.payload; |
| 49 | + setEnabled(config.enabled || false); |
| 50 | + setStartDate(config.start_time ? formatDateForInput(config.start_time) : ""); |
| 51 | + setEndDate(config.end_time ? formatDateForInput(config.end_time) : ""); |
| 52 | + setMessage(config.message || ""); |
| 53 | + } |
| 54 | + } catch (error) { |
| 55 | + console.error("Error fetching maintenance config:", error); |
| 56 | + showNotification("error", "Failed to load maintenance configuration"); |
| 57 | + } finally { |
| 58 | + setLoading(false); |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + const showNotification = (kind, subtitle) => { |
| 63 | + setNotification({ kind, subtitle }); |
| 64 | + setTimeout(() => setNotification(null), 5000); |
| 65 | + }; |
| 66 | + |
| 67 | + const handleSave = async () => { |
| 68 | + // Validation |
| 69 | + if (enabled) { |
| 70 | + if (!startDate || !endDate) { |
| 71 | + showNotification("error", "Start time and end time are required when maintenance is enabled"); |
| 72 | + return; |
| 73 | + } |
| 74 | + if (new Date(convertToUTC(endDate)) <= new Date(convertToUTC(startDate))) { |
| 75 | + showNotification("error", "End time must be after start time"); |
| 76 | + return; |
| 77 | + } |
| 78 | + if (!message.trim()) { |
| 79 | + showNotification("error", "Message is required when maintenance is enabled"); |
| 80 | + return; |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + setSaving(true); |
| 85 | + try { |
| 86 | + const payload = { |
| 87 | + enabled, |
| 88 | + start_time: enabled ? convertToUTC(startDate) : null, |
| 89 | + end_time: enabled ? convertToUTC(endDate) : null, |
| 90 | + message: enabled ? message.trim() : "", |
| 91 | + }; |
| 92 | + |
| 93 | + console.log("Sending payload:", payload); |
| 94 | + |
| 95 | + const response = await updateMaintenanceConfig(payload); |
| 96 | + |
| 97 | + if (response.type === "UPDATE_MAINTENANCE_CONFIG") { |
| 98 | + const successMsg = enabled |
| 99 | + ? "Successfully updated maintenance notification" |
| 100 | + : "Successfully disabled maintenance notification"; |
| 101 | + showNotification("success", successMsg); |
| 102 | + // Refresh the config to show updated values |
| 103 | + await fetchConfig(); |
| 104 | + } else if (response.type === "API_ERROR") { |
| 105 | + const errorMsg = response.payload?.response?.data?.error || response.payload?.message || "Failed to update maintenance configuration"; |
| 106 | + showNotification("error", errorMsg); |
| 107 | + } else { |
| 108 | + showNotification("error", "Failed to update maintenance configuration"); |
| 109 | + } |
| 110 | + } catch (error) { |
| 111 | + console.error("Error updating maintenance config:", error); |
| 112 | + const errorMsg = error.response?.data?.error || error.message || "Failed to update maintenance configuration"; |
| 113 | + showNotification("error", errorMsg); |
| 114 | + } finally { |
| 115 | + setSaving(false); |
| 116 | + } |
| 117 | + }; |
| 118 | + |
| 119 | + if (loading) { |
| 120 | + return ( |
| 121 | + <div className="maintenance-config-loading"> |
| 122 | + <Loading description="Loading maintenance configuration..." withOverlay={false} /> |
| 123 | + </div> |
| 124 | + ); |
| 125 | + } |
| 126 | + |
| 127 | + return ( |
| 128 | + <div className="maintenance-config-container"> |
| 129 | + <h2>Maintenance Notification Configuration</h2> |
| 130 | + <p className="maintenance-config-description"> |
| 131 | + Configure maintenance notifications that will be displayed to all users. |
| 132 | + Notifications will appear 24 hours before the maintenance start time and remain visible until the end time. |
| 133 | + <strong> All times are in UTC timezone.</strong> |
| 134 | + </p> |
| 135 | + |
| 136 | + {notification && ( |
| 137 | + <InlineNotification |
| 138 | + kind={notification.kind} |
| 139 | + title={notification.kind === "success" ? "Success" : "Error"} |
| 140 | + subtitle={notification.subtitle} |
| 141 | + onCloseButtonClick={() => setNotification(null)} |
| 142 | + lowContrast |
| 143 | + /> |
| 144 | + )} |
| 145 | + |
| 146 | + <Form className="maintenance-config-form"> |
| 147 | + <Stack gap={6}> |
| 148 | + <Toggle |
| 149 | + id="maintenance-enabled" |
| 150 | + labelText="Enable Maintenance Notification" |
| 151 | + labelA="Disabled" |
| 152 | + labelB="Enabled" |
| 153 | + toggled={enabled} |
| 154 | + onToggle={(checked) => setEnabled(checked)} |
| 155 | + /> |
| 156 | + |
| 157 | + {enabled && ( |
| 158 | + <> |
| 159 | + <div className="date-picker-group"> |
| 160 | + <label htmlFor="start-date" className="cds--label"> |
| 161 | + Maintenance Start Time (UTC) |
| 162 | + </label> |
| 163 | + <input |
| 164 | + id="start-date" |
| 165 | + type="datetime-local" |
| 166 | + className="cds--text-input datetime-input" |
| 167 | + value={startDate} |
| 168 | + onChange={(e) => setStartDate(e.target.value)} |
| 169 | + onBlur={(e) => setStartDate(e.target.value)} |
| 170 | + step="60" |
| 171 | + min={new Date().toISOString().slice(0, 16)} |
| 172 | + /> |
| 173 | + <div className="cds--form__helper-text"> |
| 174 | + Format: YYYY-MM-DDTHH:mm (e.g., {new Date().getFullYear()}-04-15T10:00) - UTC timezone |
| 175 | + </div> |
| 176 | + </div> |
| 177 | + |
| 178 | + <div className="date-picker-group"> |
| 179 | + <label htmlFor="end-date" className="cds--label"> |
| 180 | + Maintenance End Time (UTC) |
| 181 | + </label> |
| 182 | + <input |
| 183 | + id="end-date" |
| 184 | + type="datetime-local" |
| 185 | + className="cds--text-input datetime-input" |
| 186 | + value={endDate} |
| 187 | + onChange={(e) => setEndDate(e.target.value)} |
| 188 | + onBlur={(e) => setEndDate(e.target.value)} |
| 189 | + step="60" |
| 190 | + min={new Date().toISOString().slice(0, 16)} |
| 191 | + /> |
| 192 | + <div className="cds--form__helper-text"> |
| 193 | + Format: YYYY-MM-DDTHH:mm (e.g., {new Date().getFullYear()}-04-15T14:00) - UTC timezone |
| 194 | + </div> |
| 195 | + </div> |
| 196 | + |
| 197 | + <TextArea |
| 198 | + id="maintenance-message" |
| 199 | + labelText="Notification Message" |
| 200 | + placeholder="Enter the maintenance notification message..." |
| 201 | + value={message} |
| 202 | + onChange={(e) => setMessage(e.target.value)} |
| 203 | + rows={4} |
| 204 | + helperText="This message will be displayed to all users during the maintenance window" |
| 205 | + /> |
| 206 | + </> |
| 207 | + )} |
| 208 | + |
| 209 | + <div className="button-group"> |
| 210 | + <Button |
| 211 | + kind="primary" |
| 212 | + onClick={handleSave} |
| 213 | + disabled={saving} |
| 214 | + > |
| 215 | + {saving ? "Saving..." : "Save Configuration"} |
| 216 | + </Button> |
| 217 | + {enabled && ( |
| 218 | + <Button |
| 219 | + kind="secondary" |
| 220 | + onClick={fetchConfig} |
| 221 | + disabled={saving} |
| 222 | + > |
| 223 | + Reset |
| 224 | + </Button> |
| 225 | + )} |
| 226 | + </div> |
| 227 | + </Stack> |
| 228 | + </Form> |
| 229 | + </div> |
| 230 | + ); |
| 231 | +}; |
| 232 | + |
| 233 | +export default MaintenanceConfig; |
| 234 | + |
0 commit comments