view page error checking

This commit is contained in:
grey-cat-1908 2024-08-22 09:09:33 +03:00
parent 1e2ee1c2a3
commit b99f1d403d
7 changed files with 150 additions and 113 deletions

View file

@ -0,0 +1,7 @@
<script setup lang="ts"></script>
<template>
<h2>Форма недоступна</h2>
</template>
<style scoped></style>

View file

@ -7,16 +7,18 @@
<div class="rating-options"> <div class="rating-options">
<label v-for="n in range" :key="n" class="rating-option"> <label v-for="n in range" :key="n" class="rating-option">
<input <input
type="radio" type="radio"
:value="n" :value="n"
v-model="selectedValue" v-model="selectedValue"
:required="isRequired" :required="isRequired"
@change="updateValue" @change="updateValue"
/> />
<span>{{ n }}</span> <span>{{ n }}</span>
</label> </label>
</div> </div>
<button v-if="selectedValue !== null" @click="cancelSelection">Отменить выбор</button> <button v-if="selectedValue !== null" @click="cancelSelection">
Отменить выбор
</button>
</div> </div>
</template> </template>
@ -54,7 +56,7 @@ const emit = defineEmits(['input'])
const selectedValue = ref(props.value) const selectedValue = ref(props.value)
const range = computed(() => { const range = computed(() => {
return Array.from({ length: props.max - props.min + 1 }, (_, i) => props.min + i) return Array.from({length: props.max - props.min + 1}, (_, i) => props.min + i)
}) })
function updateValue() { function updateValue() {

View file

@ -2,11 +2,11 @@
<div class="selector-component"> <div class="selector-component">
<div class="options"> <div class="options">
<div <div
v-for="(option, index) in options" v-for="(option, index) in options"
:key="index" :key="index"
class="option" class="option"
@click="toggleSelection(index)" @click="toggleSelection(index)"
:class="{ selected: isSelected(index) }" :class="{ selected: isSelected(index) }"
> >
<span>{{ option.label }}</span> <span>{{ option.label }}</span>
</div> </div>

View file

@ -1,12 +1,12 @@
<template> <template>
<div class="text-question-component"> <div class="text-question-component">
<input <input
type="text" type="text"
v-model="inputValue" v-model="inputValue"
:maxlength="maxLength" :maxlength="maxLength"
:required="isRequired" :required="isRequired"
@input="validateInput" @input="validateInput"
@blur="validateInput" @blur="validateInput"
/> />
<div v-if="error" class="error">{{ error }}</div> <div v-if="error" class="error">{{ error }}</div>
</div> </div>
@ -50,48 +50,48 @@ function validateTIN(value) {
if (len === 10) { if (len === 10) {
const checksum = const checksum =
((2 * digits[0] + ((2 * digits[0] +
4 * digits[1] + 4 * digits[1] +
10 * digits[2] + 10 * digits[2] +
3 * digits[3] + 3 * digits[3] +
5 * digits[4] + 5 * digits[4] +
9 * digits[5] + 9 * digits[5] +
4 * digits[6] + 4 * digits[6] +
6 * digits[7] + 6 * digits[7] +
8 * digits[8]) % 8 * digits[8]) %
11) % 11) %
10 10
return digits[9] === checksum return digits[9] === checksum
} }
if (len === 12) { if (len === 12) {
const checksum1 = const checksum1 =
((7 * digits[0] + ((7 * digits[0] +
2 * digits[1] + 2 * digits[1] +
4 * digits[2] + 4 * digits[2] +
10 * digits[3] + 10 * digits[3] +
3 * digits[4] + 3 * digits[4] +
5 * digits[5] + 5 * digits[5] +
9 * digits[6] + 9 * digits[6] +
4 * digits[7] + 4 * digits[7] +
6 * digits[8] + 6 * digits[8] +
8 * digits[9]) % 8 * digits[9]) %
11) % 11) %
10 10
const checksum2 = const checksum2 =
((3 * digits[0] + ((3 * digits[0] +
7 * digits[1] + 7 * digits[1] +
2 * digits[2] + 2 * digits[2] +
4 * digits[3] + 4 * digits[3] +
10 * digits[4] + 10 * digits[4] +
3 * digits[5] + 3 * digits[5] +
5 * digits[6] + 5 * digits[6] +
9 * digits[7] + 9 * digits[7] +
4 * digits[8] + 4 * digits[8] +
6 * digits[9] + 6 * digits[9] +
8 * digits[10]) % 8 * digits[10]) %
11) % 11) %
10 10
return digits[10] === checksum1 && digits[11] === checksum2 return digits[10] === checksum1 && digits[11] === checksum2
} }

View file

@ -9,7 +9,7 @@ const router = createRouter({
component: () => import('@/views/Index.vue') component: () => import('@/views/Index.vue')
}, },
{ {
path: '/form/view', path: '/form/view/:id',
name: 'View Form', name: 'View Form',
component: () => import('@/views/form/View.vue') component: () => import('@/views/form/View.vue')
} }

View file

@ -1,10 +1,10 @@
export function makeAPIRequest( export function makeAPIRequest(
path: string = '', path: string = '',
method: string = 'GET', method: string = 'GET',
query: any = {}, query: any = {},
body: any = {}, body: any = {},
useAuthorization: boolean = false useAuthorization: boolean = false
): Promise<any> { ): Promise < any > {
return new Promise(async (resolve, _) => { return new Promise(async (resolve, _) => {
let options: any = { let options: any = {
method, method,
@ -23,13 +23,18 @@ export function makeAPIRequest(
finalPath += '?' + new URLSearchParams(query) finalPath += '?' + new URLSearchParams(query)
} }
const r = await fetch(finalPath, options) await fetch(finalPath, options)
.then((r) => r.json()) .then(async (r) => {
.catch((err) => { return resolve({
console.error(err) json: await r.json(),
return resolve({ error: 'CRITICAL_ERROR' }) status: r.status
}) })
})
return resolve(r.data) .catch((err) => {
console.error(err)
return resolve({
error: 'CRITICAL_ERROR'
})
})
}) })
} }

View file

@ -4,11 +4,18 @@ import { makeAPIRequest } from '@/utils/http'
import Scale from '@/components/forms/ScaleQuestion.vue' import Scale from '@/components/forms/ScaleQuestion.vue'
import TextQuestion from '@/components/forms/TextQuestion.vue' import TextQuestion from '@/components/forms/TextQuestion.vue'
import SelectorQuestion from '@/components/forms/SelectorQuestion.vue' import SelectorQuestion from '@/components/forms/SelectorQuestion.vue'
import FormNotFound from '@/components/FormNotFound.vue'
import { useRoute } from 'vue-router'
const route = useRoute()
const data = ref({}) const data = ref({})
const currentPageNumber = ref(0) const currentPageNumber = ref(0)
const pageCount = ref(0)
const currentPage = ref({}) const currentPage = ref({})
const answers = ref([]) const answers = ref([])
const isFormNotFound = ref(true)
const isSent = ref(false)
async function prepareNewPage() { async function prepareNewPage() {
currentPage.value = data.value.pages[currentPageNumber.value] currentPage.value = data.value.pages[currentPageNumber.value]
@ -39,62 +46,78 @@ async function submitForm() {
await prepareNewPage() await prepareNewPage()
} else { } else {
await makeAPIRequest( await makeAPIRequest(
'/answer/create', '/answer/create',
'POST', 'POST',
{ form_id: 1 }, { form_id: Number(route.params.id) },
{ {
values: Array.from(Object.keys(answers.value).map((val) => answers.value[val])) values: Array.from(Object.keys(answers.value).map((val) => answers.value[val]))
} }
) )
isSent.value = true
} }
} }
} }
onMounted(async () => { onMounted(async () => {
data.value = await makeAPIRequest('/form/get', 'GET', { id: 1 }) const formResponse = await makeAPIRequest('/form/get', 'GET', { id: Number(route.params.id) })
if (!formResponse.json || formResponse.status !== 200) {
return
}
data.value = formResponse.json.data
pageCount.value = data.value.pages.length
isFormNotFound.value = false
await prepareNewPage() await prepareNewPage()
}) })
</script> </script>
<template> <template>
<h2>{{ data.name }}</h2> <FormNotFound v-if="isFormNotFound" />
<p>{{ currentPage.text }}</p> <div v-else>
<div v-if="isSent">Форма была успешно отправлена.</div>
<div v-else>
<h2>{{ data.name }}</h2>
<p>{{ currentPage.text }}</p>
<form @submit.prevent="submitForm"> <form @submit.prevent="submitForm">
<div class="" v-for="question in currentPage.questions"> <div class="" v-for="question in currentPage.questions">
<h3>{{ question.label }}</h3> <h3>{{ question.label }}</h3>
<p>{{ question.description }}</p> <p>{{ question.description }}</p>
<TextQuestion <TextQuestion
v-if="question.question_type === 1" v-if="question.question_type === 1"
:minLength="question.min_length" :minLength="question.min_length"
:maxLength="question.max_length" :maxLength="question.max_length"
:validator="question.validator" :validator="question.validator"
:isRequired="question.required" :isRequired="question.required"
v-model="answers[question.id].value" v-model="answers[question.id].value"
@input="answers[question.id].value = $event" @input="answers[question.id].value = $event"
/> />
<SelectorQuestion <SelectorQuestion
v-if="question.question_type === 2" v-if="question.question_type === 2"
:minValues="question.min_values" :minValues="question.min_values"
:maxValues="question.max_values" :maxValues="question.max_values"
:options="question.options" :options="question.options"
:isRequired="question.required" :isRequired="question.required"
v-model="answers[question.id].values" v-model="answers[question.id].values"
@input="answers[question.id].values = $event" @input="answers[question.id].values = $event"
/> />
<Scale <Scale
v-if="question.question_type === 3" v-if="question.question_type === 3"
:min="question.min_value" :min="question.min_value"
:max="question.max_value" :max="question.max_value"
:minLabel="question.min_label" :minLabel="question.min_label"
:maxLabel="question.max_label" :maxLabel="question.max_label"
:isRequired="question.required" :isRequired="question.required"
v-model="answers[question.id].value" v-model="answers[question.id].value"
@input="answers[question.id].value = $event" @input="answers[question.id].value = $event"
/> />
</div>
<button type="submit" v-if="currentPageNumber === pageCount - 1">
Отправить
</button>
<button type="submit" v-else>Дальше</button>
</form>
</div> </div>
<button type="submit">Отправить</button> </div>
</form>
</template> </template>
<style scoped></style> <style scoped></style>