Moved fetch call for getting charts to api.js

This commit is contained in:
2019-12-26 00:28:33 +01:00
parent 6269f178e9
commit 1c7a688cb8
2 changed files with 25 additions and 10 deletions

View File

@@ -335,6 +335,25 @@ const unlinkPlexAccount = (username, password) => {
} }
// - - - User graphs - - -
const fetchChart = (urlPath, days, chartType) => {
const url = new URL('v1/user' + urlPath, SEASONED_URL)
url.searchParams.append('days', days)
url.searchParams.append('y_axis', chartType)
const authorization_token = localStorage.getItem('token')
const headers = authorization_token ? {
'Authorization': authorization_token,
'Content-Type': 'application/json'
} : {}
return fetch(url.href, { headers })
.then(resp => resp.json())
.catch(error => { console.log('api error fetching chart'); throw error })
}
// - - - Random emoji - - - // - - - Random emoji - - -
const getEmoji = () => { const getEmoji = () => {
@@ -412,6 +431,7 @@ export {
login, login,
getSettings, getSettings,
updateSettings, updateSettings,
fetchChart,
getEmoji, getEmoji,
elasticSearchMoviesAndShows elasticSearchMoviesAndShows
} }

View File

@@ -41,6 +41,7 @@
<script> <script>
import store from '@/store' import store from '@/store'
import ToggleButton from '@/components/ui/ToggleButton'; import ToggleButton from '@/components/ui/ToggleButton';
import { fetchChart } from '@/api'
var Chart = require('chart.js'); var Chart = require('chart.js');
Chart.defaults.global.elements.point.radius = 0 Chart.defaults.global.elements.point.radius = 0
@@ -58,13 +59,13 @@ export default {
name: 'Watch activity', name: 'Watch activity',
ref: 'activityCanvas', ref: 'activityCanvas',
data: null, data: null,
urlPath: 'api/v1/user/plays_by_day', urlPath: '/plays_by_day',
graphType: 'line' graphType: 'line'
}, { }, {
name: 'Plays by day of week', name: 'Plays by day of week',
ref: 'playsByDayOfWeekCanvas', ref: 'playsByDayOfWeekCanvas',
data: null, data: null,
urlPath: 'api/v1/user/plays_by_dayofweek', urlPath: '/plays_by_dayofweek',
graphType: 'bar' graphType: 'bar'
}], }],
chartData: [{ chartData: [{
@@ -116,16 +117,10 @@ export default {
} }
for (let chart of charts) { for (let chart of charts) {
const url = new URL(chart.urlPath, 'http://10.0.0.10:31459')
url.searchParams.append('days', this.days)
url.searchParams.append('y_axis', this.selectedChartType.type)
const headers = {
authorization: localStorage.getItem('token')
}
fetch(url.href, { headers })
.then(resp => resp.json()) fetchChart(chart.urlPath, this.days, this.selectedChartType.type)
.then(data => { .then(data => {
this.series = data.data.series.filter(group => group.name === 'TV')[0].data; // plays pr date in groups (movie/tv/music) this.series = data.data.series.filter(group => group.name === 'TV')[0].data; // plays pr date in groups (movie/tv/music)
this.categories = data.data.categories; // dates this.categories = data.data.categories; // dates