diff --git a/.gitignore b/.gitignore index b6e47617de..dc00b0bc92 100644 --- a/.gitignore +++ b/.gitignore @@ -127,3 +127,4 @@ dmypy.json # Pyre type checker .pyre/ +node_modules diff --git a/src/App.js b/src/App.js index 610e47d694..50e609189e 100644 --- a/src/App.js +++ b/src/App.js @@ -1,47 +1,48 @@ import React from 'react'; - import 'bootstrap/dist/css/bootstrap.min.css'; -//Code to import Budget.js +import { AppProvider } from './context/AppContext'; import Budget from './components/Budget'; +import ExpenseTotal from './components/ExpenseTotal'; +import ExpenseList from './components/ExpenseList'; +import AllocationForm from './components/AllocationForm'; +import RemainingBudget from './components/Remaining'; +import Currency from './components/Currency'; -// Add code to import the other components here under - - -import { AppProvider } from './context/AppContext'; const App = () => { return (

Company's Budget Allocation

-
-     { - /* Add Budget component here */ - } - -     { - /* Add Remaining component here*/ - } - -     { - /* Add ExpenseTotal component here */ - } - -     { - /* Add ExpenseList component here */ - } - -     { - /* Add ExpenseItem component here */ - } - -     { - /* Add AllocationForm component here under */ - } - +
+
+ +
+
+ +
+
+ +
+
+ +
+
+

Allocation

+
+
+ +
+
+

Change allocation

+
+
+ +
); }; -export default App; + +export default App; \ No newline at end of file diff --git a/src/components/AllocationForm.js b/src/components/AllocationForm.js index 8b13789179..382e7bdbd6 100644 --- a/src/components/AllocationForm.js +++ b/src/components/AllocationForm.js @@ -1 +1,84 @@ +import React, { useContext, useState } from 'react'; +import { AppContext } from '../context/AppContext'; +const AllocationForm = (props) => { + const { dispatch,remaining, currency} = useContext(AppContext); + + const [name, setName] = useState(''); + const [cost, setCost] = useState(''); + const [action, setAction] = useState(''); + + const submitEvent = () => { + + if(cost > remaining) { + alert("The value cannot exceed remaining funds £"+remaining); + setCost(""); + return; + } + + const expense = { + name: name, + cost: parseInt(cost), + }; + if(action === "Reduce") { + dispatch({ + type: 'RED_EXPENSE', + payload: expense, + }); + } else { + dispatch({ + type: 'ADD_EXPENSE', + payload: expense, + }); + } + }; + + return ( +
+
+ +
+
+ +
+ + +
+ +
+ + + + setCost(event.target.value)}> + + + +
+
+ +
+ ); +}; + +export default AllocationForm; diff --git a/src/components/Budget.js b/src/components/Budget.js index 8b13789179..f745b312b5 100644 --- a/src/components/Budget.js +++ b/src/components/Budget.js @@ -1 +1,30 @@ +import React, { useContext, useState } from 'react'; +import { AppContext } from '../context/AppContext'; +const Budget = () => { + const { budget, currency} = useContext(AppContext); + const [newBudget, setNewBudget] = useState(budget); + // const [newCurrency, setNewCurrency] = useState(currency); + const handleBudgetChange = (event) => { + setNewBudget(event.target.value); + } + + // const handleCurrencyChange = (event) => { + // setNewCurrency(event.target.value); + // } + return ( +
+Budget: {currency} + +{/* */} + + +
+ ); +}; +export default Budget; diff --git a/src/components/ExpenseItem.js b/src/components/ExpenseItem.js index 8b13789179..36385c6063 100644 --- a/src/components/ExpenseItem.js +++ b/src/components/ExpenseItem.js @@ -1 +1,62 @@ +import React, { useContext } from 'react'; +import { TiDelete } from 'react-icons/ti'; +import { AppContext } from '../context/AppContext'; +import Currency from './Currency'; +const ExpenseItem = (props) => { + const {currency, dispatch } = useContext(AppContext); + + const handleDeleteExpense = () => { + dispatch({ + type: 'DELETE_EXPENSE', + payload: props.id, + }); + }; + + const increaseAllocation = (name) => { + const expense = { + name: name, + cost: 10, + }; + + dispatch({ + type: 'ADD_EXPENSE', + payload: expense + }); + + } + const decreaseAllocation = (name) => { + const expense = { + name: name, + cost: -10, + }; + + dispatch({ + type: 'ADD_EXPENSE', + payload: expense + }); + + } + const buttonStylePlus = { + borderRadius: "100px", + backgroundColor: 'green', + color: "white" + }; + + const buttonStyleMinus = { + borderRadius: "100px", + backgroundColor: 'red', + color: "white" + }; + return ( + + {props.name} + {currency}{props.cost} + + + + + ); +}; + +export default ExpenseItem; diff --git a/src/components/ExpenseList.js b/src/components/ExpenseList.js index 8b13789179..23508bb24a 100644 --- a/src/components/ExpenseList.js +++ b/src/components/ExpenseList.js @@ -1 +1,25 @@ - +import React, { useContext } from 'react'; +import ExpenseItem from './ExpenseItem'; +import { AppContext } from '../context/AppContext'; +const ExpenseList = () => { + const { expenses } = useContext(AppContext); + return ( + + + + + + + + + + + + {expenses.map((expense) => ( + + ))} + +
DepartmentAllocated BudgetIncrease by 10Decrease by 10Delete
+ ); +}; +export default ExpenseList; diff --git a/src/components/ExpenseTotal.js b/src/components/ExpenseTotal.js index 8b13789179..150a7f336c 100644 --- a/src/components/ExpenseTotal.js +++ b/src/components/ExpenseTotal.js @@ -1 +1,14 @@ - +import React, { useContext } from 'react'; +import { AppContext } from '../context/AppContext'; +const ExpenseTotal = () => { + const { expenses, currency } = useContext(AppContext); + const totalExpenses = expenses.reduce((total, item) => { + return (total += item.cost); + }, 0); + return ( +
+ Spent so far: {currency}{totalExpenses} +
+ ); +}; +export default ExpenseTotal; diff --git a/src/components/Remaining.js b/src/components/Remaining.js index 8b13789179..8d9881a29b 100644 --- a/src/components/Remaining.js +++ b/src/components/Remaining.js @@ -1 +1,15 @@ - +import React, { useContext } from 'react'; +import { AppContext } from '../context/AppContext'; +const Remaining = () => { + const { expenses, budget, currency } = useContext(AppContext); + const totalExpenses = expenses.reduce((total, item) => { + return (total = total + item.cost); + }, 0); + const alertType = totalExpenses > budget ? 'alert-danger' : 'alert-success'; + return ( +
+ Remaining: {currency}{budget - totalExpenses} +
+ ); +}; +export default Remaining; diff --git a/src/context/AppContext.js b/src/context/AppContext.js index 88214dd309..3cc7fec86a 100644 --- a/src/context/AppContext.js +++ b/src/context/AppContext.js @@ -69,10 +69,10 @@ export const AppReducer = (state, action) => { state.currency = action.payload; return { ...state - } + }; + default: + return state; - default: - return state; } };