<commit_before>(ns swarmpit.component.service.create-config
  (:require [material.icon :as icon]
            [material.component :as comp]
            [swarmpit.url :refer [dispatch!]]
            [swarmpit.component.mixin :as mixin]
            [swarmpit.component.handler :as handler]
            [swarmpit.component.state :as state]
            [swarmpit.component.service.form-settings :as settings]
            [swarmpit.component.service.form-ports :as ports]
            [swarmpit.component.service.form-networks :as networks]
            [swarmpit.component.service.form-mounts :as mounts]
            [swarmpit.component.service.form-secrets :as secrets]
            [swarmpit.component.service.form-variables :as variables]
            [swarmpit.component.service.form-labels :as labels]
            [swarmpit.component.service.form-deployment :as deployment]
            [swarmpit.component.service.form-deployment-placement :as placement]
            [swarmpit.component.message :as message]
            [swarmpit.routes :as routes]
            [rum.core :as rum]))

(enable-console-print!)

(defonce step-index (atom 0))

(def steps ["General settings" "Ports" "Networks" "Mounts" "Secrets" "Environment variables" "Labels" "Deployment"])

(def step-style
  {:backgroundColor "transparent"})

(def step-content-style
  {:minWidth "800px"})

(def stepper-style
  {:height "60px"})

(defn- step-item [index form]
  (comp/step
    {:key (str "step-" index)}
    (comp/step-button
      {:key                (str "step-btn-" index)
       :disableTouchRipple true
       :style              step-style
       :className          "step-label"
       :onClick            (fn [] (reset! step-index index))}
      (nth steps index))
    (comp/step-content
      {:key   (str "step-context-" index)
       :style step-content-style}
      form)))

(defn- create-service-handler
  []
  (let [settings (state/get-value settings/cursor)
        ports (state/get-value ports/cursor)
        networks (state/get-value networks/cursor)
        mounts (state/get-value mounts/cursor)
        secrets (state/get-value secrets/cursor)
        variables (state/get-value variables/cursor)
        labels (state/get-value labels/cursor)
        deployment (state/get-value deployment/cursor)]
    (handler/post
      (routes/path-for-backend :service-create)
      {:params     (-> settings
                       (assoc :ports ports)
                       (assoc :networks networks)
                       (assoc :mounts mounts)
                       (assoc :secrets secrets)
                       (assoc :variables variables)
                       (assoc :labels labels)
                       (assoc :deployment deployment))
       :on-success (fn [response]
                     (dispatch!
                       (routes/path-for-frontend :service-info (select-keys response [:id])))
                     (message/info
                       (str "Service " (:id response) " has been created.")))
       :on-error   (fn [response]
                     (message/error
                       (str "Service creation failed. Reason: " (:error response))))})))

(defn init-state
  [registry user repository]
  (reset! step-index 0)
  (if (= "dockerhub" registry)
    (settings/dockerhub-image-tags-handler user repository)
    (settings/registry-image-tags-handler registry repository))
  (state/set-value {:registry    {:name registry
                                  :user user}
                    :repository  {:name repository
                                  :tag  ""
                                  :tags []}
                    :serviceName ""
                    :mode        "replicated"
                    :replicas    1
                    :isValid     false} settings/cursor)
  (state/set-value [] ports/cursor)
  (state/set-value [] networks/cursor)
  (state/set-value [] mounts/cursor)
  (state/set-value [] secrets/cursor)
  (state/set-value [] variables/cursor)
  (state/set-value [] labels/cursor)
  (state/set-value {:autoredeploy  false
                    :restartPolicy {:condition "any"
                                    :delay     5
                                    :attempts  0}
                    :update        {:parallelism   1
                                    :delay         0
                                    :failureAction "pause"}
                    :rollback      {:parallelism   1
                                    :delay         0
                                    :failureAction "pause"}} deployment/cursor)
  (state/set-value [] placement/cursor))

(def init-state-mixin
  (mixin/init
    (fn [data]
      (init-state (:registry data)
                  (:user data)
                  (:repository data)))))

(rum/defc form < rum/reactive
                 init-state-mixin [data]
  (let [index (rum/react step-index)
        {:keys [secrets volumes networks placement]} data
        {:keys [isValid]} (state/react settings/cursor)]
    [:div
     [:div.form-panel
      [:div.form-panel-left
       (comp/panel-info icon/services "New service")]
      [:div.form-panel-right
       (comp/mui
         (comp/raised-button
           {:label      "Create"
            :disabled   (not isValid)
            :primary    true
            :onTouchTap create-service-handler}))]]
     (comp/mui
       (comp/stepper
         {:activeStep  index
          :linear      false
          :style       stepper-style
          :orientation "vertical"}
         (step-item 0 (settings/form false))
         (step-item 1 (ports/form-create))
         (step-item 2 (networks/form-create networks))
         (step-item 3 (mounts/form-create volumes))
         (step-item 4 (secrets/form-create secrets))
         (step-item 5 (variables/form-create))
         (step-item 6 (labels/form-create))
         (step-item 7 (deployment/form placement))))]))<commit_msg>cleanup service create layout<commit_after>(ns swarmpit.component.service.create-config
  (:require [material.icon :as icon]
            [material.component :as comp]
            [swarmpit.url :refer [dispatch!]]
            [swarmpit.component.mixin :as mixin]
            [swarmpit.component.handler :as handler]
            [swarmpit.component.state :as state]
            [swarmpit.component.service.form-settings :as settings]
            [swarmpit.component.service.form-ports :as ports]
            [swarmpit.component.service.form-networks :as networks]
            [swarmpit.component.service.form-mounts :as mounts]
            [swarmpit.component.service.form-secrets :as secrets]
            [swarmpit.component.service.form-variables :as variables]
            [swarmpit.component.service.form-labels :as labels]
            [swarmpit.component.service.form-deployment :as deployment]
            [swarmpit.component.service.form-deployment-placement :as placement]
            [swarmpit.component.message :as message]
            [swarmpit.routes :as routes]
            [rum.core :as rum]))

