0
0
Fork 0

Add Axios and data fetching to the MainView

This commit is contained in:
Tyler-A 2020-08-03 22:03:24 -06:00
parent b8c4ed9c4b
commit 647727c388
2 changed files with 53 additions and 3 deletions

View file

@ -70,6 +70,7 @@
</template> </template>
<script> <script>
import * as axios from "axios";
import Icon from "./Icon.vue"; import Icon from "./Icon.vue";
export default { export default {
@ -147,6 +148,30 @@ export default {
duration: this.duration, duration: this.duration,
}); });
}, },
},
mounted: function() {
this.$nextTick(function() {
axios.get(
`${this.api_url}/me`,
{ headers: { Authorization: `Bearer ${this.token}` } }
).then((response) => {
if (response.error) {
window.location.hash = ``;
window.location.href = `${this.auth.redirect}?error=${encodeURI(response.error)}`;
return
}
let data = response.data;
// Set the Vue user object
this.user.name = data.display_name;
this.user.image = data.images.length > 0 ? data.images[0].url : ``;
}).catch((err) => {
window.location.hash = ``;
window.location.href = `${this.auth.redirect}?error=${encodeURI(err)}`;;
return
})
});
} }
} }
</script> </script>

View file

@ -10,7 +10,8 @@
</template> </template>
<script> <script>
import ControlCard from "./ControlBar.vue" import * as axios from "axios";
import ControlCard from "./ControlBar.vue";
export default { export default {
name: `MainView`, name: `MainView`,
@ -21,14 +22,38 @@ export default {
components: { components: {
Control: ControlCard Control: ControlCard
}, },
data() { return {};}, data() { return {
data: [],
error: ``,
api_base: `https://api.spotify.com/v1`,
};},
computed: {}, computed: {},
methods: { methods: {
get_token() {
let params = new URLSearchParams(window.location.hash.slice(1));
return params.get(`access_token`);
},
handle_export() { handle_export() {
console.log("Handling the export"); console.log("Handling the export");
}, },
get_data() { get_data(config) {
console.log("Fetching data from Spotify") console.log("Fetching data from Spotify")
console.log(config)
let url = `${this.api_base}/me/top/${config.type.toLowerCase()}`;
let limit = parseInt(config.count) || 10;
url += `?limit=${limit}&time_range=${config.duration}`;
axios.get(
url,
{ headers: { Authorization: `Bearer ${this.get_token()}` } }
).then((response) => {
this.error = ``;
this.data = response.data.items;
}).catch((err) => {
this.error = `${err.name}: ${err.message}`
});
}, },
} }
} }