Skip to content

Commit

Permalink
feat: add songlists store and songlists effect
Browse files Browse the repository at this point in the history
  • Loading branch information
Clarkkkk committed Jul 28, 2024
1 parent 62f9626 commit d0319e5
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/App.vue
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
useLyricsEffect,
useMediaSessionEffect,
usePlayStatusEffect,
useSonglistsEffect,
useThemeEffect
} from './services'
import './global.css'
Expand All @@ -21,6 +22,7 @@ useLikeEffect()
useThemeEffect()
useDebugEffect()
useGlobalKeyboardListeners()
useSonglistsEffect()
const route = useRoute()
</script>
Expand Down
5 changes: 4 additions & 1 deletion src/api/ApiUserSubcount.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,8 @@
export interface ApiUserSubcount {
api: '/user/subcount'
method: 'post'
return: ApiResponse
return: {
createdPlaylistCount: number
subPlaylistCount: number
}
}
1 change: 1 addition & 0 deletions src/services/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,5 @@ export * from './useLyricsEffect'
export * from './useMediaSessionEffect'
export * from './usePlayStatusEffect'
export * from './useSonglist'
export * from './useSonglistsEffect'
export * from './useThemeEffect'
16 changes: 16 additions & 0 deletions src/services/useSonglistsEffect.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { watchEffect } from 'vue'
import { storeToRefs } from 'pinia'
import { useProfileStore, useSonglistsStore } from 'stores'

export function useSonglistsEffect() {
const { updateSonglists } = useSonglistsStore()
const { profile } = storeToRefs(useProfileStore())

let initialized = false
watchEffect(async () => {
if (profile.value?.profile.userId && !initialized) {
initialized = true
await updateSonglists()
}
})
}
1 change: 1 addition & 0 deletions src/stores/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,4 @@ export * from './useLyricsStore'
export * from './usePlaylistStore'
export * from './usePreferenceStore'
export * from './useProfileStore'
export * from './useSonglistsStore'
41 changes: 41 additions & 0 deletions src/stores/useSonglistsStore.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
import { ref } from 'vue'
import type { ApiUserPlaylist, ApiUserSubcount } from 'api'
import { defineStore, storeToRefs } from 'pinia'
import { post } from 'utils'
import { useProfileStore } from './useProfileStore'

export const useSonglistsStore = defineStore('songlists', () => {
const { profile } = storeToRefs(useProfileStore())
const createdCount = ref(0)
const collectedCount = ref(0)
const createdSonglists = ref<ApiUserPlaylist['return']['playlist']>([])
const collectedSonglists = ref<ApiUserPlaylist['return']['playlist']>([])

async function updateCount() {
if (!profile.value?.profile.userId) return
const res = await post<ApiUserSubcount>('/user/subcount')
createdCount.value = res.createdPlaylistCount
collectedCount.value = res.subPlaylistCount
}

async function updateSonglists() {
if (!profile.value?.profile.userId) return
await updateCount()
const res = await post<ApiUserPlaylist>('/user/playlist', {
uid: profile.value.profile.userId,
offset: 0
})
createdSonglists.value = createdSonglists.value.concat(
res.playlist.slice(0, createdCount.value)
)
collectedSonglists.value = collectedSonglists.value.concat(
res.playlist.slice(createdCount.value + 1)
)
}

return {
updateSonglists,
createdSonglists,
collectedSonglists
}
})

0 comments on commit d0319e5

Please sign in to comment.