Skip to content

Commit 37339a3

Browse files
author
Mahidhar Chaluvadi
committed
Updated to include actions, reducers
1 parent 86c69cf commit 37339a3

12 files changed

Lines changed: 153 additions & 4 deletions

File tree

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@
1010
"react-router": "^4.3.1",
1111
"react-router-dom": "^4.3.1",
1212
"react-scripts": "1.1.5",
13-
"redux": "^4.0.0"
13+
"redux": "^4.0.0",
14+
"uuid": "^3.3.2"
1415
},
1516
"scripts": {
1617
"build-css": "node-sass-chokidar --include-path ./src --include-path ./node_modules src/ -o src/",

src/actions/Expensify/Expenses.js

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import uuid from "uuid";
2+
import actions from "./";
3+
4+
export const addExpense = (description ="Default Desc", amt = 10) =>
5+
({type:actions.ADD_EXPENSE,
6+
payload: {
7+
id:uuid(),
8+
description,
9+
amt
10+
}});
11+
12+
export const removeExpense = ({id = 0} = {}) =>
13+
({type:actions.REMOVE_EXPENSE,
14+
payload: {
15+
id
16+
}});
17+
18+
export const editExpense = (id = 0, description="Unknown",amt = 0) =>
19+
({type:actions.EDIT_EXPENSE,
20+
payload: {
21+
id,
22+
description,
23+
amt
24+
}});

src/actions/Expensify/Filters.js

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import actions from "./";
2+
3+
export const setTextFilter = (textEntered = "") => ({type:actions.SET_TEXT_FILTER, payload:{filterText:textEntered}});
4+
export const sortByDesc = () => ({type:actions.SORT_BY_DESC});
5+
6+
export const setStartDate = (startDate = undefined) => ({type:actions.SET_START_DATE, payload:{startDate:startDate}});
7+
export const setEndDate = (endDate = undefined) => ({type:actions.SET_END_DATE, payload:{endDate:endDate}});
8+
export const sortByDate = () => ({type:actions.SORT_BY_DATE});
9+
10+
export const sortByAmt = () => ({type:actions.SORT_BY_AMT});

src/actions/Expensify/index.js

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
export default {
2+
ADD_EXPENSE:"ADD_EXPENSE",
3+
EDIT_EXPENSE:"EDIT_EXPENSE",
4+
REMOVE_EXPENSE:"REMOVE_EXPENSE",
5+
SET_TEXT_FILTER:"SET_TEXT_FILTER",
6+
SET_START_DATE:"SET_START_DATE",
7+
SET_END_DATE:"SET_END_DATE",
8+
SORT_BY_DATE:"SORT_BY_DATE",
9+
SORT_BY_AMT:"SORT_BY_AMT",
10+
SORT_BY_DESC:"SORT_BY_DESC",
11+
};

src/components/Expensify/Layout/Header/index.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,11 @@ export default () =>
1010
<li><NavLink to='/create' activeClassName="is-active" exact>Add Expense</NavLink></li>
1111
<li><NavLink to='/edit' activeClassName="is-active">Edit Expense</NavLink></li>
1212
<li><NavLink to='/help' activeClassName="is-active">Help</NavLink></li>
13+
{
14+
/*
1315
<li><NavLink to='/playground' activeClassName="is-active">Playground</NavLink></li>
16+
*/
17+
}
1418
<li><Link to='/create/1'>Non Existing Page</Link></li>
1519
</ul>
1620
</div>;
File renamed without changes.
Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const setCount =({count=1} ={}) =>({
2323
count
2424
});
2525

26-
const store = createStore((state ={count:0},action) => {
26+
const countReducer = (state ={count:0},action) => {
2727
// console.log("store created/updated");
2828
switch(action.type)
2929
{
@@ -33,7 +33,9 @@ const store = createStore((state ={count:0},action) => {
3333
case "SET":return {count:action.count};
3434
default: return state;
3535
}
36-
});
36+
};
37+
38+
const store = createStore(countReducer);
3739

3840
const unsubscribe = store.subscribe(()=>{
3941
console.log("Subscribed. Cur state",store.getState());

src/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,6 @@ import ReactDOM from "react-dom";
22
import registerServiceWorker from "./registerServiceWorker";
33
import AppRouter from "./routers/ExpensifyRouter";
44
// import AppRouter from "./routers/PortfolioRouter.js";
5+
56
ReactDOM.render(AppRouter, document.getElementById("root"));
67
registerServiceWorker();

src/reducers/Expensify/Expenses.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import actions from "./../../actions/Expensify";
2+
3+
const defaultExpenseList = [];
4+
5+
export default (state = defaultExpenseList,action) =>
6+
{
7+
switch(action.type)
8+
{
9+
case actions.ADD_EXPENSE:
10+
// return state.concat([{id:uuid()}]);
11+
return [...state,action.payload];
12+
case actions.REMOVE_EXPENSE:
13+
// return state.concat([{id:uuid()}]);
14+
return state.filter((expense)=>action.payload.id!==expense.id);
15+
case actions.EDIT_EXPENSE:
16+
return state.map((expense)=>action.payload.id!==expense.id?({...expense}):({...expense,description:action.payload.description,amt:action.payload.amt}));
17+
default:
18+
return state;
19+
}
20+
};

src/reducers/Expensify/Filters.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import actions from "./../../actions/Expensify";
2+
3+
const defaultFilters = {
4+
sortBy:"amt",
5+
filterText:"",
6+
startDate:undefined,
7+
endDate:undefined
8+
};
9+
10+
export default (state = defaultFilters,action) =>
11+
{
12+
switch(action.type)
13+
{
14+
case actions.SORT_BY_AMT:
15+
return {...state,sortBy:"amt"};
16+
case actions.SORT_BY_DATE:
17+
return {...state,sortBy:"date"};
18+
case actions.SORT_BY_DESC:
19+
return {...state,sortBy:"desc"};
20+
case actions.SET_START_DATE:
21+
return {...state,startDate:action.payload.startDate};
22+
case actions.SET_END_DATE:
23+
return {...state,endDate:action.payload.endDate};
24+
case actions.SET_TEXT_FILTER:
25+
return {...state,filterText:action.payload.filterText};
26+
default:
27+
return state;
28+
}
29+
};

0 commit comments

Comments
 (0)