aboutsummaryrefslogtreecommitdiff
path: root/src/pages/IndexPage.vue
blob: b97d68e6be93a256d693ea9d702b653dd15e108b (plain)
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
<template>
  <q-page class="row q-pa-md q-gutter-md">
    <div>
      <q-btn label="Log Books" color="primary" @click="books_dialog = true" size="large"/>
    </div>
    <q-dialog v-model="books_dialog" persistent full-width>
      <q-card>
        <q-form @submit.prevent="submit_books">
          <q-card-section
            v-for="(book, index) in books" :key="index" class="row">
            <q-input
              :label="book.title" type="number"
              class="col-10"
              v-model="book_form_data[book.id].progress"
            ></q-input>
            <q-toggle
              v-model="book_form_data[book.id].completed"
              label="Completed?"
              class="col-2"
            />
          </q-card-section>
          <q-card-section>
            <div class="row">
              <q-input
                label="New book" type="text"
                class="col-10"
                v-model="new_book_data.name"
              ></q-input>
              <q-btn label="Submit" @click="submit_book()" color="primary" size="large"/>
            </div>
          </q-card-section>
          <q-card-actions align="right" class="text-primary">
            <q-btn label="Submit" type="submit" color="primary" size="large"/>
            <q-btn flat label="Cancel" v-close-popup size="large"/>
          </q-card-actions>
        </q-form>
      </q-card>
    </q-dialog>
    <q-form class="q-gutter-md col-11" @submit.prevent="submit">
      <div v-for="(form, index) in forms" :key="index">
        <template v-if="form.type === 'multiple_select'">
          <div class="row">
            <q-select
              :label="form.prompt"
              filled
              v-model="form_data[form.prompt_id]"
              use-input
              use-chips
              multiple
              input-debounce="0"
              :options="form.filter_options"
              @filter="form.filterFn"
              @new-value="form.createValue"
              class="col-12"
            ></q-select>
          </div>
        </template>
        <template v-else-if="form.type === 'number'">
          <q-input
            :label="form.prompt"
            type="number"
            v-model="form_data[form.prompt_id]"
          ></q-input>
        </template>
        <template v-else-if="form.type === 'range'">
          <span>{{ form.prompt }}</span>
          <q-slider
            :min="form.extra.min"
            :max="form.extra.max"
            v-model="form_data[form.prompt_id]"
          ></q-slider>
        </template>
        <template v-else-if="form.type === 'text'">
          <!-- TODO autofill -->
          <q-input
            :label="form.prompt"
            type="text"
            v-model="form_data[form.prompt_id]"
          ></q-input>
        </template>
        <template v-else-if="form.type === 'textarea'">
          <q-input
            :label="form.prompt"
            type="text"
            v-model="form_data[form.prompt_id]"
          ></q-input>
        </template>
      </div>
      <q-btn label="Submit" type="submit" color="primary" size="large"/>
    </q-form>
  </q-page>
</template>

<script>
import { defineComponent, ref } from 'vue'
import {
  get_forms,
  get_books,
  submit_data,
  submit_book_data,
  create_option,
  complete_book,
  create_book_datapoint,
  create_book,
} from '../api.js'

export default defineComponent({
  name: 'IndexPage',
  setup() {
    let forms = ref([])
    let form_data = ref({})
    get_forms().then(json => {
      forms.value = json
      forms.value.forEach(f => {
        if(f.type === 'multiple_select'){
          form_data.value[f.prompt_id] = []
          let str_options = f.extra.map(o => o.id)
          f.filter_options = str_options
          f.filterFn = function(val, update) {
            update(() => {
              if (val === '') {
                f.filter_options = str_options
              } else {
                const needle = val.toLowerCase()
                f.filter_options = str_options.filter(
                  v => v.toLowerCase().indexOf(needle) > -1
                )
              }
            })
          }
          f.createValue = function(val, done) {
            if (val.length > 1) {
              if (!str_options.includes(val)) {
                create_option(f, val)
                done(val, 'add-unique')
              }
            }
          }
        }
      })
    })

    let books = ref([])
    let book_form_data = ref({})
    let new_book_data = ref({})
    get_books().then(json => {
      books.value = json
      books.value.forEach( book => {
        book_form_data.value[book.id] = {
          "completed": false,
          "progress": "",
        }
      })
    })
    let books_dialog = ref(false)

    return {
      forms, form_data,
      books, book_form_data, new_book_data,
      books_dialog,
      submit_book(){
        create_book(new_book_data.value.name)
      },
      submit(){
        submit_data(forms.value, form_data.value)
      },
      submit_books(){
        submit_book_data(book_form_data.value)
        books_dialog.value = false
      }
    }
  }
})
</script>