Skip to content

Submission #505

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 33 additions & 33 deletions src/App.js
Original file line number Diff line number Diff line change
@@ -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 (
<AppProvider>
<div className='container'>
<h1 className='mt-3'>Company's Budget Allocation</h1>
<div className='row mt-3'>
    {
/* 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 */
}

<div className='row mt-3'>
<div className='col-sm'>
<Budget />
</div>
<div className='col-sm'>
<RemainingBudget />
</div>
<div className='col-sm'>
<ExpenseTotal />
</div>
<div className='col-sm'>
<Currency/>
</div>
</div>
<h3 className='mt-3'>Allocation</h3>
<div className='row '>
<div className='col-sm'>
<ExpenseList />
</div>
</div>
<h3 className='mt-3'>Change allocation</h3>
<div className='row mt-3'>
<div className='col-sm'>
<AllocationForm/>
</div>
</div>
</div>
</AppProvider>
);
};
export default App;

export default App;
78 changes: 78 additions & 0 deletions src/components/AllocationForm.js
Original file line number Diff line number Diff line change
@@ -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 (
<div>
<div className='row'>

<div className="input-group mb-3" style={{ marginLeft: '2rem' }}>
<div className="input-group-prepend">
<label className="input-group-text" htmlFor="inputGroupSelect01">Department</label>
</div>
<select className="custom-select" id="inputGroupSelect01" onChange={(event) => setName(event.target.value)}>
<option defaultValue>Choose...</option>
<option value="Marketing" name="marketing"> Marketing</option>
<option value="Sales" name="sales">Sales</option>
<option value="Finance" name="finance">Finance</option>
<option value="HR" name="hr">HR</option>
<option value="IT" name="it">IT</option>
<option value="Admin" name="admin">Admin</option>
</select>

<div className="input-group-prepend" style={{ marginLeft: '2rem' }}>
<label className="input-group-text" htmlFor="inputGroupSelect02">Allocation</label>
</div>
<select className="custom-select" id="inputGroupSelect02" onChange={(event) => setAction(event.target.value)}>
<option defaultValue value="Add" name="Add">Add</option>
<option value="Reduce" name="Reduce">Reduce</option>
</select> {currency}<input
required='required'
type='number'
id='cost'
value={cost}
style={{ marginLeft: '2rem' , size: 10}}
onChange={(event) => setCost(event.target.value)}>
</input>

<button className="btn btn-primary" onClick={submitEvent} style={{ marginLeft: '2rem' }}>
Save
</button>
</div>
</div>

</div>
);
};

export default AllocationForm;
30 changes: 30 additions & 0 deletions src/components/Budget.js
Original file line number Diff line number Diff line change
@@ -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<totalExpenses){
alert("The value cannot be less than spending");
}
else{
dispatch({
type:'SET_BUDGET',
payload:event.target.value
});
}

}
return (
<div className='alert alert-secondary'>
<span>Budget: {currency}</span>
<input type="number" step="10" value={budget} onChange={handleBudgetChange}></input>
</div>
);
};
export default Budget;
24 changes: 24 additions & 0 deletions src/components/Currency.js
Original file line number Diff line number Diff line change
@@ -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(
<div className='alert alert-secondary'>
<label for="cur">Currency : {currency} </label>
<select name="cur" id="cur" onChange={handleCurrencyChange}>
<option value="₹">₹ rupees</option>
<option value="$">$ dollor</option>
<option value="€">€ euro</option>
<option value="£">£ Pound</option>
</select>
</div>
);
};
export default Currency;
51 changes: 51 additions & 0 deletions src/components/ExpenseItem.js
Original file line number Diff line number Diff line change
@@ -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 (
<tr>
<td>{props.name}</td>
<td>{currency}{props.cost}</td>
<td><HiPlusCircle onClick={event=> increaseAllocation(props.name)}></HiPlusCircle></td>
<td><HiOutlineMinusCircle onClick={event=> decreaseAllocation(props.name)}></HiOutlineMinusCircle></td>
<td><TiDelete size='1.5em' onClick={handleDeleteExpense}></TiDelete></td>
</tr>
);
};

export default ExpenseItem;
28 changes: 28 additions & 0 deletions src/components/ExpenseList.js
Original file line number Diff line number Diff line change
@@ -1 +1,29 @@

import React, { useContext } from 'react';
import ExpenseItem from './ExpenseItem';
import { AppContext } from '../context/AppContext';

const ExpenseList = () => {
const { expenses } = useContext(AppContext);

return (
<table className='table'>
<thead className="thead-light">
<tr>
<th scope="col">Department</th>
<th scope="col">Allocated Budget</th>
<th scope="col">Increase by 10</th>
<th scope="col">Decrease by 10</th>
<th scope="col">Delete</th>
</tr>
</thead>
<tbody>
{expenses.map((expense) => (
<ExpenseItem id={expense.id} key={expense.id} name={expense.name} cost={expense.cost} />
))}
</tbody>
</table>
);
};

export default ExpenseList;
14 changes: 14 additions & 0 deletions src/components/ExpenseTotal.js
Original file line number Diff line number Diff line change
@@ -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 (
<div className='alert alert-primary'>
<span>Spent so far: {currency}{totalExpenses}</span>
</div>
);
};
export default ExpenseTotal;
15 changes: 15 additions & 0 deletions src/components/Remaining.js
Original file line number Diff line number Diff line change
@@ -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 (
<div className={`alert ${alertType}`}>
<span>Remaining: {currency}{budget - totalExpenses}</span>
</div>
);
};
export default Remaining;
2 changes: 1 addition & 1 deletion src/context/AppContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down