Skip to content
Snippets Groups Projects
new.vue 4.18 KiB
Newer Older
  • Learn to ignore specific revisions
  • <!--
    Web client of the Sensor Management System software developed within the
    Helmholtz DataHub Initiative by GFZ and UFZ.
    
    
    Copyright (C) 2020, 2021
    
    - Nils Brinckmann (GFZ, nils.brinckmann@gfz-potsdam.de)
    - Marc Hanisch (GFZ, marc.hanisch@gfz-potsdam.de)
    - Helmholtz Centre Potsdam - GFZ German Research Centre for
      Geosciences (GFZ, https://www.gfz-potsdam.de)
    
    Parts of this program were developed within the context of the
    following publicly funded projects or measures:
    - Helmholtz Earth and Environment DataHub
      (https://www.helmholtz.de/en/research/earth_and_environment/initiatives/#h51095)
    
    Licensed under the HEESIL, Version 1.0 or - as soon they will be
    approved by the "Community" - subsequent versions of the HEESIL
    (the "Licence").
    
    You may not use this work except in compliance with the Licence.
    
    You may obtain a copy of the Licence at:
    https://gitext.gfz-potsdam.de/software/heesil
    
    Unless required by applicable law or agreed to in writing, software
    distributed under the Licence is distributed on an "AS IS" basis,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
    implied. See the Licence for the specific language governing
    permissions and limitations under the Licence.
    -->
    <template>
      <div>
    
        <ProgressIndicator
          v-model="isInProgress"
          :dark="isSaving"
        />
    
    Marc Hanisch's avatar
    Marc Hanisch committed
        <v-row>
          <v-col
            cols="12"
            md="5"
          >
            <v-autocomplete :items="allExceptSelected" :item-text="(x) => x" :item-value="(x) => x.id" label="New contact" @change="select" />
          </v-col>
          <v-col
            cols="12"
            md="2"
            align-self="center"
          >
            <v-btn
    
    Marc Hanisch's avatar
    Marc Hanisch committed
              small
              color="primary"
              :disabled="selectedContact == null"
              @click="addContact"
            >
              Add
            </v-btn>
            <v-btn
              small
              text
              nuxt
    
              :to="'/devices/' + deviceId + '/contacts'"
    
    Marc Hanisch's avatar
    Marc Hanisch committed
            >
              Cancel
            </v-btn>
          </v-col>
        </v-row>
    
    import { Component, Vue, Prop } from 'nuxt-property-decorator'
    
    
    import { Contact } from '@/models/Contact'
    
    
    import ProgressIndicator from '@/components/ProgressIndicator.vue'
    
    
        ProgressIndicator
    
      }
    })
    export default class DeviceAddContactPage extends Vue {
      private alreadyUsedContacts: Contact[] = []
      private allContacts: Contact[] = []
      private selectedContact: Contact | null = null
    
      private isLoading: boolean = false
      private isSaving: boolean = false
    
    
      @Prop({
        default: () => [] as Contact[],
        required: true,
        type: Array
      })
      readonly value!: Contact[]
    
      created () {
        this.alreadyUsedContacts = [...this.value] as Contact[]
      }
    
        this.isLoading = true
    
        this.$api.contacts.findAll().then((foundContacts) => {
          this.allContacts = foundContacts
    
          this.isLoading = false
    
    Marc Hanisch's avatar
    Marc Hanisch committed
        }).catch(() => {
    
          this.$store.commit('snackbar/setError', 'Failed to fetch related contacts')
          this.isLoading = false
    
      get isInProgress (): boolean {
        return this.isLoading || this.isSaving
      }
    
    
    Marc Hanisch's avatar
    Marc Hanisch committed
      addContact (): void {
    
    Nils Brinckmann's avatar
    Nils Brinckmann committed
        if (this.selectedContact && this.selectedContact.id && this.isLoggedIn) {
    
          this.isSaving = true
    
          this.$api.devices.addContact(this.deviceId, this.selectedContact.id).then(() => {
    
            this.isSaving = false
    
            this.alreadyUsedContacts.push(this.selectedContact as Contact)
            this.$emit('input', this.alreadyUsedContacts)
    
    Marc Hanisch's avatar
    Marc Hanisch committed
            this.$router.push('/devices/' + this.deviceId + '/contacts')
    
    Marc Hanisch's avatar
    Marc Hanisch committed
          }).catch(() => {
    
            this.isSaving = false
    
    Marc Hanisch's avatar
    Marc Hanisch committed
            this.$store.commit('snackbar/setError', 'Failed to add a contact')
    
    Marc Hanisch's avatar
    Marc Hanisch committed
      select (newContactId: string): void {
    
        const idx = this.allContacts.findIndex((c: Contact) => c.id === newContactId)
        if (idx > -1) {
          this.selectedContact = this.allContacts[idx]
        } else {
          this.selectedContact = null
        }
      }
    
    
    Marc Hanisch's avatar
    Marc Hanisch committed
      get allExceptSelected (): Contact[] {
    
        return this.allContacts.filter(c => !this.alreadyUsedContacts.find(rc => rc.id === c.id))
      }
    
    
    Marc Hanisch's avatar
    Marc Hanisch committed
      get deviceId (): string {
    
        return this.$route.params.deviceId
      }
    
    Nils Brinckmann's avatar
    Nils Brinckmann committed
    
    
    Marc Hanisch's avatar
    Marc Hanisch committed
      get isLoggedIn (): boolean {
    
    Nils Brinckmann's avatar
    Nils Brinckmann committed
        return this.$store.getters['oidc/isAuthenticated']
      }