Meikan client v2
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

userAnime.ts 1.0KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { GetAnimeList } from '@/domain'
  2. import { RootState } from '@/store'
  3. import { BareActionContext, getStoreBuilder } from 'vuex-typex'
  4. export interface UserAnimeState {
  5. anime: UserAnime[]
  6. }
  7. const initialState: UserAnimeState = {
  8. anime: [],
  9. }
  10. const builder = getStoreBuilder<RootState>().module('userAnime', initialState)
  11. // Getters
  12. const getterUserAnime = builder.read(function anime(state: UserAnimeState) {
  13. return state.anime
  14. })
  15. // Mutations
  16. function setAnime(state: UserAnimeState, animeList: UserAnime[]) {
  17. state.anime = animeList
  18. }
  19. // Action
  20. async function fetchUserAnime(context: BareActionContext<UserAnimeState, RootState>, user: User) {
  21. const list = await GetAnimeList(user.id)
  22. userAnime.setAnime(list)
  23. }
  24. function resetUserAnime(context: BareActionContext<UserAnimeState, RootState>) {
  25. userAnime.setAnime([])
  26. }
  27. export const userAnime = {
  28. get anime(): UserAnime[] {
  29. return getterUserAnime()
  30. },
  31. setAnime: builder.commit(setAnime),
  32. fetchUserAnime: builder.dispatch(fetchUserAnime),
  33. resetUserAnime: builder.dispatch(resetUserAnime),
  34. }