-
Notifications
You must be signed in to change notification settings - Fork 181
/
Copy pathSurveyView.vue
319 lines (294 loc) · 9.71 KB
/
SurveyView.vue
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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
<template>
<PageComponent>
<template v-slot:header>
<div class="flex items-center justify-between">
<h1 class="text-3xl font-bold text-gray-900">
{{ route.params.id ? model.title : "Create a Survey" }}
</h1>
<div class="flex">
<TButton v-if="model.slug" link :href="`/view/survey/${model.slug}`" target="_blank" class="mr-2">
<ExternalLinkIcon class="w-5 h-5" />
View Public link
</TButton>
<TButton v-if="route.params.id" color="red" @click="deleteSurvey()">
<TrashIcon class="w-5 h-5 mr-2" />
Delete
</TButton>
</div>
</div>
</template>
<div v-if="surveyLoading" class="flex justify-center">Loading...</div>
<form v-else @submit.prevent="saveSurvey" class="animate-fade-in-down">
<div class="shadow sm:rounded-md sm:overflow-hidden">
<!-- Survey Fields -->
<div class="px-4 py-5 bg-white space-y-6 sm:p-6">
<!-- Image -->
<div>
<label class="block text-sm font-medium text-gray-700">
Image
</label>
<div class="mt-1 flex items-center">
<img
v-if="model.image_url"
:src="model.image_url"
:alt="model.title"
class="w-64 h-48 object-cover"
/>
<span
v-else
class="flex items-center justify-center h-12 w-12 rounded-full overflow-hidden bg-gray-100"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-[80%] w-[80%] text-gray-300"
viewBox="0 0 20 20"
fill="currentColor"
>
<path
fill-rule="evenodd"
d="M4 3a2 2 0 00-2 2v10a2 2 0 002 2h12a2 2 0 002-2V5a2 2 0 00-2-2H4zm12 12H4l4-8 3 6 2-4 3 6z"
clip-rule="evenodd"
/>
</svg>
</span>
<button
type="button"
class="relative overflow-hidden ml-5 bg-white py-2 px-3 border border-gray-300 rounded-md shadow-sm text-sm leading-4 font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus:ring-2 focus:ring-offset-2 focus:ring-indigo-500"
>
<input
type="file"
@change="onImageChoose"
class="absolute left-0 top-0 right-0 bottom-0 opacity-0 cursor-pointer"
/>
Change
</button>
</div>
</div>
<!--/ Image -->
<!-- Title -->
<div>
<label for="title" class="block text-sm font-medium text-gray-700"
>Title</label
>
<input
type="text"
name="title"
id="title"
v-model="model.title"
autocomplete="survey_title"
class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md"
/>
</div>
<!--/ Title -->
<!-- Description -->
<div>
<label for="about" class="block text-sm font-medium text-gray-700">
Description
</label>
<div class="mt-1">
<textarea
id="description"
name="description"
rows="3"
v-model="model.description"
autocomplete="survey_description"
class="shadow-sm focus:ring-indigo-500 focus:border-indigo-500 mt-1 block w-full sm:text-sm border border-gray-300 rounded-md"
placeholder="Describe your survey"
/>
</div>
</div>
<!-- Description -->
<!-- Expire Date -->
<div>
<label
for="expire_date"
class="block text-sm font-medium text-gray-700"
>Expire Date</label
>
<input
type="date"
name="expire_date"
id="expire_date"
v-model="model.expire_date"
class="mt-1 focus:ring-indigo-500 focus:border-indigo-500 block w-full shadow-sm sm:text-sm border-gray-300 rounded-md"
/>
</div>
<!--/ Expire Date -->
<!-- Status -->
<div class="flex items-start">
<div class="flex items-center h-5">
<input
id="status"
name="status"
type="checkbox"
v-model="model.status"
class="focus:ring-indigo-500 h-4 w-4 text-indigo-600 border-gray-300 rounded"
/>
</div>
<div class="ml-3 text-sm">
<label for="status" class="font-medium text-gray-700"
>Active</label
>
</div>
</div>
<!--/ Status -->
</div>
<!--/ Survey Fields -->
<div class="px-4 py-5 bg-white space-y-6 sm:p-6">
<h3 class="text-2xl font-semibold flex items-center justify-between">
Questions
<!-- Add new question -->
<button
type="button"
@click="addQuestion()"
class="flex items-center text-sm py-1 px-4 rounded-sm text-white bg-gray-600 hover:bg-gray-700"
>
<svg
xmlns="http://www.w3.org/2000/svg"
class="h-4 w-4"
viewBox="0 0 20 20"
fill="currentColor"
>
<path
fill-rule="evenodd"
d="M10 5a1 1 0 011 1v3h3a1 1 0 110 2h-3v3a1 1 0 11-2 0v-3H6a1 1 0 110-2h3V6a1 1 0 011-1z"
clip-rule="evenodd"
/>
</svg>
Add Question
</button>
<!--/ Add new question -->
</h3>
<div v-if="!model.questions.length" class="text-center text-gray-600">
You don't have any questions created
</div>
<div v-for="(question, index) in model.questions" :key="question.id">
<QuestionEditor
:question="question"
:index="index"
@change="questionChange"
@addQuestion="addQuestion"
@deleteQuestion="deleteQuestion"
/>
</div>
</div>
<div class="px-4 py-3 bg-gray-50 text-right sm:px-6">
<TButton>
<SaveIcon class="w-5 h-5 mr-2" />
Save
</TButton>
</div>
</div>
</form>
</PageComponent>
</template>
<script setup>
import { v4 as uuidv4 } from "uuid";
import { computed, ref, watch } from "vue";
import { useRoute, useRouter } from "vue-router";
import { SaveIcon, TrashIcon, ExternalLinkIcon } from '@heroicons/vue/solid'
import store from "../store";
import PageComponent from "../components/PageComponent.vue";
import QuestionEditor from "../components/editor/QuestionEditor.vue";
import TButton from "../components/core/TButton.vue";
const router = useRouter();
const route = useRoute();
// Get survey loading state, which only changes when we fetch survey from backend
const surveyLoading = computed(() => store.state.currentSurvey.loading);
// Create empty survey
let model = ref({
title: "",
slug: "",
status: false,
description: null,
image: null,
image_url: null,
expire_date: null,
questions: [],
});
// Watch to current survey data change and when this happens we update local model
watch(
() => store.state.currentSurvey.data,
(newVal, oldVal) => {
model.value = {
...JSON.parse(JSON.stringify(newVal)),
status: !!newVal.status,
};
}
);
// If the current component is rendered on survey update route we make a request to fetch survey
if (route.params.id) {
store.dispatch("getSurvey", route.params.id);
}
function onImageChoose(ev) {
const file = ev.target.files[0];
const reader = new FileReader();
reader.onload = () => {
// The field to send on backend and apply validations
model.value.image = reader.result;
// The field to display here
model.value.image_url = reader.result;
ev.target.value = "";
};
reader.readAsDataURL(file);
}
function addQuestion(index) {
const newQuestion = {
id: uuidv4(),
type: "text",
question: "",
description: null,
data: {},
};
model.value.questions.splice(index, 0, newQuestion);
}
function deleteQuestion(question) {
model.value.questions = model.value.questions.filter((q) => q !== question);
}
function questionChange(question) {
// Important to explicitelly assign question.data.options, because otherwise it is a Proxy object
// and it is lost in JSON.stringify()
if (question.data.options) {
question.data.options = [...question.data.options];
}
model.value.questions = model.value.questions.map((q) => {
if (q.id === question.id) {
return JSON.parse(JSON.stringify(question));
}
return q;
});
}
/**
* Create or update survey
*/
function saveSurvey() {
let action = "created";
if (model.value.id) {
action = "updated";
}
store.dispatch("saveSurvey", { ...model.value }).then(({ data }) => {
store.commit("notify", {
type: "success",
message: "The survey was successfully " + action,
});
router.push({
name: "SurveyView",
params: { id: data.data.id },
});
});
}
function deleteSurvey() {
if (
confirm(
`Are you sure you want to delete this survey? Operation can't be undone!!`
)
) {
store.dispatch("deleteSurvey", model.value.id).then(() => {
router.push({
name: "Surveys",
});
});
}
}
</script>
<style></style>