diff --git a/src/App.js b/src/App.js index 610e47d694..a892d55f43 100644 --- a/src/App.js +++ b/src/App.js @@ -1,47 +1,47 @@ import React from 'react'; - import 'bootstrap/dist/css/bootstrap.min.css'; -//Code to import Budget.js -import Budget from './components/Budget'; - -// Add code to import the other components here under - - 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'; 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..16210ee953 100644 --- a/src/components/AllocationForm.js +++ b/src/components/AllocationForm.js @@ -1 +1,79 @@ +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 ( +
+
+ +
+
+ +
+ + +
+ +
+ {currency} 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..6ad51fee19 100644 --- a/src/components/Budget.js +++ b/src/components/Budget.js @@ -1 +1,31 @@ +import React, { useContext } from 'react'; +import { AppContext } from '../context/AppContext'; + +const Budget = () => { + const { dispatch,budget,expenses,currency } = useContext(AppContext); + const totalExpenses = expenses.reduce((total, item) => { + return (total += item.cost); + }, 0); + const handleBudgetChange = (event) => { + if(event.target.value>20000){ + alert("The value cannot be more than 20000"); + }else if(event.target.value +Budget: {currency} + +
+ ); +}; +export default Budget; \ No newline at end of file diff --git a/src/components/Currency.js b/src/components/Currency.js new file mode 100644 index 0000000000..ab2453956c --- /dev/null +++ b/src/components/Currency.js @@ -0,0 +1,24 @@ + +import React, { useContext } from 'react'; +import { AppContext } from '../context/AppContext'; +const Currency = ()=>{ + const {dispatch,currency}=useContext(AppContext); + const handleCurrencyChange = (event) => { + dispatch({ + type:'CHG_CURRENCY', + payload:event.target.value + }) + } + return( +
+ + +
+ ); +}; +export default Currency; \ No newline at end of file diff --git a/src/components/ExpenseItem.js b/src/components/ExpenseItem.js index 8b13789179..827b46e5df 100644 --- a/src/components/ExpenseItem.js +++ b/src/components/ExpenseItem.js @@ -1 +1,52 @@ +import React, { useContext } from 'react'; +import { TiDelete } from 'react-icons/ti'; +import { HiOutlineMinusCircle,HiPlusCircle } from 'react-icons/hi'; +import { AppContext } from '../context/AppContext'; + +const ExpenseItem = (props) => { + const { dispatch,currency } = 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: 'RED_EXPENSE', + payload: expense + }); + + } + return ( + + {props.name} + {currency}{props.cost} + increaseAllocation(props.name)}> + decreaseAllocation(props.name)}> + + + ); +}; + +export default ExpenseItem; \ No newline at end of file diff --git a/src/components/ExpenseList.js b/src/components/ExpenseList.js index 8b13789179..d02849d209 100644 --- a/src/components/ExpenseList.js +++ b/src/components/ExpenseList.js @@ -1 +1,29 @@ +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; \ No newline at end of file diff --git a/src/components/ExpenseTotal.js b/src/components/ExpenseTotal.js index 8b13789179..cbacad77a6 100644 --- a/src/components/ExpenseTotal.js +++ b/src/components/ExpenseTotal.js @@ -1 +1,15 @@ +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; \ No newline at end of file diff --git a/src/components/Remaining.js b/src/components/Remaining.js index 8b13789179..d41ee658f0 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,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; \ No newline at end of file diff --git a/src/context/AppContext.js b/src/context/AppContext.js index 88214dd309..aeea3924d2 100644 --- a/src/context/AppContext.js +++ b/src/context/AppContext.js @@ -86,7 +86,7 @@ const initialState = { { id: "Human Resource", name: 'Human Resource', cost: 40 }, { id: "IT", name: 'IT', cost: 500 }, ], - currency: '£' + currency: '₹' }; // 2. Creates the context this is the thing our components import and use to get the state