/ config_portal / frontend / app / common / submitConfig.ts
submitConfig.ts
 1  const cleanDataBeforeSubmit = (data: Record<string, unknown>) => {
 2    const cleaned = { ...data };
 3    if (cleaned.namespace && !String(cleaned.namespace).endsWith("/")) {
 4      cleaned.namespace += "/";
 5    }
 6    if (cleaned.container_started) {
 7      delete cleaned.container_started;
 8    }
 9    if (cleaned.broker_type === "container") {
10      cleaned.broker_type = "solace";
11    }
12    return cleaned;
13  };
14  
15  export async function submitInitConfig(
16    data: Record<string, unknown>
17  ): Promise<{ error: string | null }> {
18    const cleaned = cleanDataBeforeSubmit(data);
19    try {
20      const response = await fetch("api/save_config", {
21        method: "POST",
22        headers: { "Content-Type": "application/json" },
23        body: JSON.stringify({ ...cleaned, force: true }),
24      });
25  
26      const result = await response.json();
27  
28      if (!response.ok) {
29        return {
30          error: `HTTP error ${response.status}: ${result.message ?? "Unknown error"}`,
31        };
32      }
33  
34      if (result.status !== "success") {
35        return { error: result.message ?? "Failed to save configuration" };
36      }
37  
38      try {
39        await fetch("api/shutdown", { method: "POST" });
40      } catch (shutdownError) {
41        console.error("Error sending shutdown request:", shutdownError);
42      }
43  
44      return { error: null };
45    } catch (error) {
46      return {
47        error: error instanceof Error ? error.message : "An unknown error occurred",
48      };
49    }
50  }