Adds subscriptions
This commit is contained in:
@@ -1,12 +0,0 @@
|
||||
import { ref, computed } from 'vue'
|
||||
import { defineStore } from 'pinia'
|
||||
|
||||
export const useCounterStore = defineStore('counter', () => {
|
||||
const count = ref(0)
|
||||
const doubleCount = computed(() => count.value * 2)
|
||||
function increment() {
|
||||
count.value++
|
||||
}
|
||||
|
||||
return { count, doubleCount, increment }
|
||||
})
|
||||
71
src/stores/subscriptionStore.js
Normal file
71
src/stores/subscriptionStore.js
Normal file
@@ -0,0 +1,71 @@
|
||||
import {defineStore} from "pinia";
|
||||
import {useSessionStorage} from "@vueuse/core";
|
||||
import {useClient} from "@/plugins/api.js";
|
||||
import {useAuthStore} from "@/stores/authStore.js";
|
||||
import {watch} from "vue";
|
||||
|
||||
export const useSubscriptionStore = defineStore(
|
||||
'subscription',
|
||||
() => {
|
||||
|
||||
const subscriptions = useSessionStorage(
|
||||
'subscription-subscriptions',
|
||||
{})
|
||||
|
||||
const authStore = useAuthStore()
|
||||
const authWatcher = watch(
|
||||
() => authStore.isAuthenticated,
|
||||
async (newValue) => {
|
||||
if (newValue) {
|
||||
await loadSubscriptions()
|
||||
} else {
|
||||
subscriptions.value = {}
|
||||
}
|
||||
})
|
||||
|
||||
function isSubscribeTo(creatorId) {
|
||||
return !!subscriptions.value[creatorId];
|
||||
}
|
||||
|
||||
async function subscribeTo(creatorId) {
|
||||
try {
|
||||
const client = useClient()
|
||||
const response = await client.post(
|
||||
`/api/creators/${creatorId}/subscribe`
|
||||
);
|
||||
subscriptions.value[creatorId] = response.data;
|
||||
} catch (error) {
|
||||
console.error("Error subscribing to creator", error);
|
||||
}
|
||||
}
|
||||
|
||||
async function unsubscribeFrom(creatorId) {
|
||||
try {
|
||||
const client = useClient()
|
||||
const response = await client.post(
|
||||
`/api/creators/${creatorId}/unsubscribe`
|
||||
);
|
||||
delete subscriptions.value[creatorId];
|
||||
} catch (error) {
|
||||
console.error("Error unsubscribing from creator", error);
|
||||
}
|
||||
}
|
||||
|
||||
async function loadSubscriptions() {
|
||||
try {
|
||||
const client = useClient()
|
||||
const response = await client.get(`/api/subscriptions`);
|
||||
|
||||
subscriptions.value = response.data.reduce(
|
||||
(acc, sub) => {
|
||||
acc[sub.creatorId] = sub;
|
||||
return acc;
|
||||
},
|
||||
{});
|
||||
} catch (error) {
|
||||
console.error("Error loading subscriptions:", error);
|
||||
}
|
||||
}
|
||||
|
||||
return {subscriptions, isSubscribeTo, subscribeTo, unsubscribeFrom}
|
||||
});
|
||||
Reference in New Issue
Block a user