|
import { TypedRecord, makeTypedFactory } from 'typed-immutable-record'; |
|
import { List } from 'immutable'; |
|
|
|
import { OtherAction } from './other'; |
|
import combineReducers from './combineReducers'; |
|
|
|
|
|
export type ADD_TASK = 'tasker/tasks/ADD_TASK'; |
|
export const ADD_TASK: ADD_TASK = 'tasker/tasks/ADD_TASK'; |
|
export type AddTaskAction = { |
|
type: ADD_TASK, |
|
payload: { |
|
title: string, |
|
}, |
|
}; |
|
|
|
export function addTask(title: string): AddTaskAction { |
|
return { |
|
type: ADD_TASK, |
|
payload: { title }, |
|
}; |
|
} |
|
|
|
|
|
export type SET_TASK_ESTIMATE = 'tasker/tasks/SET_TASK_ESTIMATE'; |
|
export const SET_TASK_ESTIMATE: SET_TASK_ESTIMATE = 'tasker/tasks/SET_TASK_ESTIMATE'; |
|
export type SetTaskEstimateAction = { |
|
type: SET_TASK_ESTIMATE, |
|
payload: { |
|
index: number, |
|
estimate: number, |
|
}, |
|
}; |
|
|
|
export function setTaskEstimate(index: number, estimate: number): SetTaskEstimateAction { |
|
return { |
|
type: SET_TASK_ESTIMATE, |
|
payload: { index, estimate }, |
|
}; |
|
} |
|
|
|
|
|
export interface Task { |
|
title: string; |
|
estimate?: number; |
|
}; |
|
|
|
export interface TaskRecord extends TypedRecord<TaskRecord>, Task {} |
|
|
|
const makeTask = makeTypedFactory<Task, TaskRecord>({ |
|
title: '', |
|
estimate: undefined |
|
}); |
|
|
|
|
|
export interface Tasks { |
|
allTasks: List<TaskRecord>; |
|
} |
|
|
|
export interface TasksRecord extends TypedRecord<TasksRecord>, Tasks {} |
|
|
|
export const makeTasks = makeTypedFactory<Tasks, TasksRecord>({ |
|
allTasks: List<TaskRecord>(), |
|
}); |
|
|
|
|
|
export type TasksAction = AddTaskAction | SetTaskEstimateAction | OtherAction; |
|
|
|
|
|
function allTasks( |
|
state: List<TaskRecord> = List<TaskRecord>(), |
|
action: TasksAction = OtherAction |
|
): List<TaskRecord> { |
|
switch (action.type) { |
|
case ADD_TASK: |
|
return state.push(makeTask(action.payload)); |
|
case SET_TASK_ESTIMATE: |
|
const newTask = state |
|
.get(action.payload.index) |
|
.set('estimate', action.payload.estimate); // TODO typesafe setters |
|
|
|
return state.set(action.payload.index, newTask); |
|
default: |
|
return state; |
|
} |
|
} |
|
|
|
|
|
export default combineReducers<TasksRecord, Tasks, TasksAction>( |
|
{ |
|
allTasks, |
|
}, |
|
makeTasks() |
|
); |
Hello, did you advance on the creation of this NPM library? I have reached a similar solution with the same packages and I was wondering if this could be an official package =)
Cheers!