Add caching for popular videos

Also add the button to refresh the cache
This commit is contained in:
Mykyta Poturai 2020-08-13 17:26:20 +03:00
parent 62c588dad6
commit 739dddf74c
4 changed files with 61 additions and 18 deletions

View File

@ -3,6 +3,7 @@ import FtToastEvents from '../../components/ft-toast/ft-toast-events'
const state = { const state = {
isSideNavOpen: false, isSideNavOpen: false,
sessionSearchHistory: [], sessionSearchHistory: [],
popularCache: undefined,
searchSettings: { searchSettings: {
sortBy: 'relevance', sortBy: 'relevance',
time: '', time: '',
@ -42,6 +43,10 @@ const getters = {
return state.sessionSearchHistory return state.sessionSearchHistory
}, },
getPopularCache () {
return state.popularCache
},
getSearchSettings () { getSearchSettings () {
return state.searchSettings return state.searchSettings
} }
@ -113,6 +118,10 @@ const mutations = {
} }
}, },
setPopularCache (state, value) {
state.popularCache = value
},
setSearchSortBy (state, value) { setSearchSortBy (state, value) {
state.searchSettings.sortBy = value state.searchSettings.sortBy = value
}, },

View File

@ -4,6 +4,12 @@
margin-bottom: 60px; margin-bottom: 60px;
} }
.floatingTopButton {
position: absolute;
top: 70px;
right: 10px;
}
@media only screen and (max-width: 680px) { @media only screen and (max-width: 680px) {
.card { .card {
width: 90%; width: 90%;

View File

@ -2,13 +2,15 @@ import Vue from 'vue'
import FtLoader from '../../components/ft-loader/ft-loader.vue' import FtLoader from '../../components/ft-loader/ft-loader.vue'
import FtCard from '../../components/ft-card/ft-card.vue' import FtCard from '../../components/ft-card/ft-card.vue'
import FtElementList from '../../components/ft-element-list/ft-element-list.vue' import FtElementList from '../../components/ft-element-list/ft-element-list.vue'
import FtIconButton from '../../components/ft-icon-button/ft-icon-button.vue'
export default Vue.extend({ export default Vue.extend({
name: 'Popular', name: 'Popular',
components: { components: {
'ft-loader': FtLoader, 'ft-loader': FtLoader,
'ft-card': FtCard, 'ft-card': FtCard,
'ft-element-list': FtElementList 'ft-element-list': FtElementList,
'ft-icon-button': FtIconButton
}, },
data: function () { data: function () {
return { return {
@ -16,20 +18,30 @@ export default Vue.extend({
shownResults: [] shownResults: []
} }
}, },
computed: {
popularCache: function () {
return this.$store.getters.getPopularCache
}
},
mounted: function () { mounted: function () {
this.getTrendingInfo() this.getTrendingInfo()
}, },
methods: { methods: {
getTrendingInfo: function () { refreshTrendingInfo: async function () {
this.isLoading = true await this.fetchTrendingInfo()
await this.getTrendingInfo()
},
fetchTrendingInfo: async function () {
const searchPayload = { const searchPayload = {
resource: 'popular', resource: 'popular',
id: '', id: '',
params: {} params: {}
} }
this.$store.dispatch('invidiousAPICall', searchPayload).then((result) => { const result = await this.$store.dispatch('invidiousAPICall', searchPayload).catch((err) => {
console.log(err)
})
if (!result) { if (!result) {
return return
} }
@ -39,12 +51,22 @@ export default Vue.extend({
const returnData = result.filter((item) => { const returnData = result.filter((item) => {
return item.type === 'video' || item.type === 'shortVideo' || item.type === 'channel' || item.type === 'playlist' return item.type === 'video' || item.type === 'shortVideo' || item.type === 'channel' || item.type === 'playlist'
}) })
this.$store.commit('setPopularCache', returnData)
return returnData
},
this.shownResults = this.shownResults.concat(returnData) getTrendingInfo: async function () {
this.isLoading = true
let data = this.popularCache
if (!data || data.length < 1) {
data = await this.fetchTrendingInfo()
}
if (!data) {
return
}
this.shownResults = this.shownResults.concat(data)
this.isLoading = false this.isLoading = false
}).catch((err) => {
console.log(err)
})
} }
} }
}) })

View File

@ -13,6 +13,12 @@
:data="shownResults" :data="shownResults"
/> />
</ft-card> </ft-card>
<ft-icon-button
icon="sync"
class="floatingTopButton"
:size="10"
@click="refreshTrendingInfo"
/>
</div> </div>
</template> </template>