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

@ -16,7 +16,9 @@
<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>

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

@ -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) => {
return resolve({
json: await r.json(),
status: r.status
})
})
.catch((err) => { .catch((err) => {
console.error(err) console.error(err)
return resolve({ error: 'CRITICAL_ERROR' }) return resolve({
error: 'CRITICAL_ERROR'
})
}) })
return resolve(r.data)
}) })
} }

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]
@ -41,22 +48,33 @@ async function submitForm() {
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>
<FormNotFound v-if="isFormNotFound" />
<div v-else>
<div v-if="isSent">Форма была успешно отправлена.</div>
<div v-else>
<h2>{{ data.name }}</h2> <h2>{{ data.name }}</h2>
<p>{{ currentPage.text }}</p> <p>{{ currentPage.text }}</p>
@ -93,8 +111,13 @@ onMounted(async () => {
@input="answers[question.id].value = $event" @input="answers[question.id].value = $event"
/> />
</div> </div>
<button type="submit">Отправить</button> <button type="submit" v-if="currentPageNumber === pageCount - 1">
Отправить
</button>
<button type="submit" v-else>Дальше</button>
</form> </form>
</div>
</div>
</template> </template>
<style scoped></style> <style scoped></style>