-
Notifications
You must be signed in to change notification settings - Fork 2
/
todosActions.js
49 lines (45 loc) · 1.01 KB
/
todosActions.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
import {
CREATE_TODO,
DELETE_TODO,
GET_TODOS,
UPDATE_TODO,
} from './actionTypes';
import { isServer } from '../utils/universal';
import * as api from '../data/api';
export const getTodos = (_params, db) => {
const promise = isServer ? db.getAll() : api.fetchTodos();
return {
type: GET_TODOS,
payload: promise,
};
};
export const createTodo = (rawTodo, db) => {
const todo = {
...rawTodo,
text: rawTodo.text || '',
completed: Boolean(rawTodo.completed),
};
const promise = isServer ? db.put(todo.id, todo) : api.createTodo(todo);
return {
type: CREATE_TODO,
payload: promise,
};
};
export const updateTodo = (todo, db) => {
const promise = isServer
? db.put(todo.id, todo)
: api.updateTodo(todo).then(() => todo);
return {
type: UPDATE_TODO,
payload: promise,
};
};
export const deleteTodo = (todo, db) => {
const payload = isServer
? db.delete(todo.id)
: api.deleteTodo(todo).then(() => todo);
return {
type: DELETE_TODO,
payload,
};
};