mirror of
https://github.com/gabehf/BudgetBuddy.git
synced 2026-03-11 00:10:38 -07:00
54 lines
1.1 KiB
JavaScript
54 lines
1.1 KiB
JavaScript
import React, { useContext, useState } from 'react';
|
|
import { AppContext } from '../context/AppContext';
|
|
import { v4 as uuidv4 } from 'uuid';
|
|
|
|
const AddExpenseForm = (props) => {
|
|
const { dispatch } = useContext(AppContext);
|
|
|
|
const [name, setName] = useState('');
|
|
const [cost, setCost] = useState('');
|
|
|
|
const onSubmit = (event) => {
|
|
event.preventDefault();
|
|
const expense = {
|
|
id: uuidv4(),
|
|
name,
|
|
cost: parseInt(cost),
|
|
};
|
|
|
|
dispatch({
|
|
type: 'ADD_EXPENSE',
|
|
payload: expense,
|
|
});
|
|
|
|
setName('');
|
|
setCost('');
|
|
};
|
|
|
|
return (
|
|
<form onSubmit={onSubmit}>
|
|
<div className='row'>
|
|
<div className='col-lg-3'>
|
|
<label htmlFor='cost'>Income</label>
|
|
<input
|
|
required='required'
|
|
type='number'
|
|
className='form-control'
|
|
id='cost'
|
|
value={cost}
|
|
onChange={(event) => setCost(event.target.value)}
|
|
/>
|
|
</div>
|
|
</div>
|
|
<div className='row mt-3'>
|
|
<div className='col-sm'>
|
|
<button type='submit' className='btn btn-primary'>
|
|
Save
|
|
</button>
|
|
</div>
|
|
</div>
|
|
</form>
|
|
);
|
|
};
|
|
|
|
export default AddExpenseForm;
|