POST and GET data with axios

This commit is contained in:
Andreas Zweili 2021-03-17 21:58:55 +01:00
parent 4350cd5bb5
commit ef5c9b777a
6 changed files with 99 additions and 40 deletions

View File

@ -2460,6 +2460,14 @@
"integrity": "sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==", "integrity": "sha512-xh1Rl34h6Fi1DC2WWKfxUTVqRsNnr6LsKz2+hfwDxQJWmrx8+c7ylaqBMcHfl1U1r2dsifOvKX3LQuLNZ+XSvA==",
"dev": true "dev": true
}, },
"axios": {
"version": "0.21.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
"requires": {
"follow-redirects": "^1.10.0"
}
},
"babel-eslint": { "babel-eslint": {
"version": "10.1.0", "version": "10.1.0",
"resolved": "https://registry.npmjs.org/babel-eslint/-/babel-eslint-10.1.0.tgz", "resolved": "https://registry.npmjs.org/babel-eslint/-/babel-eslint-10.1.0.tgz",
@ -5510,8 +5518,7 @@
"follow-redirects": { "follow-redirects": {
"version": "1.13.3", "version": "1.13.3",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.3.tgz", "resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.3.tgz",
"integrity": "sha512-DUgl6+HDzB0iEptNQEXLx/KhTmDb8tZUHSeLqpnjpknR70H0nC2t9N73BK6fN4hOvJ84pKlIQVQ4k5FFlBedKA==", "integrity": "sha512-DUgl6+HDzB0iEptNQEXLx/KhTmDb8tZUHSeLqpnjpknR70H0nC2t9N73BK6fN4hOvJ84pKlIQVQ4k5FFlBedKA=="
"dev": true
}, },
"for-in": { "for-in": {
"version": "1.0.2", "version": "1.0.2",

View File

@ -8,6 +8,7 @@
"lint": "vue-cli-service lint" "lint": "vue-cli-service lint"
}, },
"dependencies": { "dependencies": {
"axios": "^0.21.1",
"core-js": "^3.6.5", "core-js": "^3.6.5",
"vue": "^3.0.0" "vue": "^3.0.0"
}, },

View File

@ -1,6 +1,6 @@
<template> <template>
<learning-survey @survey-submit="storeSurvey"></learning-survey> <learning-survey></learning-survey>
<user-experiences :results="savedSurveyResults"></user-experiences> <user-experiences></user-experiences>
</template> </template>
<script> <script>
@ -10,24 +10,24 @@ import UserExperiences from './components/survey/UserExperiences.vue';
export default { export default {
components: { components: {
LearningSurvey, LearningSurvey,
UserExperiences, UserExperiences
}, }
data() { // data() {
return { // return {
savedSurveyResults: [], // savedSurveyResults: [],
}; // };
}, // },
methods: { // methods: {
storeSurvey(surveyData) { // storeSurvey(surveyData) {
const surveyResult = { // const surveyResult = {
name: surveyData.userName, // name: surveyData.userName,
rating: surveyData.rating, // rating: surveyData.rating,
id: new Date().toISOString(), // id: new Date().toISOString()
}; // };
this.savedSurveyResults.push(surveyResult); // this.savedSurveyResults.push(surveyResult);
console.log(surveyResult); // console.log(surveyResult);
}, // }
}, // }
}; };
</script> </script>
@ -43,4 +43,4 @@ html {
body { body {
margin: 0; margin: 0;
} }
</style> </style>

View File

@ -9,7 +9,13 @@
</div> </div>
<h3>My learning experience was ...</h3> <h3>My learning experience was ...</h3>
<div class="form-control"> <div class="form-control">
<input type="radio" id="rating-poor" value="poor" name="rating" v-model="chosenRating" /> <input
type="radio"
id="rating-poor"
value="poor"
name="rating"
v-model="chosenRating"
/>
<label for="rating-poor">Poor</label> <label for="rating-poor">Poor</label>
</div> </div>
<div class="form-control"> <div class="form-control">
@ -23,12 +29,18 @@
<label for="rating-average">Average</label> <label for="rating-average">Average</label>
</div> </div>
<div class="form-control"> <div class="form-control">
<input type="radio" id="rating-great" value="great" name="rating" v-model="chosenRating" /> <input
type="radio"
id="rating-great"
value="great"
name="rating"
v-model="chosenRating"
/>
<label for="rating-great">Great</label> <label for="rating-great">Great</label>
</div> </div>
<p <p v-if="invalidInput">
v-if="invalidInput" One or more input fields are invalid. Please check your provided data.
>One or more input fields are invalid. Please check your provided data.</p> </p>
<div> <div>
<base-button>Submit</base-button> <base-button>Submit</base-button>
</div> </div>
@ -38,12 +50,14 @@
</template> </template>
<script> <script>
import axios from 'axios';
export default { export default {
data() { data() {
return { return {
enteredName: '', enteredName: '',
chosenRating: null, chosenRating: null,
invalidInput: false, invalidInput: false
}; };
}, },
emits: ['survey-submit'], emits: ['survey-submit'],
@ -55,15 +69,20 @@ export default {
} }
this.invalidInput = false; this.invalidInput = false;
this.$emit('survey-submit', { axios.post(
userName: this.enteredName, 'https://vue-http-demo-8a928-default-rtdb.europe-west1.firebasedatabase.app/surveys.json',
rating: this.chosenRating, { name: this.enteredName, rating: this.chosenRating }
}); );
// this.$emit('survey-submit', {
// userName: this.enteredName,
// rating: this.chosenRating
// });
this.enteredName = ''; this.enteredName = '';
this.chosenRating = null; this.chosenRating = null;
}, }
}, }
}; };
</script> </script>
@ -77,4 +96,4 @@ input[type='text'] {
width: 20rem; width: 20rem;
margin-top: 0.5rem; margin-top: 0.5rem;
} }
</style> </style>

View File

@ -3,7 +3,9 @@
<base-card> <base-card>
<h2>Submitted Experiences</h2> <h2>Submitted Experiences</h2>
<div> <div>
<base-button>Load Submitted Experiences</base-button> <base-button @click="loadExperiences"
>Load Submitted Experiences</base-button
>
</div> </div>
<ul> <ul>
<survey-result <survey-result
@ -18,13 +20,41 @@
</template> </template>
<script> <script>
import axios from 'axios';
import SurveyResult from './SurveyResult.vue'; import SurveyResult from './SurveyResult.vue';
export default { export default {
props: ['results'],
components: { components: {
SurveyResult, SurveyResult
}, },
data() {
return {
results: []
};
},
methods: {
loadExperiences() {
axios
.get(
'https://vue-http-demo-8a928-default-rtdb.europe-west1.firebasedatabase.app/surveys.json'
)
.then(response => {
const data = response.data;
const results = [];
for (const id in data) {
results.push({
id: id,
name: data[id].name,
rating: data[id].rating
});
}
this.results = results;
})
.catch(error => {
console.log(error);
});
}
}
}; };
</script> </script>
@ -34,4 +64,4 @@ ul {
margin: 0; margin: 0;
padding: 0; padding: 0;
} }
</style> </style>

View File

@ -1,3 +1,5 @@
'use strict';
import { createApp } from 'vue'; import { createApp } from 'vue';
import BaseCard from './components/UI/BaseCard.vue'; import BaseCard from './components/UI/BaseCard.vue';