Skip to content

Commit 4f147c2

Browse files
committed
Collect all information on survey to submit on backend
1 parent 37c9447 commit 4f147c2

File tree

2 files changed

+34
-22
lines changed

2 files changed

+34
-22
lines changed

vue/src/components/viewer/QuestionViewer.vue

Lines changed: 31 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +11,12 @@
1111
<div class="mt-3">
1212
<div v-if="question.type === 'select'">
1313
<select
14+
:value="modelValue"
15+
@change="emits('update:modelValue', $event.target.value)"
1416
class="mt-1 block w-full py-2 px-3 border border-gray-300 bg-white rounded-md shadow-sm focus:outline-none focus:ring-indigo-500 focus:border-indigo-500 sm:text-sm"
1517
>
16-
<option v-for="option in question.data.options" :key="option.uuid">
18+
<option value="">Please Select</option>
19+
<option v-for="option in question.data.options" :key="option.uuid" :value="option.text">
1720
{{ option.text }}
1821
</option>
1922
</select>
@@ -27,6 +30,8 @@
2730
<input
2831
:id="option.uuid"
2932
:name="'question' + question.id"
33+
:value="option.text"
34+
@change="emits('update:modelValue', $event.target.value)"
3035
type="radio"
3136
class="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300"
3237
/>
@@ -46,6 +51,8 @@
4651
>
4752
<input
4853
:id="option.uuid"
54+
v-model="model[option.text]"
55+
@change="onCheckboxChange"
4956
type="checkbox"
5057
class="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 rounded"
5158
/>
@@ -60,34 +67,50 @@
6067
<div v-else-if="question.type === 'text'">
6168
<input
6269
type="text"
70+
:value="modelValue"
71+
@input="emits('update:modelValue', $event.target.value)"
6372
class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md"
6473
/>
6574
</div>
6675
<div v-else-if="question.type === 'textarea'">
6776
<textarea
68-
:value="value"
69-
@change="emits('input', $event.target.value)"
77+
:value="modelValue"
78+
@input="emits('update:modelValue', $event.target.value)"
7079
class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md"
7180
></textarea>
7281
</div>
73-
<pre>{{value}}</pre>
7482
</div>
7583
</fieldset>
7684
<hr class="mb-4" />
7785
</template>
7886

7987
<script setup>
80-
81-
const { question, index, value } = defineProps({
88+
import { ref } from "vue";
89+
const { question, index, modelValue } = defineProps({
8290
question: Object,
8391
index: Number,
84-
value: String
92+
modelValue: [String, Array],
8593
});
86-
const emits = defineEmits(['input', 'update']);
94+
const emits = defineEmits(["update:modelValue"]);
95+
96+
let model;
97+
if (question.type === "checkbox") {
98+
model = ref({});
99+
}
87100
88101
function shouldHaveOptions() {
89102
return ["select", "radio", "checkbox"].includes(question.type);
90103
}
104+
105+
function onCheckboxChange($event) {
106+
const selectedOptions = [];
107+
for (let uuid in model.value) {
108+
if (model.value[uuid]) {
109+
selectedOptions.push(uuid);
110+
}
111+
}
112+
emits("update:modelValue", selectedOptions);
113+
}
91114
</script>
92115

93116
<style></style>

vue/src/views/SurveyPublicView.vue

Lines changed: 3 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@
1010
<h1 class="text-3xl">{{ survey.title }}</h1>
1111
</div>
1212
</div>
13-
<pre>{{ model }}</pre>
1413
<div v-for="(question, ind) of survey.questions" :key="question.id">
15-
16-
<QuestionViewer v-model="model.questions[question.id]" :question="question" :index="ind" />
14+
<QuestionViewer v-model="questions[question.id]" :question="question" :index="ind" />
1715
</div>
1816

1917
<button
@@ -37,21 +35,12 @@ const store = useStore();
3735
const loading = computed(() => store.state.currentSurvey.loading);
3836
const survey = computed(() => store.state.currentSurvey.data);
3937
40-
const defaultValue = {
41-
questions: {},
42-
};
43-
console.log(survey.value.questions);
44-
if (survey.value.questions) {
45-
for (let question of survey.value.questions) {
46-
defaultValue.questions[question.id] = ''
47-
}
48-
}
49-
const model = ref(defaultValue);
38+
const questions = ref({});
5039
5140
store.dispatch("getSurveyBySlug", route.params.slug);
5241
5342
function submitSurvey() {
54-
console.log(JSON.stringify(model, undefined, 2));
43+
console.log(JSON.stringify(questions.value, undefined, 2));
5544
}
5645
</script>
5746

0 commit comments

Comments
 (0)