summaryrefslogtreecommitdiffstats
path: root/webapp/src/components/BackendModuleEdit.vue
blob: 90045f4561f7655960855409bb9d2d48881ae4a7 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
<i18n>
{
  "en": {
    "select_backend_type": "Select a backend type.",
    "input_credentials": "Fill in the backend details.",
    "backend_name": "Backend name",
    "backend_empty_error": "This field can not be empty.",
    "backend_type": "Backend type",
    "backendtype_empty_error": "Please choose a backend type.",
    "stepper_optional": "optional"
  },
  "de": {
    "select_backend_type": "Wähle einen Backend typ aus.",
    "input_credentials": "Gib die benötigten Backend Daten ein.",
    "backend_name": "Backend Name",
    "backend_empty_error": "Dieses Feld darf nicht leer sein.",
    "backend_type": "Backend Typ",
    "backendtype_empty_error": "Bitte wähle einen Backendtyp aus.",
    "stepper_optional": "Optional"
  }
}
</i18n>

<template>
  <v-form class="edit-backend-form" ref="form" v-model="valid" @submit.prevent="submit" lazy-validation>
    <v-stepper v-model="step" horizontal>
      <v-stepper-header>
        <v-stepper-step
          :complete="stepCompleted >= 1"
          step="1"
          :editable="stepCompleted >= 1"
          edit-icon="check"
          :rules="[() => true]"
        >{{ $t('select_backend_type') }}</v-stepper-step>
        <v-divider></v-divider>
        <v-stepper-step
          :complete="stepCompleted >= 2"
          step="2"
          :editable="stepCompleted >= 2"
          edit-icon="check"
        >{{ $t('input_credentials') }}</v-stepper-step>
        <v-divider></v-divider>
        <v-stepper-step
          :complete="stepCompleted >= 3"
          step="3"
          :editable="stepCompleted >= 3"
          edit-icon="check"
        >{{ $t('test_connection') }}
        <small>{{ $t('stepper_optional') }}</small>
        </v-stepper-step>
      </v-stepper-header>
      <v-stepper-items>
        <v-stepper-content step="1" class="stepper-content">
          <v-select
            offset-y
            v-model="backendType"
            :items="backendChoices"
            :label="$t('backend_type')"
            :rules="[() => !!backendType || $t('backendtype_empty_error')]"
            @change="loadInputFields"
          ></v-select>
        </v-stepper-content>
        <v-stepper-content step="2" class="stepper-content">
          <v-text-field
            v-model="backendName"
            :label="$t('backend_name')"
            :rules="[() => !!backendName || $t('backend_empty_error')]"
            ref="backendName"
          ></v-text-field>
          <template v-for="element in elements">
            <v-text-field
              v-if="element.type == 'text'"
              label="TODO:"
              :key="element.name"
              v-model="element.value"
              :prepend-icon="element.icon"
            ></v-text-field>
            <v-text-field
              v-if="element.type == 'password'"
              label="TODOOO yoo"
              :key="element.name"
              :append-icon="element.show ? 'visibility_off' : 'visibility'"
              :type="element.show ? 'text' : 'password'"
              @click:append="element.show = !element.show"
              v-model="element.value"
            ></v-text-field>
            <v-switch
              v-if="element.type == 'switch'"
              v-model="element.value"
              :label="element.name"
              :key="element.name"
              color="primary"
            ></v-switch>
            <v-select
              offset-y
              v-if="element.type == 'select'"
              :items="element.items"
              label="Standard"
              :key="element.name"
              v-model="element.value"
            ></v-select>
          </template>
        </v-stepper-content>
        <v-stepper-content step="3" class="stepper-content">
          <v-btn color="primary">{{ $t('work_in_progress') }}</v-btn>
        </v-stepper-content>
      </v-stepper-items>

<v-divider></v-divider>
    <v-layout>
      <v-flex xl10 offset-xl1 lg12 text-xs-right>
        <v-btn flat @click.native="$store.commit('backends/setDialog', { show: false })">{{ $t('cancel') }}</v-btn>
        <v-btn color="primary" v-show="step == 1" @click.native="completeStepOne()">{{ $t('continue') }}</v-btn>
        <v-btn color="primary" v-show="step == 2" @click.native="completeStepTwo()">{{ $t('continue') }}</v-btn>
        <v-btn type="submit" v-show="step == 3" class="primary" raised>{{ $t('submit') }}</v-btn>
      </v-flex>
    </v-layout>
    </v-stepper>
  </v-form>
</template>

<script>

export default {
  name: 'BackendModuleEdit',
  props: ['backendId'],
  data () {
    return {
      valid: true,
      step: 1,
      stepCompleted: 0,
      backendChoices: [],
      elements: [],
      backendType: '',
      backendName: '',
      loadData: false
    }
  },
  methods: {
    submit (event) {
      if (this.$refs.form.validate()) {
        var credentials = []
        this.elements.forEach(function (element) {
          const e = { id: element.id, value: element.value }
          credentials.push(e)
        })

        this.$http.post('/api/backends/saveBackend', {
          backendId: this.backendId,
          backendName: this.backendName,
          backendType: this.backendType,
          backendCredentials: credentials
        }).then(response => {
          // TODO: Add backend saved successfull msg.
          console.log('TODO: Implement snackbar and print backend added successfully msg.')
          this.$store.dispatch('backends/loadData')
          this.$store.commit('backends/setDialog', { show: false })
        }).catch(error => {
          console.log(error)
          // if (error.response.data.status === '') {
          // }
          // this.$refs.form.validate()
        })
      }
    },
    loadBackendTypes () {
      this.$http('/api/backends/getBackendTypes').then(response => {
        this.backendChoices = response.data
      })
    },
    loadInputFields () {
      if (!this.loadData) {
        this.$http('/api/backends/getCredentialsByType?type=' + this.backendType).then(response => {
          this.elements = response.data
        })
      }
    },
    loadBackend (backendId) {
      this.$http('/api/backends/getBackendInfoById?id=' + this.backendId).then(response => {
        this.backendName = response.data.backendName
        this.loadData = true
        this.backendType = response.data.backendType
        const credentialValues = JSON.parse(response.data.backendCredentials)
        this.$http('/api/backends/getCredentialsByType?type=' + this.backendType).then(res => {
          var credentials = res.data

          // Make an array merge to combine the credentials with the values.
          var mergedCredentials = credentials.map(x => Object.assign(x, credentialValues.find(y => y.id === x.id)))
          this.elements = mergedCredentials
        })
      })
    },
    completeStepOne () {
      // Error handling
      if (this.backendType !== '') {
        this.step = 2
        this.stepCompleted = Math.max(1, this.stepCompleted)
      } else {
        this.$refs.form.validate()
      }
    },
    completeStepTwo () {
      // Error handling
      if (this.backendName !== '') {
        this.step = 3
        this.stepCompleted = Math.max(2, this.stepCompleted)
      } else {
        this.$refs.form.validate()
      }
    }
  },
  computed: {
  },
  beforeMount () {
    this.loadBackendTypes()
    if (this.backendId !== 0) {
      this.loadBackend(this.backendId)
      this.step = 2
      this.stepCompleted = 3
    }
  },
  watch: {
  }
}
</script>

<!-- Add "scoped" attribute to limit CSS to this component only -->
<style scoped>
.stepper-content {
  height: 600px;
  overflow: auto;
}
</style>