-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppContext.js
More file actions
122 lines (114 loc) · 4.01 KB
/
AppContext.js
File metadata and controls
122 lines (114 loc) · 4.01 KB
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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import React, { createContext, useReducer } from 'react';
// 5. The reducer - this is used to update the state, based on the action
export const AppReducer = (state, action) => {
let budget = 0;
switch (action.type) {
case 'ADD_EXPENSE':
let total_budget = 0;
total_budget = state.expenses.reduce(
(previousExp, currentExp) => {
return previousExp + currentExp.cost
},0
);
total_budget = total_budget + action.payload.cost;
action.type = "DONE";
if(total_budget <= state.budget) {
total_budget = 0;
state.expenses.map((currentExp)=> {
if(currentExp.name === action.payload.name) {
currentExp.cost = action.payload.cost + currentExp.cost;
}
return currentExp
});
return {
...state,
};
} else {
alert("Cannot increase the allocation! Out of funds");
return {
...state
}
}
case 'RED_EXPENSE':
const red_expenses = state.expenses.map((currentExp)=> {
if (currentExp.name === action.payload.name && currentExp.cost - action.payload.cost >= 0) {
currentExp.cost = currentExp.cost - action.payload.cost;
budget = state.budget + action.payload.cost
}
return currentExp
})
action.type = "DONE";
return {
...state,
expenses: [...red_expenses],
};
case 'DELETE_EXPENSE':
action.type = "DONE";
state.expenses.map((currentExp)=> {
if (currentExp.name === action.payload) {
budget = state.budget + currentExp.cost
currentExp.cost = 0;
}
return currentExp
})
action.type = "DONE";
return {
...state,
budget
};
case 'SET_BUDGET':
action.type = "DONE";
state.budget = action.payload;
return {
...state,
};
case 'CHG_CURRENCY':
action.type = "DONE";
state.currency = action.payload;
return {
...state
}
default:
return state;
}
};
// 1. Sets the initial state when the app loads
const initialState = {
budget: 2000,
expenses: [
{ id: "Marketing", name: 'Marketing', cost: 50 },
{ id: "Finance", name: 'Finance', cost: 300 },
{ id: "Sales", name: 'Sales', cost: 70 },
{ id: "Human Resource", name: 'Human Resource', cost: 40 },
{ id: "IT", name: 'IT', cost: 500 },
],
currency: '£'
};
// 2. Creates the context this is the thing our components import and use to get the state
export const AppContext = createContext();
// 3. Provider component - wraps the components we want to give access to the state
// Accepts the children, which are the nested(wrapped) components
export const AppProvider = (props) => {
// 4. Sets up the app state. takes a reducer, and an initial state
const [state, dispatch] = useReducer(AppReducer, initialState);
let remaining = 0;
if (state.expenses) {
const totalExpenses = state.expenses.reduce((total, item) => {
return (total = total + item.cost);
}, 0);
remaining = state.budget - totalExpenses;
}
return (
<AppContext.Provider
value={{
expenses: state.expenses,
budget: state.budget,
remaining: remaining,
dispatch,
currency: state.currency
}}
>
{props.children}
</AppContext.Provider>
);
};