Skip to content

Instantly share code, notes, and snippets.

@ktsn
Last active January 26, 2017 16:44
Show Gist options
  • Save ktsn/e39685f7000da8e2f6757cb196060beb to your computer and use it in GitHub Desktop.
Save ktsn/e39685f7000da8e2f6757cb196060beb to your computer and use it in GitHub Desktop.
import { inject, create } from '../../../src/interface'
import Counter from './counter'
const { Getters, Mutations, Actions } = inject('counter', Counter)
interface Todo {
id: number
isCompleted: boolean
title: string
}
class TodosState {
todos: Todo[] = []
filter: 'all' | 'active' | 'completed' = 'all'
}
class TodosGetters extends Getters<TodosState>() {
get all () {
return this.state.todos
}
get active () {
return this.state.todos.filter(t => !t.isCompleted)
}
get completed () {
return this.state.todos.filter(t => t.isCompleted)
}
get filtered () {
return this[this.state.filter]
}
find (id: number): Todo | undefined {
return this.state.todos.filter(t => t.id === id)[0]
}
}
class TodosMutations extends Mutations<TodosState>() {
done (id: number) {
const todo = this.state.todos.filter(t => t.id === id)[0]
todo.isCompleted = true
}
}
class TodosActions extends Actions<TodosState, TodosGetters, TodosMutations>() {
done (id: number) {
const todo = this.getters.find(id)
if (!todo) {
return
}
this.modules.counter.mutations.inc(1)
this.mutations.done(id)
}
}
export default create({
state: TodosState,
getters: TodosGetters,
mutations: TodosMutations,
actions: TodosActions
})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment