Add namespace for Meikan

This commit is contained in:
Crow Crowcrow 2018-12-13 21:19:31 +01:00
parent 962341e9ad
commit 507709ab00
Signed by: Crow
GPG Key ID: 45A8E203AF859FD8
7 changed files with 32 additions and 30 deletions

View File

@ -1,11 +1,11 @@
export const GetAnimeList = async (userID: number): Promise<UserAnime[]> => { export const GetAnimeList = async (userID: number): Promise<Meikan.UserAnime[]> => {
const res = await fetch(`https://api.meikan.moe/v1/users/${userID}/anime`) const res = await fetch(`https://api.meikan.moe/v1/users/${userID}/anime`)
const anime: UserAnime[] = await res.json() const anime: Meikan.UserAnime[] = await res.json()
return anime return anime
} }
export const FindUsers = async (name: string): Promise<User[]> => { export const FindUsers = async (name: string): Promise<Meikan.User[]> => {
const res = await fetch('https://api.meikan.moe/v1/users', { const res = await fetch('https://api.meikan.moe/v1/users', {
method: 'POST', method: 'POST',
mode: 'cors', mode: 'cors',
@ -13,8 +13,7 @@ export const FindUsers = async (name: string): Promise<User[]> => {
name, name,
}), }),
}) })
const users: Meikan.User[] = await res.json()
const users: User[] = await res.json()
return users return users
} }

View File

@ -24,6 +24,6 @@ const user = namespace('user')
@Component({}) @Component({})
export default class Base extends Vue { export default class Base extends Vue {
@user.Getter('currentUser') private currentUser!: User | null @user.Getter('currentUser') private currentUser!: Meikan.User | null
} }
</script> </script>

View File

@ -3,8 +3,8 @@ import { RootState } from '@/store'
import { BareActionContext, getStoreBuilder } from 'vuex-typex' import { BareActionContext, getStoreBuilder } from 'vuex-typex'
export interface UserState { export interface UserState {
currentUser: User | null currentUser: Meikan.User | null
foundUsers: User[] foundUsers: Meikan.User[]
} }
const initialState: UserState = { const initialState: UserState = {
@ -23,11 +23,11 @@ const getterCurrentUser = builder.read(function currentUser(state: UserState) {
}) })
// Mutations // Mutations
function setUserList(state: UserState, foundUsers: User[]) { function setUserList(state: UserState, foundUsers: Meikan.User[]) {
state.foundUsers = foundUsers state.foundUsers = foundUsers
} }
function setCurrentUser(state: UserState, currentUser: User) { function setCurrentUser(state: UserState, currentUser: Meikan.User) {
state.currentUser = currentUser state.currentUser = currentUser
} }
@ -43,10 +43,11 @@ function resetFoundUsers(context: BareActionContext<UserState, RootState>) {
} }
export const user = { export const user = {
get foundUsers(): User[] { get foundUsers(): Meikan.User[] {
return getterUserList() return getterUserList()
}, },
getCurrentUser(): User | null {
get currentUser(): Meikan.User | null {
return getterCurrentUser() return getterCurrentUser()
}, },

View File

@ -3,7 +3,7 @@ import { RootState } from '@/store'
import { BareActionContext, getStoreBuilder } from 'vuex-typex' import { BareActionContext, getStoreBuilder } from 'vuex-typex'
export interface UserAnimeState { export interface UserAnimeState {
anime: UserAnime[] anime: Meikan.UserAnime[]
} }
const initialState: UserAnimeState = { const initialState: UserAnimeState = {
@ -17,12 +17,12 @@ const getterUserAnime = builder.read(function anime(state: UserAnimeState) {
}) })
// Mutations // Mutations
function setAnime(state: UserAnimeState, animeList: UserAnime[]) { function setAnime(state: UserAnimeState, animeList: Meikan.UserAnime[]) {
state.anime = animeList state.anime = animeList
} }
// Action // Action
async function fetchUserAnime(context: BareActionContext<UserAnimeState, RootState>, user: User) { async function fetchUserAnime(context: BareActionContext<UserAnimeState, RootState>, user: Meikan.User) {
const list = await GetAnimeList(user.id) const list = await GetAnimeList(user.id)
userAnime.setAnime(list) userAnime.setAnime(list)
@ -33,7 +33,7 @@ function resetUserAnime(context: BareActionContext<UserAnimeState, RootState>) {
} }
export const userAnime = { export const userAnime = {
get anime(): UserAnime[] { get anime(): Meikan.UserAnime[] {
return getterUserAnime() return getterUserAnime()
}, },

View File

@ -3,12 +3,14 @@ declare module '*.vue' {
export default Vue export default Vue
} }
interface User { declare namespace Meikan {
id: number export interface User {
name: string id: number
} name: string
}
interface UserAnime { export interface UserAnime {
id: number id: number
title: string title: string
}
} }

View File

@ -29,16 +29,16 @@ const userAnime = namespace('userAnime')
@Component({}) @Component({})
export default class Home extends Vue { export default class Home extends Vue {
@user.Getter @user.Getter
private currentUser!: User private currentUser!: Meikan.User
@userAnime.Action @userAnime.Action
private resetUserAnime!: () => void private resetUserAnime!: () => void
@userAnime.Action @userAnime.Action
private fetchUserAnime!: (user: User) => Promise<UserAnime[]> private fetchUserAnime!: (user: Meikan.User) => Promise<Meikan.UserAnime[]>
@userAnime.Getter @userAnime.Getter
private anime!: UserAnime[] private anime!: Meikan.UserAnime[]
private created() { private created() {
if (this.anime.length === 0) { if (this.anime.length === 0) {

View File

@ -30,16 +30,16 @@ const userAnimeStore = namespace('userAnime')
@Component({}) @Component({})
export default class Home extends Vue { export default class Home extends Vue {
@userStore.Mutation('setCurrentUser') @userStore.Mutation('setCurrentUser')
private setCurrentUser!: (user: User) => void private setCurrentUser!: (user: Meikan.User) => void
@userStore.Action('findUsers') @userStore.Action('findUsers')
private findUsers!: (name: string) => Promise<User[]> private findUsers!: (name: string) => Promise<Meikan.User[]>
@userStore.Action('resetFoundUsers') @userStore.Action('resetFoundUsers')
private resetFoundUsers!: () => void private resetFoundUsers!: () => void
@userStore.Getter('foundUsers') @userStore.Getter('foundUsers')
private foundUsers!: User[] private foundUsers!: Meikan.User[]
@userAnimeStore.Action @userAnimeStore.Action
private resetUserAnime!: () => void private resetUserAnime!: () => void
@ -53,7 +53,7 @@ export default class Home extends Vue {
this.findUsers(name) this.findUsers(name)
}, 200) }, 200)
private onSelect(user: User) { private onSelect(user: Meikan.User) {
this.setCurrentUser(user) this.setCurrentUser(user)
this.resetUserAnime() this.resetUserAnime()