(enable-console-print!)

(defonce step-index (atom 0))

(def steps ["General settings" "Ports" "Networks" "Mounts" "Secrets" "Environment variables" "Labels" "Deployment"])

(def step-style
  {:backgroundColor "transparent"})

(def step-content-style
  {:minWidth "800px"})

(def stepper-style
  {:height "60px"})

(defn- step-item [index form]
  (comp/step
    {:key (str "step-" index)}
    (comp/step-button
      {:key                (str "step-btn-" index)
       :disableTouchRipple true
       :style              step-style
       :className          "step-label"
       :onClick            (fn [] (reset! step-index index))}
      (nth steps index))
    (comp/step-content
      {:key   (str "step-context-" index)
       :style step-content-style}
      form)))

(defn- create-service-handler
  []
  (let [settings (state/get-value settings/cursor)
        ports (state/get-value ports/cursor)
        networks (state/get-value networks/cursor)
        mounts (state/get-value mounts/cursor)
        secrets (state/get-value secrets/cursor)
        variables (state/get-value variables/cursor)
        labels (state/get-value labels/cursor)
        deployment (state/get-value deployment/cursor)]
    (handler/post
      (routes/path-for-backend :service-create)
      {:params     (-> settings
                       (assoc :ports ports)
                       (assoc :networks networks)
                       (assoc :mounts mounts)
                       (assoc :secrets secrets)
                       (assoc :variables variables)
                       (assoc :labels labels)
                       (assoc :deployment deployment))
       :on-success (fn [response]
                     (dispatch!
                       (routes/path-for-frontend :service-info (select-keys response [:id])))
                     (message/info
                       (str "Service " (:id response) " has been created.")))
       :on-error   (fn [response]
                     (message/error
                       (str "Service creation failed. Reason: " (:error response))))})))

(defn init-state
  [registry user repository]
  (reset! step-index 0)
  (state/set-value {:registry    {:name registry
                                  :user user}
                    :repository  {:name repository
                                  :tag  ""
                                  :tags []}
                    :serviceName ""
                    :mode        "replicated"
                    :replicas    1
                    :isValid     false} settings/cursor)
  (state/set-value [] ports/cursor)
  (state/set-value [] networks/cursor)
  (state/set-value [] mounts/cursor)
  (state/set-value [] secrets/cursor)
  (state/set-value [] variables/cursor)
  (state/set-value [] labels/cursor)
  (state/set-value {:autoredeploy  false
                    :restartPolicy {:condition "any"
                                    :delay     5
                                    :attempts  0}
                    :update        {:parallelism   1
                                    :delay         0
                                    :failureAction "pause"}
                    :rollback      {:parallelism   1
                                    :delay         0
                                    :failureAction "pause"}} deployment/cursor)
  (state/set-value [] placement/cursor))

(def init-state-mixin
  (mixin/init
    (fn [data]
      (let [registry (:registry data)
            repository (:repository data)
            user (:user data)]
        (init-state (:registry data)
                    (:user data)
                    (:repository data))
        (mounts/volumes-handler)
        (networks/networks-handler)
        (secrets/secrets-handler)
        (placement/placement-handler)
        (if (= "dockerhub" registry)
          (settings/dockerhub-tags-handler user repository)
          (settings/registry-tags-handler registry repository))))))

(rum/defc form < rum/reactive
                 init-state-mixin [_]
  (let [index (rum/react step-index)
        {:keys [isValid]} (state/react settings/cursor)]
    [:div
     [:div.form-panel
      [:div.form-panel-left
       (comp/panel-info icon/services "New service")]
      [:div.form-panel-right
       (comp/mui
         (comp/raised-button
           {:label      "Create"
            :disabled   (not isValid)
            :primary    true
            :onTouchTap create-service-handler}))]]
     (comp/mui
       (comp/stepper
         {:activeStep  index
          :linear      false
          :style       stepper-style
          :orientation "vertical"}
         (step-item 0 (settings/form false))
         (step-item 1 (ports/form-create))
         (step-item 2 (networks/form-create))
         (step-item 3 (mounts/form-create))
         (step-item 4 (secrets/form-create))
         (step-item 5 (variables/form-create))
         (step-item 6 (labels/form-create))
         (step-item 7 (deployment/form))))]))
{'fraction_non_alphanumeric': 0.1166652758073938, 'fraction_numerical': 0.0036718684803471583, 'mean_word_length': 1.6478126380910296, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 18, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7387644', 'n_tokens_mistral': 3433, 'n_tokens_neox': 3245, 'n_words': 739}