Skip to content
This repository has been archived by the owner on Jan 22, 2018. It is now read-only.

[RFR] Use the actionTypeBuilder pattern #7

Merged
merged 2 commits into from
Dec 4, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions app/client/actions/actionTypeBuilder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// This function will be extracted to an npm package later
export function actionTypeBuilder(prefix) {
return {
type: actionType => `${prefix}/${actionType}`,
ready: actionType => `${actionType}/ready`,
changed: actionType => `${actionType}/changed`,
error: actionType => `${actionType}/error`,
};
}

export default actionTypeBuilder('@my-nutrition');
4 changes: 3 additions & 1 deletion app/client/actions/app.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
export const SET_TITLE = 'SET_TITLE';
import actionTypeBuilder from './actionTypeBuilder';

export const SET_TITLE = actionTypeBuilder.type('SET_TITLE');

export function setTitle(title) {
return {
Expand Down
14 changes: 9 additions & 5 deletions app/client/actions/auth.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
/* global Accounts, Meteor, Roles */
import actionTypeBuilder from './actionTypeBuilder';
import { newNotification } from './notifications';
import { updatePath } from 'redux-simple-router';

export const USER_LOGGING_IN = 'USER_LOGGING_IN';
export const USER_DATA = 'USER_DATA';
export const USER_LOGGING_IN = actionTypeBuilder.type('USER_LOGGING_IN');
export const USER_DATA = actionTypeBuilder.type('USER_DATA');
export const SET_ACCOUNT_AS_COACH = actionTypeBuilder.type('SET_ACCOUNT_AS_COACH');
export const SET_ACCOUNT_AS_COACHEE = actionTypeBuilder.type('SET_ACCOUNT_AS_COACHEE');
export const INITIALIZE_ACCOUNT_FROM_INVITE = actionTypeBuilder.type('INITIALIZE_ACCOUNT_FROM_INVITE');

export function loadUser() {
return dispatch => {
Expand Down Expand Up @@ -39,7 +43,7 @@ export function logout() {
export function setAccountAsCoach() {
return dispatch => {
dispatch({
type: 'SET_ACCOUNT_AS_COACH',
type: SET_ACCOUNT_AS_COACH,
meteor: {
call: {
method: 'setAccountAsCoach',
Expand All @@ -53,7 +57,7 @@ export function setAccountAsCoach() {
export function setAccountAsCoachee() {
return dispatch => {
dispatch({
type: 'SET_ACCOUNT_AS_COACHEE',
type: SET_ACCOUNT_AS_COACHEE,
meteor: {
call: {
method: 'setAccountAsCoachee',
Expand All @@ -67,7 +71,7 @@ export function setAccountAsCoachee() {
export function initializeAccountFromInvite(token) {
return dispatch => {
dispatch({
type: 'INITIALIZE_ACCOUNT_FROM_INVITE',
type: INITIALIZE_ACCOUNT_FROM_INVITE,
meteor: {
call: {
method: 'initializeAccountFromInvite',
Expand Down
8 changes: 6 additions & 2 deletions app/client/actions/coachees.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
/* global Meteor */
import actionTypeBuilder from './actionTypeBuilder';

export const COACHEE = actionTypeBuilder.type('COACHEE');
export const COACHEES = actionTypeBuilder.type('COACHEES');

export function loadCoacheeFactory(coacheeCollection) {
return (userId) => {
return dispatch => {
dispatch({
type: 'COACHEE',
type: COACHEE,
meteor: {
get: () => coacheeCollection.findOne(userId),
},
Expand All @@ -17,7 +21,7 @@ export function loadCoacheesFactory(coacheeCollection) {
return () => {
return dispatch => {
dispatch({
type: 'COACHEES',
type: COACHEES,
meteor: {
subscribe: () => Meteor.subscribe('coachees'),
get: () => coacheeCollection.find({
Expand Down
17 changes: 12 additions & 5 deletions app/client/actions/dishes.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
/* global Meteor */
import { assign, omit } from 'lodash';
import actionTypeBuilder from './actionTypeBuilder';

export const DISHES_COPY = actionTypeBuilder.type('DISHES_COPY');
export const DISHES_MOVE = actionTypeBuilder.type('DISHES_MOVE');
export const DISHES_INSERT = actionTypeBuilder.type('DISHES_INSERT');
export const DISHES_REMOVE = actionTypeBuilder.type('DISHES_REMOVE');
export const DISHES_UPDATE = actionTypeBuilder.type('DISHES_UPDATE');

export function copyDishToMeal(dishId, mealId, position) {
return dispatch => {
dispatch({
type: 'DISHES_COPY',
type: DISHES_COPY,
meteor: {
call: {
method: 'copyDishToMeal',
Expand All @@ -22,7 +29,7 @@ export function copyDishToMeal(dishId, mealId, position) {
export function moveDishToMeal(dishId, mealId, position) {
return dispatch => {
dispatch({
type: 'DISHES_MOVE',
type: DISHES_MOVE,
meteor: {
call: {
method: 'moveDishToMeal',
Expand All @@ -41,7 +48,7 @@ export function newMealDishFactory(collection) {
return (mealId, dish) => {
return dispatch => {
dispatch({
type: 'DISHES_INSERT',
type: DISHES_INSERT,
meteor: {
insert: {
entity: assign({}, dish, { mealId }),
Expand All @@ -57,7 +64,7 @@ export function deleteMealDishFactory(collection) {
return id => {
return dispatch => {
dispatch({
type: 'DISHES_REMOVE',
type: DISHES_REMOVE,
meteor: {
remove: {
id,
Expand All @@ -73,7 +80,7 @@ export function updateMealDishFactory(collection) {
return (dish) => {
return dispatch => {
dispatch({
type: 'DISHES_UPDATE',
type: DISHES_UPDATE,
meteor: {
update: {
id: dish._id,
Expand Down
8 changes: 6 additions & 2 deletions app/client/actions/invites.js
Original file line number Diff line number Diff line change
@@ -1,10 +1,14 @@
import { newSuccessNotification } from './notifications';
import { updatePath } from 'redux-simple-router';
import actionTypeBuilder from './actionTypeBuilder';

export const INVITE_COACH = actionTypeBuilder.type('INVITE_COACH');
export const INVITE_COACHEE = actionTypeBuilder.type('INVITE_COACHEE');

export function inviteCoach(email) {
return dispatch => {
dispatch({
type: 'INVITE_COACH',
type: INVITE_COACH,
meteor: {
call: {
method: 'inviteCoach',
Expand All @@ -21,7 +25,7 @@ export function inviteCoach(email) {
export function inviteCoachee(email) {
return dispatch => {
dispatch({
type: 'INVITE_COACHEE',
type: INVITE_COACHEE,
meteor: {
call: {
method: 'inviteCoachee',
Expand Down
14 changes: 10 additions & 4 deletions app/client/actions/mealTemplates.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
/* global Meteor */
import moment from 'moment';
import actionTypeBuilder from './actionTypeBuilder';

export const MEAL_TEMPLATES = actionTypeBuilder.type('MEAL_TEMPLATES');
export const MEAL_TEMPLATES_REMOVE = actionTypeBuilder.type('MEAL_TEMPLATES_REMOVE');
export const MEAL_TEMPLATES_NEW_FROM_MEAL = actionTypeBuilder.type('MEAL_TEMPLATES_NEW_FROM_MEAL');
export const MEALS_NEW_FROM_MEAL_TEMPLATE = actionTypeBuilder.type('MEALS_NEW_FROM_MEAL_TEMPLATE');

export function loadMealTemplatesFactory(collection) {
return () => {
return dispatch => {
dispatch({
type: 'MEAL_TEMPLATES',
type: MEAL_TEMPLATES,
meteor: {
subscribe: () => Meteor.subscribe('mealTemplates'),
get: () => collection.find({}, { sort: { time: 1 }}).fetch().sort((x, y) => {
Expand All @@ -28,7 +34,7 @@ export function deleteMealTemplateFactory(collection) {
return id => {
return dispatch => {
dispatch({
type: 'MEAL_TEMPLATES_REMOVE',
type: MEAL_TEMPLATES_REMOVE,
meteor: {
remove: {
id,
Expand All @@ -43,7 +49,7 @@ export function deleteMealTemplateFactory(collection) {
export function newMealTemplate(mealId, name) {
return dispatch => {
dispatch({
type: 'MEAL_TEMPLATES_NEW_FROM_MEAL',
type: MEAL_TEMPLATES_NEW_FROM_MEAL,
meteor: {
call: {
method: 'createMealTemplate',
Expand All @@ -60,7 +66,7 @@ export function newMealTemplate(mealId, name) {
export function newMealFromTemplate(mealTemplateId, date) {
return dispatch => {
dispatch({
type: 'MEALS_NEW_FROM_MEAL_TEMPLATE',
type: MEALS_NEW_FROM_MEAL_TEMPLATE,
meteor: {
call: {
method: 'createMealFromTemplate',
Expand Down
14 changes: 10 additions & 4 deletions app/client/actions/meals.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
/* 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) => {
Expand All @@ -10,7 +16,7 @@ export function loadMealsFactory(mealCollection) {

return dispatch => {
dispatch({
type: 'MEALS',
type: MEALS,
meteor: {
subscribe: () => Meteor.subscribe('meals', dateStart, dateEnd, finalUserId, {
onStop: error => {
Expand All @@ -30,7 +36,7 @@ export function deleteMealFactory(collection) {
return id => {
return dispatch => {
dispatch({
type: 'MEALS_REMOVE',
type: MEALS_REMOVE,
meteor: {
remove: {
id,
Expand All @@ -46,7 +52,7 @@ export function newMealFactory(collection) {
return (type, name, date) => {
return dispatch => {
dispatch({
type: 'MEALS_INSERT',
type: MEALS_INSERT,
meteor: {
insert: {
entity: {
Expand All @@ -66,7 +72,7 @@ export function updateMealTimeFactory(collection) {
return (id, date) => {
return dispatch => {
dispatch({
type: 'MEALS_UPDATE',
type: MEALS_UPDATE,
meteor: {
update: {
id,
Expand Down
6 changes: 4 additions & 2 deletions app/client/actions/notifications.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
export const NEW_NOTIFICATION = 'NEW_NOTIFICATION';
export const CLEAR_NOTIFICATION = 'CLEAR_NOTIFICATION';
import actionTypeBuilder from './actionTypeBuilder';

export const NEW_NOTIFICATION = actionTypeBuilder.type('NEW_NOTIFICATION');
export const CLEAR_NOTIFICATION = actionTypeBuilder.type('CLEAR_NOTIFICATION');

export function newNotification(level, message) {
return {
Expand Down
4 changes: 3 additions & 1 deletion app/client/actions/planning.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
/* global Meteor */
import { updatePath } from 'redux-simple-router';
import moment from 'moment';
export const DATE_CHANGED = 'DATE_CHANGED';
import actionTypeBuilder from './actionTypeBuilder';

export const DATE_CHANGED = actionTypeBuilder.type('DATE_CHANGED');

export function setPlanningDate(dateSelected, userId, userName) {
return dispatch => {
Expand Down
6 changes: 5 additions & 1 deletion app/client/actions/profile.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
import actionTypeBuilder from './actionTypeBuilder';

export const USER_PREFERENCE_UPDATE = actionTypeBuilder.type('USER_PREFERENCE_UPDATE');

export function setUserPreferenceFactory(userCollection) {
return (userId, preference, value) => {
return dispatch => {
Expand All @@ -8,7 +12,7 @@ export function setUserPreferenceFactory(userCollection) {
modifiers.$set[`profile.preferences.${preference}`] = value;

dispatch({
type: 'USER_PREFERENCE_UPDATE',
type: USER_PREFERENCE_UPDATE,
meteor: {
update: {
id: userId,
Expand Down
4 changes: 3 additions & 1 deletion app/client/middlewares/meteorDatasource.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* global Meteor, Tracker */
import actionTypeBuilder from '../actions/actionTypeBuilder';

const changeComputations = [];

/* This middleware is used for Meteor reactive source without subscriptions. It'll handle actions
Expand Down Expand Up @@ -36,7 +38,7 @@ export default store => next => action => {
}

store.dispatch({
type: `${action.type}_CHANGED`,
type: actionTypeBuilder.changed(action.type),
data,
});
});
Expand Down
6 changes: 4 additions & 2 deletions app/client/middlewares/meteorSubscription.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
/* global Meteor, Tracker */
import actionTypeBuilder from '../actions/actionTypeBuilder';

const handles = [];
const computations = [];

Expand Down Expand Up @@ -53,7 +55,7 @@ export default store => next => action => {
const ready = handle.ready();

store.dispatch({
type: `${action.type}_READY`,
type: actionTypeBuilder.ready(action.type),
ready,
});

Expand All @@ -63,7 +65,7 @@ export default store => next => action => {
}

store.dispatch({
type: `${action.type}_CHANGED`,
type: actionTypeBuilder.changed(action.type),
data,
});
}
Expand Down
6 changes: 4 additions & 2 deletions app/client/reducers/auth.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { assign } from 'lodash';
import actionTypeBuilder from '../actions/actionTypeBuilder';
import { USER_LOGGING_IN, USER_DATA } from '../actions/auth';

export const initialState = {
user: null,
Expand All @@ -9,10 +11,10 @@ export default function(state = initialState, action) {
const { data, type } = action;

switch (type) {
case 'USER_DATA_CHANGED':
case actionTypeBuilder.changed(USER_DATA):
return assign({}, state, { user: data });

case 'USER_LOGGING_IN_CHANGED':
case actionTypeBuilder.changed(USER_LOGGING_IN):
return assign({}, state, { loggingIn: data });

default:
Expand Down
10 changes: 6 additions & 4 deletions app/client/reducers/coachees.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { assign } from 'lodash';
import actionTypeBuilder from '../actions/actionTypeBuilder';
import { COACHEE, COACHEES } from '../actions/coachees';

export const initialState = {
current: null,
Expand All @@ -10,16 +12,16 @@ export default function(state = initialState, action) {
const { data, ready, type } = action;

switch (type) {
case 'COACHEES_READY':
case actionTypeBuilder.ready(COACHEES):
return assign({}, state, { ready });

case 'COACHEES_CHANGED':
case actionTypeBuilder.changed(COACHEES):
return assign({}, state, { items: data });

case 'COACHEE_READY':
case actionTypeBuilder.ready(COACHEE):
return assign({}, state, { ready });

case 'COACHEE_CHANGED':
case actionTypeBuilder.changed(COACHEE):
return assign({}, state, { current: data });

default:
Expand Down
Loading