BasementForm.vue 5.32 KB
Newer Older
abergavenny's avatar
abergavenny committed
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
<script setup>
import { computed, onMounted, onUnmounted, reactive, ref } from 'vue'
import useVuelidate from '@vuelidate/core'
import { required } from '@vuelidate/validators'

import CheckboxWrapper from '@/components/CheckboxWrapper.vue'
import InputGroup from '@/components/InputGroup.vue'
import MessageBox from '@/components/MessageBox.vue'
import SectionTitle from '@/components/SectionTitle.vue'
import { MESSAGES } from '@/data/messages'
import { INSULATING_MATERIAL_OPTIONS } from '@/data/options'
import { resetAlert } from '@/helpers'
import { useBuildingStore } from '@/stores/buildings'

let timeout = null

const message = ref(null)
const messageType = ref(null)

const buildings = useBuildingStore()

// Initial values
const state = reactive({
  basementInsulatingMaterial: null,
  basementInsulatingMaterialThickness: 0,
  basementRefurbishmentComment: '',
  heatedBasement: false,
  insulatedBasementCeiling: false,
  insulatedBasementFloor: false
})

const rules = computed(() => {
  return {
    basementInsulatingMaterial: { required },
    basementInsulatingMaterialThickness: { required },
    basementRefurbishmentComment: {},
    heatedBasement: {},
    insulatedBasementCeiling: {},
    insulatedBasementFloor: {}
  }
})

const v$ = useVuelidate(rules, state)

async function onSubmit() {
  try {
    clearTimeout(timeout)
    message.value = null

    const isValid = await v$.value.$validate()

    if (!isValid) {
      message.value = MESSAGES['INVALID_INPUT']
      messageType.value = 'warning'

      timeout = resetAlert(message)

      return
    }

    const response = await buildings.updateBuildingBasement(buildings.active, state)

    if (response.success) {
      message.value = MESSAGES[response.message]
      messageType.value = 'success'

      await buildings.fetchBuildings()
    } else {
      message.value = MESSAGES[response.message]
      messageType.value = 'danger'
    }

    timeout = resetAlert(message)
  } catch (error) {
    console.error('ERROR:', error.name)
  }
}

onMounted(() => {
  if (buildings.current) {
    state.basementInsulatingMaterial = buildings.current.data.basementInsulatingMaterial
    state.basementInsulatingMaterialThickness = buildings.current.data.basementInsulatingMaterialThickness
    state.basementRefurbishmentComment = buildings.current.data.basementRefurbishmentComment
    state.heatedBasement = buildings.current.data.heatedBasement
    state.insulatedBasementCeiling = buildings.current.data.insulatedBasementCeiling
    state.insulatedBasementFloor = buildings.current.data.insulatedBasementFloor
  }
})

onUnmounted(() => {
  clearTimeout(timeout)
})
</script>

<template>
  <div class="form">
    <form @submit.prevent="onSubmit">
      <div class="form-content">
        <SectionTitle value="Kellerdämmung" />
        <InputGroup :row="true">
          <InputGroup>
            <label for="basement-material">Material</label>
            <select id="basement-material" v-model="state.basementInsulatingMaterial">
              <option disabled>Bitte wählen</option>
              <option v-for="option in INSULATING_MATERIAL_OPTIONS" :value="option.value" :key="option.value">
                {{ option.name }}
              </option>
            </select>
          </InputGroup>
          <InputGroup>
            <label for="basement-thickness">Dicke Dämmstoff (cm)</label>
            <input class="number" id="basement-thickness" type="number"
              v-model="state.basementInsulatingMaterialThickness" />
          </InputGroup>
        </InputGroup>
        <InputGroup :row="true">
          <InputGroup>
            <label for="basement-ceiling">Kellerdecke</label>
            <CheckboxWrapper value="gedämmt">
              <input id="basement-ceiling" type="checkbox" v-model="state.insulatedBasementCeiling" />
            </CheckboxWrapper>
          </InputGroup>
          <InputGroup>
            <label for="basement-floor">Kellerboden</label>
            <CheckboxWrapper value="gedämmt">
              <input id="basement-floor" type="checkbox" v-model="state.insulatedBasementFloor" />
            </CheckboxWrapper>
          </InputGroup>
          <InputGroup>
            <label for="basement-heated">Keller beheizt</label>
            <CheckboxWrapper value="beheizt">
              <input id="basement-heated" type="checkbox" v-model="state.heatedBasement" />
            </CheckboxWrapper>
          </InputGroup>
        </InputGroup>
        <InputGroup>
          <label for="basement-comment">Zusätzliche Kommentare</label>
          <MessageBox msg="Tragen Sie weitere Informationen zum Keller ein." type="basic" />
          <textarea id="basement-comment" :rows="5" v-model="state.basementRefurbishmentComment" />
          <MessageBox v-if="state.heatedBasement && !state.insulatedBasementFloor"
            msg="Hinweis für die Bewertung: Der Kellerboden sollte gedämmt sein." type="success" />
          <MessageBox v-if="!state.heatedBasement && !state.insulatedBasementCeiling"
            msg="Hinweis für die Bewertung: Die Kellerdecke sollte gedämmt sein." type="success" />
        </InputGroup>
      </div>
      <div class="form-section">
        <MessageBox v-if="message" :msg="message" :type="messageType" />
        <button class="button primary" type="submit" :disabled="message">
          <span>Speichern</span>
        </button>
      </div>
    </form>
  </div>
</template>