Skip to content

Commit

Permalink
Cleans up action creations. Adds badge count support. Updates list st…
Browse files Browse the repository at this point in the history
…ore to use flat ids. Starts work on new list types (single, grouped).
  • Loading branch information
Tyler Wanlass committed Dec 1, 2017
1 parent b70b424 commit 6101c40
Show file tree
Hide file tree
Showing 32 changed files with 5,808 additions and 4,070 deletions.
9,037 changes: 5,227 additions & 3,810 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
"react": "^15.6.1",
"react-dom": "^15.6.1",
"react-redux": "^5.0.6",
"react-scripts": "1.0.10",
"react-scripts": "1.0.17",
"react-transition-group": "^1.2.0",
"redux": "^3.7.2",
"redux-thunk": "^2.2.0"
Expand Down
25 changes: 15 additions & 10 deletions src/actionCreators/addItem.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import * as Api from '../helpers/api.js';
import {setTodayBadgeCount} from './setTodayBadgeCount.js';
import {
ADD_ITEM,
ADD_ITEM_INDEX,
EDIT_ITEM_INDEX,
REMOVE_ITEM
} from '../actionTypes/actionTypes.js';
import {incrementTodayBadgeCount} from './incrementTodayBadgeCount.js';

const add = (item) => {
return {
Expand All @@ -14,6 +14,13 @@ const add = (item) => {
}
}

const remove = (id) => {
return {
type: REMOVE_ITEM,
id
}
}

const addIndex = (id, listId) => {
return {
type: ADD_ITEM_INDEX,
Expand All @@ -31,13 +38,6 @@ const editIndex = (id, tempId, listId) => {
}
}

const remove = (id) => {
return {
type: REMOVE_ITEM,
id
}
}

export const addItem = (title, createdAt, dueAt, isEvent, listId) => {
// Generate temp id to be used until API returns new item id
let tempId = 'cid_' + Math.floor(performance.now())
Expand All @@ -51,11 +51,16 @@ export const addItem = (title, createdAt, dueAt, isEvent, listId) => {
'list_id': listId
})

return (dispatch) => {
return (dispatch, getState) => {
// Immediately add item to client collection and the sort index
dispatch(add({id: tempId, title, created_at: createdAt, due_at: dueAt, is_event: isEvent, list_id: listId, completed: false}));
dispatch(addIndex(tempId, listId));
dispatch(setTodayBadgeCount());

// If it's a Today item, update the badge count as well
const {lists} = getState();
if (lists[listId].title === 'today') {
dispatch(incrementTodayBadgeCount())
}

// Await API call response
item.then(response => {
Expand Down
7 changes: 7 additions & 0 deletions src/actionCreators/decrementTodayBadgeCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { DECREMENT_TODAY_BADGE_COUNT } from '../actionTypes/actionTypes.js';

export const decrementTodayBadgeCount = () => {
return {
type: DECREMENT_TODAY_BADGE_COUNT
}
}
10 changes: 8 additions & 2 deletions src/actionCreators/getListItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,14 @@ export const getListItems = listId => {
return (dispatch) => {
items.then(response => {
response.json().then(response => {
dispatch(receiveListItems(listId, response.list.items, response.meta.sort_order))
dispatch(setTodayBadgeCount())
let items = response.list.items;
let meta = response.meta;

dispatch(receiveListItems(listId, items, meta.sort_order))

if (response.list.title === 'today') {
dispatch(setTodayBadgeCount(meta.badge_count))
}
})
})
};
Expand Down
7 changes: 2 additions & 5 deletions src/actionCreators/getLists.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import _ from 'lodash';
import * as Api from '../helpers/api.js';
import {receiveLists} from './receiveLists.js';
import {setTodayBadgeCount} from './setTodayBadgeCount.js';

export const getLists = () => {
let lists = Api.getLists()
Expand All @@ -10,10 +8,9 @@ export const getLists = () => {
lists.then(response => {
response.json().then(response => {
let lists = response.lists;
let todayList = _.filter(lists, { 'type': 'today'})[0];
let meta = response.meta;

dispatch(receiveLists(lists))
dispatch(setTodayBadgeCount(todayList.badge_count))
dispatch(receiveLists(lists, meta.sort_order))
})
})
};
Expand Down
7 changes: 7 additions & 0 deletions src/actionCreators/incrementTodayBadgeCount.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { INCREMENT_TODAY_BADGE_COUNT } from '../actionTypes/actionTypes.js';

export const incrementTodayBadgeCount = () => {
return {
type: INCREMENT_TODAY_BADGE_COUNT
}
}
5 changes: 3 additions & 2 deletions src/actionCreators/receiveLists.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { RECEIVE_LISTS } from '../actionTypes/actionTypes.js';

export const receiveLists = lists => {
export const receiveLists = (lists, sortOrder) => {
return {
type: RECEIVE_LISTS,
lists: lists
lists,
sortOrder
}
}
11 changes: 8 additions & 3 deletions src/actionCreators/removeItem.js
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import * as Api from '../helpers/api.js';
import {setTodayBadgeCount} from './setTodayBadgeCount.js';
import {
REMOVE_ITEM,
REMOVE_ITEM_INDEX
} from '../actionTypes/actionTypes.js';
import {decrementTodayBadgeCount} from './decrementTodayBadgeCount.js';

const remove = (id) => {
return {
Expand All @@ -23,11 +23,16 @@ const removeIndex = (id, listId) => {
export const removeItem = (id, listId) => {
let item = Api.deleteListItem(id)

return (dispatch) => {
return (dispatch, getState) => {
// Immediately remove item from client collection and sort index
dispatch(remove(id))
dispatch(removeIndex(id, listId))
dispatch(setTodayBadgeCount());

// If it's a Today item, update the badge count as well
const {lists} = getState();
if (lists[listId].title === 'today') {
dispatch(decrementTodayBadgeCount());
}

// Dispatch API call to delete item and await response
item.then(response => {
Expand Down
14 changes: 1 addition & 13 deletions src/actionCreators/setTodayBadgeCount.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,8 @@
import _ from 'lodash';
import { SET_TODAY_BADGE_COUNT } from '../actionTypes/actionTypes.js';

const set = count => {
export const setTodayBadgeCount = count => {
return {
type: SET_TODAY_BADGE_COUNT,
count
}
}

export const setTodayBadgeCount = (count=null) => {
return (dispatch, getState) => {
let state = getState()
let todayListId = _.filter(state.lists, { 'type': 'today'})[0].id;
let computedTodayBadgeCount = _.filter(state.items, { 'completed': false, 'list_id': todayListId }).length

let badgeCount = count ? count : computedTodayBadgeCount;
dispatch(set(badgeCount))
}
}
16 changes: 11 additions & 5 deletions src/actionCreators/toggleItem.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import Moment from 'moment';
import * as Api from '../helpers/api.js';
import {setTodayBadgeCount} from './setTodayBadgeCount.js';
import { TOGGLE_ITEM } from '../actionTypes/actionTypes.js';
import Moment from 'moment';
import {incrementTodayBadgeCount} from './incrementTodayBadgeCount.js';
import {decrementTodayBadgeCount} from './decrementTodayBadgeCount.js';

const toggle = (id, completed) => {
return {
Expand All @@ -12,14 +13,19 @@ const toggle = (id, completed) => {
}
}

export const toggleItem = (id, completed) => {
export const toggleItem = (id, listId, completed) => {
// Create API fetch / promise
let item = Api.updateListItem({id, completed})

return (dispatch) => {
return (dispatch, getState) => {
// Immediately toggle item in client collection
dispatch(toggle(id, completed));
dispatch(setTodayBadgeCount());

// If it's a Today item, update the badge count as well
const {lists} = getState();
if (lists[listId].title === 'today') {
completed ? dispatch(decrementTodayBadgeCount()) : dispatch(incrementTodayBadgeCount())
}

// Await API call response
item.then(response => {
Expand Down
2 changes: 2 additions & 0 deletions src/actionTypes/actionTypes.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ export const SET_NOW_DRAGGING_TO = 'SET_NOW_DRAGGING_TO'
export const SET_NOW_LOADING = 'SET_NOW_LOADING'
export const SET_ACITVE_LIST = 'SET_ACITVE_LIST'
export const SET_TODAY_BADGE_COUNT = 'SET_TODAY_BADGE_COUNT'
export const INCREMENT_TODAY_BADGE_COUNT = 'INCREMENT_TODAY_BADGE_COUNT'
export const DECREMENT_TODAY_BADGE_COUNT = 'DECREMENT_TODAY_BADGE_COUNT'

// Sort index
export const ADD_ITEM_INDEX = 'ADD_ITEM_INDEX'
Expand Down
Loading

0 comments on commit 6101c40

Please sign in to comment.