瀏覽代碼

work on endpoint

Ivan Galakhov 4 年之前
父節點
當前提交
63f37451a7

+ 33 - 2
dashboard/src/main/home/cluster-dashboard/expanded-chart/NotificationSettingsSection.tsx

@@ -1,13 +1,17 @@
-import React, { useState } from "react";
+import React, { useContext, useState } from "react";
 import Heading from "../../../../components/form-components/Heading";
 import CheckboxRow from "../../../../components/form-components/CheckboxRow";
 import Helper from "../../../../components/form-components/Helper";
 import SaveButton from "../../../../components/SaveButton";
+import api from "../../../../shared/api";
+import { Context } from "../../../../shared/Context";
+import { ChartType } from "../../../../shared/types";
 
 const NOTIF_CATEGORIES = ["deploy", "success", "fail"];
 
 interface Props {
   disabled?: boolean;
+  currentChart: ChartType;
 }
 
 const NotificationSettingsSection: React.FC<Props> = (props) => {
@@ -21,6 +25,33 @@ const NotificationSettingsSection: React.FC<Props> = (props) => {
     }, {})
   );
 
+  const { currentProject, currentCluster } = useContext(Context);
+
+  const saveChanges = () => {
+    let payload = {
+      enabled: notificationsOn,
+      deploy: notificationsOn,
+      ...categories,
+    };
+
+    api
+      .updateNotificationConfig(
+        "<token>",
+        {
+          payload: JSON.stringify(payload),
+          namespace: props.currentChart.namespace,
+        },
+        {
+          project_id: currentProject.id,
+          cluster_id: currentCluster.id,
+          name: props.currentChart.name,
+        }
+      )
+      .then((data) => {
+        console.log(data);
+      });
+  };
+
   return (
     <>
       <Heading>Notification Settings</Heading>
@@ -55,7 +86,7 @@ const NotificationSettingsSection: React.FC<Props> = (props) => {
         </>
       )}
       <SaveButton
-        onClick={() => {}}
+        onClick={() => saveChanges()}
         text={"Save Changes"}
         clearPosition={true}
         statusPosition={"right"}

+ 1 - 1
dashboard/src/main/home/cluster-dashboard/expanded-chart/SettingsSection.tsx

@@ -264,7 +264,7 @@ const SettingsSection: React.FC<PropsType> = ({
       {!loadingWebhookToken ? (
         <StyledSettingsSection showSource={showSource}>
           {renderWebhookSection()}
-          <NotificationSettingsSection />
+          <NotificationSettingsSection currentChart={currentChart} />
           <Heading>Additional Settings</Heading>
           <Button color="#b91133" onClick={() => setShowDeleteOverlay(true)}>
             Delete {currentChart.name}

+ 15 - 0
dashboard/src/shared/api.tsx

@@ -268,6 +268,20 @@ const deleteSlackIntegration = baseApi<
   return `/api/projects/${pathParams.project_id}/slack_integrations/${pathParams.slack_integration_id}`;
 });
 
+const updateNotificationConfig = baseApi<
+  {
+    payload: any;
+    namespace: string;
+  },
+  {
+    project_id: number;
+    cluster_id: number;
+    name: string;
+  }
+>("POST", (pathParams) => {
+  return `/api/projects/${pathParams.project_id}/releases/${pathParams.name}/notifications`;
+});
+
 const deployTemplate = baseApi<
   {
     templateName: string;
@@ -1034,6 +1048,7 @@ export default {
   deleteProject,
   deleteRegistryIntegration,
   deleteSlackIntegration,
+  updateNotificationConfig,
   createSubdomain,
   deployTemplate,
   deployAddon,

+ 7 - 0
internal/models/notification.go

@@ -11,3 +11,10 @@ type NotificationConfig struct {
 	Success bool `gorm:"default:true"`
 	Failure bool `gorm:"default:true"`
 }
+
+type NotificationConfigExternal struct {
+	Enabled bool `json:"enabled"`
+	Deploy  bool `json:"deploy"`
+	Success bool `json:"success"`
+	Failure bool `json:"failure"`
+}

+ 36 - 2
server/api/notifications_handler.go

@@ -1,8 +1,42 @@
 package api
 
-import "net/http"
+import (
+	"encoding/json"
+	"fmt"
+	"github.com/go-chi/chi"
+	"net/http"
+)
 
-// HandleUpdateNotifications updates notification settings for a given release
+type HandleUpdateNotificationConfigForm struct {
+	Payload struct {
+		Enabled bool `json:"enabled"`
+		Deploy  bool `json:"deploy"`
+		Success bool `json:"success"`
+		Failure bool `json:"failure"`
+	} `json:"payload"`
+	Namespace string `json:"namespace"`
+	ClusterID uint   `json:"cluster_id"`
+}
+
+// HandleUpdateNotificationConfig updates notification settings for a given release
 func (app *App) HandleUpdateNotificationConfig(w http.ResponseWriter, r *http.Request) {
+	name := chi.URLParam(r, "name")
+
+	form := &HandleUpdateNotificationConfigForm{}
+
+	if err := json.NewDecoder(r.Body).Decode(form); err != nil {
+		app.handleErrorFormDecoding(err, ErrProjectDecode, w)
+		return
+	}
+
+	release, err := app.Repo.Release.ReadRelease(form.ClusterID, name, form.Namespace)
+
+	if err != nil {
+		app.sendExternalError(err, http.StatusInternalServerError, HTTPError{
+			Code:   ErrReleaseReadData,
+			Errors: []string{"release not found"},
+		}, w)
+	}
 
+	fmt.Println(release)
 }