![]() |
VOOZH | about |
We’re so glad you’re here. You can expect all the best TNS content to arrive Monday through Friday to keep you on top of the news and at the top of your game.
Check your inbox for a confirmation email where you can adjust your preferences and even join additional groups.
Follow TNS on your favorite social media networks.
Become a TNS follower on LinkedIn.
Check out the latest featured and trending stories while you wait for your first TNS newsletter.
npx nuxi@latest init xata-quiz-app
cd xata-quiz-app npm install npm run dev
http://localhost:3000.
xata init
.env, .xatarc and src/xata.ts files within your root directory, and includes all necessary credentials to access your database.
Note that the data in these files is automatically generated.
seed folder in the project’s root directory and download the questions CSV file.
There are two ways to add data with its defined schema: either manually through the Xata user interface (UI) or by using the CLI. The Xata CLI will handle the table creation and autogenerate the schema by importing the data from the terminal into the database.
curl --create-dirs -o seed/questions.csv https://raw.githubusercontent.com/Terieyenike/xata-quiz-app/main/seed/questions.csv
seed folder.
textbooleanlink; under Table to link records from in the drop-down menu, select questions from the drop-down. For the column name, type in questionxata pull main
src/xata.ts based on the schema defined on the main branch of your database. If you make any changes to the schema, run xata pull <branch> to update the autogenerated code.
server in the root directory is used to register APIs, which will be vital when calling the APIs in the page of your application.
Within the server directory, create a folder called api with the following files:
getXataClient function and assigns the result of calling the function with a variable named xata. The server uses the configuration sets in the .env file to read its data from the database.
It is also important to note that the export defineEventHandler function returns the data in JSON.
To test the results in your browser, run either http://localhost:3000/api/answers or http://localhost:3000/api/questions.
assets/css/main.css in the root directory to contain the app’s stylesheet:
assets/css/main.css
Now, update the nuxt.config.ts file to accept the changes globally.
nuxt.config.ts
components in the root directory and add these files: Footer.vue, Questions.vue and Result.vue.
components/Footer.vue
components/Questions.vue
components/Result.vue
To see the result in the browser, create a new folder called pages; in it, create a file called index.vue with the following contents:
pages/index.vue
The useSeoMeta composable helps you define your site’s SEO with details that can be found in every modern web app or website, such as title, description, open graph (OG) image, description for social media and so on.
In addition, the results array of objects will be shown only when the user completes the quiz, to report the number of answers the user got right with their respective title and description.
So far, the page looks like this:
Questions component, using the v-if directive with questionsAnswered < questions.length states that if the questions answered are less than the question’s length, show the question component; otherwise, show the Result component with the v-else directive. Finally, apply the attributes :questions and :answers and bind them to the questions and answers array objects.
The numQuestions variable counts the length of the questions array.
components/Questions.vue
The updated code for the Result component accepts the questions and answers properties passed down from the parent component in the defineProps() macro. The getAnswersForQuestion function uses the filter method to create a new array.
pages/index.vue file, pass an attribute called questionsAnswered in the Questions component.
pages/index.vue
In the Questions.vue component, pass the props as follows:
components/Questions.vue
Check whether the current index of the questions answered in the loop matches the index in the database, using the v-show attribute with the qi in the v-for.
Displaying a single question with answers
Questions.vue component:
components/Questions.vue
The selectAnswer() function passes a parameter with the boolean value is_correct and receives an emit event with the value question-answered. Vue requires you to add the emit option, which lets you know what custom events will be emitted from the component.
In the home component, index.vue, pass the event in the Questions component with the questionAnswered function, which is responsible for incrementing both the value of totalCorrect (answer) and the questionsAnswered (questions) on the user’s click.
pages/index.vue
Questions.vue file, use the style bindings and pass an object (:style) to handle the changes dynamically.
Also, within the status element, change the static value to its respective expressions.
components/Questions.vue
index.vue component, add:
pages/index.vue
The Reset button element contains the condition that states that if the number of questions answered is equal to the length of the questions in the database, the user should be returned to the first question.
index.vue component and pass the following in the <Result /> component:
pages/index.vue
Next, update the Result component with the following:
components/Result.vue
In the code block, the resultIndex uses the computed object to compare the minimum and maximum values with the user’s total number of correct answers. If both indexes are true, a result has been found. The currentResult displays the result from the results array, mapping to the index value.
Finally, the computed titleClass property changes the color of the {{ currentResult.title }} expression to either red if the user gives incorrect answers or green for the right answers.
Results screen