This repository has been archived by the owner on Jan 22, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
/
meals.js
90 lines (84 loc) · 2.21 KB
/
meals.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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/* global Meteor */
import moment from 'moment';
import { newErrorNotification } from './notifications';
import actionTypeBuilder from './actionTypeBuilder';
export const MEALS = actionTypeBuilder.type('MEALS');
export const MEALS_REMOVE = actionTypeBuilder.type('MEALS_REMOVE');
export const MEALS_INSERT = actionTypeBuilder.type('MEALS_INSERT');
export const MEALS_UPDATE = actionTypeBuilder.type('MEALS_UPDATE');
export function loadMealsFactory(mealCollection) {
return (userId, date) => {
const dateStart = moment(date).startOf('day').toDate();
const dateEnd = moment(date).endOf('day').toDate();
const finalUserId = userId || Meteor.userId(); // Override with current user if not specified
return dispatch => {
dispatch({
type: MEALS,
meteor: {
subscribe: () => Meteor.subscribe('meals', dateStart, dateEnd, finalUserId, {
onStop: error => {
if (error && error.error === 401) {
dispatch(newErrorNotification("Vous n'avez pas accès au planning de cet utilisateur."));
}
},
}),
get: () => mealCollection.findByUserAndDate(finalUserId, dateStart, dateEnd).fetch(),
},
});
};
};
}
export function deleteMealFactory(collection) {
return id => {
return dispatch => {
dispatch({
type: MEALS_REMOVE,
meteor: {
remove: {
id,
collection,
},
},
});
};
};
}
export function newMealFactory(collection) {
return (type, name, date) => {
return dispatch => {
dispatch({
type: MEALS_INSERT,
meteor: {
insert: {
entity: {
date,
name,
type,
},
collection,
},
},
});
};
};
}
export function updateMealTimeFactory(collection) {
return (id, date) => {
return dispatch => {
dispatch({
type: MEALS_UPDATE,
meteor: {
update: {
id,
modifiers: {
$set: {
date: moment(date).toDate(),
},
},
collection,
},
},
});
};
};
}