diff --git a/src/App.js b/src/App.js index 610e47d694..24908b9ba8 100644 --- a/src/App.js +++ b/src/App.js @@ -4,40 +4,76 @@ import 'bootstrap/dist/css/bootstrap.min.css'; //Code to import Budget.js import Budget from './components/Budget'; - +import Remaining from './components/Remaining'; +import ExpenseTotal from './components/ExpenseTotal'; // Add code to import the other components here under import { AppProvider } from './context/AppContext'; +import ExpenseList from './components/ExpenseList'; +import ExpenseItem from './components/ExpenseItem'; +import AllocationForm from './components/AllocationForm'; 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 */ - } +
+ { + /* 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 */ +
+ +
+

+ +
+ +
+ }
diff --git a/src/components/AllocationForm.js b/src/components/AllocationForm.js index 8b13789179..3b1aafb55c 100644 --- a/src/components/AllocationForm.js +++ b/src/components/AllocationForm.js @@ -1 +1,82 @@ +import React, { useContext, useState } from 'react'; +import { AppContext } from '../context/AppContext'; + +const AllocationForm = (props) => { + const { dispatch,remaining } = 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; \ No newline at end of file diff --git a/src/components/Budget.js b/src/components/Budget.js index 8b13789179..4a12fc6ea3 100644 --- a/src/components/Budget.js +++ b/src/components/Budget.js @@ -1 +1,28 @@ +import React, { useContext, useState } from 'react'; +import { AppContext } from '../context/AppContext'; +const Budget = () => { + const { budget,expenses } = useContext(AppContext); + const [newBudget, setNewBudget] = useState(budget); + const totalExpenses = expenses.reduce((total, item) => { + return (total += item.cost); + }, 0); + const handleBudgetChange = (event) => { + if(event.target.value > 20000){ + alert(`The budget should not exceed 20000`) + } + else if(event.target.value < totalExpenses){ + alert(`You cannot reduce the budget value lower than the spending`) + } + else{ + setNewBudget(event.target.value); + } + } + return ( +
+Budget: £ + +
+ ); +}; +export default Budget; diff --git a/src/components/ExpenseItem.js b/src/components/ExpenseItem.js index 8b13789179..8128981882 100644 --- a/src/components/ExpenseItem.js +++ b/src/components/ExpenseItem.js @@ -1 +1,39 @@ +import React, { useContext } from 'react'; +import { TiDelete } from 'react-icons/ti'; +import { AppContext } from '../context/AppContext'; + +const ExpenseItem = (props) => { + const { 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 + }); + + } + + return ( + + {props.name} + £{props.cost} + + + + ); +}; + +export default ExpenseItem; \ No newline at end of file diff --git a/src/components/ExpenseList.js b/src/components/ExpenseList.js index 8b13789179..3f10dc3bad 100644 --- a/src/components/ExpenseList.js +++ b/src/components/ExpenseList.js @@ -1 +1,28 @@ +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 10Delete
+ ); +}; + +export default ExpenseList; \ No newline at end of file diff --git a/src/components/ExpenseTotal.js b/src/components/ExpenseTotal.js index 8b13789179..2d4f695f05 100644 --- a/src/components/ExpenseTotal.js +++ b/src/components/ExpenseTotal.js @@ -1 +1,18 @@ +import React, { useContext } from 'react'; +import { AppContext } from '../context/AppContext'; + + +const ExpenseTotal = () => { + const { expenses } = useContext(AppContext); + const totalExpenses = expenses.reduce((total, item) => { + return (total += item.cost); + }, 0); + return ( + +
+ Spent so far: £{totalExpenses} +
+ ); +}; +export default ExpenseTotal; \ No newline at end of file diff --git a/src/components/Remaining.js b/src/components/Remaining.js index 8b13789179..0bbd07dbef 100644 --- a/src/components/Remaining.js +++ b/src/components/Remaining.js @@ -1 +1,16 @@ +import React, { useContext } from 'react'; +import { AppContext } from '../context/AppContext'; +const Remaining = () => { + const { expenses, budget } = useContext(AppContext); + const totalExpenses = expenses.reduce((total, item) => { + return (total = total + item.cost); + }, 0); + const alertType = totalExpenses > budget ? 'alert-danger' : 'alert-success'; + return ( +
+ Remaining: £{budget - totalExpenses} +
+ ); +}; +export default Remaining; \ No newline at end of file