repod/src/store/subscriptions.js

49 lines
1.2 KiB
JavaScript
Raw Normal View History

import { getCookie, setCookie } from '../utils/cookies.js'
2023-07-07 16:38:53 +00:00
import axios from '@nextcloud/axios'
import { defineStore } from 'pinia'
2023-07-07 16:38:53 +00:00
import { generateUrl } from '@nextcloud/router'
export const useSubscriptions = defineStore('subscriptions', {
state: () => ({
subs: [],
favs: [],
}),
2023-07-07 16:38:53 +00:00
actions: {
2024-08-09 20:51:32 +00:00
async fetch() {
const metrics = await axios.get(
generateUrl('/apps/gpoddersync/personal_settings/metrics'),
)
const subs = [...metrics.data.subscriptions].sort(
(a, b) => b.listenedSeconds - a.listenedSeconds,
)
this.subs = subs.map((sub) => sub.url)
try {
const favs = JSON.parse(getCookie('repod.favorites')) || []
this.favs = favs.map((url) => ({ url }))
} catch {}
},
addFavorite(url) {
this.favs.push({ url })
setCookie(
'repod.favorites',
JSON.stringify(this.favs.map((fav) => fav.url)),
365,
)
},
editFavoriteData(url, data) {
this.favs = this.favs.map((fav) =>
fav.url === url ? { ...fav, ...data } : fav,
)
},
removeFavorite(url) {
this.favs = this.favs.filter((fav) => fav.url !== url)
setCookie(
'repod.favorites',
JSON.stringify(this.favs.map((fav) => fav.url)),
365,
)
2023-07-07 16:38:53 +00:00
},
},
})