Skip to content

Commit

Permalink
Use camelcase for mutations
Browse files Browse the repository at this point in the history
  • Loading branch information
zyml committed May 30, 2017
1 parent 840946b commit ed8e4da
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 29 deletions.
15 changes: 8 additions & 7 deletions src/store/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
postConfession,
} from '../api/confessions';
import { fetchTags } from '../api/tags';
import capitalize from '../utils/capitalize';
import State from './state';

type FetchFunction = (count?: number, offset?: number) => Promise<any>;
Expand All @@ -25,8 +26,8 @@ const getNext = (fetch: FetchFunction, type: string, skipIfNotEmpty?: boolean) =
}

return fetch(ctx.state.itemsPerPage, ctx.state[type].length).then((payload) => {
ctx.commit('UPDATE_ENTITIES', payload.entities);
ctx.commit(`UPDATE_${type.toUpperCase()}`, payload.result);
ctx.commit('updateEntities', payload.entities);
ctx.commit(`update${capitalize(type)}`, payload.result);
});
};
};
Expand All @@ -44,7 +45,7 @@ const getCategories = (ctx: ActionContext<State, any>) => {
}

return fetchCategories().then((payload) => {
ctx.commit('UPDATE_ENTITIES', payload.entities);
ctx.commit('updateEntities', payload.entities);
});
};

Expand All @@ -54,7 +55,7 @@ const getTags = (ctx: ActionContext<State, any>) => {
}

return fetchTags(9).then((payload) => {
ctx.commit('UPDATE_ENTITIES', payload.entities);
ctx.commit('updateEntities', payload.entities);
});
};

Expand All @@ -68,8 +69,8 @@ const getByNext = (fetch: FetchByFunction, type: string, skipIfNotEmpty?: boolea
}

return fetch(id, ctx.state.itemsPerPage, count).then((payload) => {
ctx.commit('UPDATE_ENTITIES', payload.entities);
ctx.commit(`UPDATE_${snakeCase(type).toUpperCase()}`, { id, list: payload.result });
ctx.commit('updateEntities', payload.entities);
ctx.commit(`update${capitalize(type)}`, { id, list: payload.result });
});
};
};
Expand All @@ -89,7 +90,7 @@ const getById = (ctx: ActionContext<State, any>, id: string) => {
}

return fetchById(id).then((payload) => {
ctx.commit('UPDATE_ENTITIES', payload.entities);
ctx.commit('updateEntities', payload.entities);
});
};

Expand Down
36 changes: 14 additions & 22 deletions src/store/mutations.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,33 +16,25 @@ interface ParamsPayload {
list: string[];
}

function UPDATE_ENTITIES(state: State, entities: any) {
Object
.keys(entityMapping)
.map((key) => {
state[key] = { ...state[key], ...entities[entityMapping[key]] };
});
}

const concat = (type: string) => (state: State, list: string[]) => state[type] = state[type].concat(list);

const UPDATE_FEATURED = concat('featured');
const UPDATE_LATEST = concat('latest');
const UPDATE_POPULAR = concat('popular');

const updateListsByParam = (type: string) => (state: State, payload: ParamsPayload) =>
state[type][payload.id] = (state[type][payload.id] || []).concat(payload.list);

const UPDATE_LISTS_BY_CATEGORY = updateListsByParam('listsByCategory');
const UPDATE_LISTS_BY_TAG = updateListsByParam('listsByTag');
const UPDATE_LISTS_BY_KEYWORD = updateListsByParam('listsByKeyword');
const updateEntities = (state: State, entities: any) => {
Object
.keys(entityMapping)
.map((key) => {
state[key] = { ...state[key], ...entities[entityMapping[key]] };
});
};

export default {
UPDATE_ENTITIES,
UPDATE_FEATURED,
UPDATE_LATEST,
UPDATE_LISTS_BY_CATEGORY,
UPDATE_LISTS_BY_KEYWORD,
UPDATE_LISTS_BY_TAG,
UPDATE_POPULAR,
updateEntities,
updateFeatured: concat('featured'),
updateLatest: concat('latest'),
updateListsByCategory: updateListsByParam('listsByCategory'),
updateListsByKeyword: updateListsByParam('listsByKeyword'),
updateListsByTag: updateListsByParam('listsByTag'),
updatePopular: concat('popular'),
};

0 comments on commit ed8e4da

Please sign in to comment.