-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create Goals and GoalsAdd subpage, add and delete goal feature for Is…
…sue #14
- Loading branch information
1 parent
c0e7a84
commit 29dfb45
Showing
13 changed files
with
195 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import { createSlice } from "@reduxjs/toolkit"; | ||
|
||
const goalsSlice = createSlice({ | ||
name:'goals', | ||
initialState: [], | ||
reducers: { | ||
setGoalsData: (state, action)=>{ | ||
return action.payload; | ||
} | ||
} | ||
}) | ||
|
||
export const {setGoalsData} = goalsSlice.actions; | ||
export default goalsSlice.reducer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import React, { useEffect } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import AddButton from '../../components/layout/AddButton'; | ||
import { getData, deleteData } from '../../api/firebase'; | ||
import { DatagridColumnType, GoalFormType } from '../../state/types'; | ||
//import redux | ||
import { RootState } from '../../redux/store'; | ||
import { setGoalsData } from '../../redux/goalsSlice'; | ||
import { useDispatch, useSelector } from 'react-redux'; | ||
//import MUI | ||
import Box from '@mui/material/Box'; | ||
import { DataGrid, GridCellParams, GridActionsCellItem} from '@mui/x-data-grid'; | ||
import DeleteIcon from '@mui/icons-material/Delete'; | ||
import EditIcon from '@mui/icons-material/Edit'; | ||
|
||
|
||
export const Goals = () => { | ||
const goalsData: GoalFormType[] = useSelector((store: RootState)=>store.goals); | ||
const dispatch = useDispatch(); | ||
const navigate = useNavigate(); | ||
//define a delete function | ||
const handleDelete = (id:string) => { | ||
deleteData(id, "goals"); | ||
// delete the row from data grid | ||
const updatedGoals = goalsData.filter((item)=>item["id"] !== id); | ||
dispatch(setGoalsData(updatedGoals)); | ||
} | ||
//define an edit function that directs users to a separate page to edit a particular debt item | ||
const handleEdit = (id:string) => { | ||
const goalsToEdit = goalsData.find((item)=>item["id"] === id); | ||
|
||
if(goalsToEdit){ | ||
navigate(`/goals/edit/${id}`, {state: goalsToEdit}); | ||
} | ||
} | ||
const goalsColumns: DatagridColumnType[] = [ | ||
{ | ||
field: "Name", | ||
headerName: "Name", | ||
width: 150, | ||
}, | ||
{ | ||
field: "Amount", | ||
headerName: "Amount", | ||
width: 150, | ||
renderCell: (params: GridCellParams)=> `$${params.value}`, //add a dollar sign | ||
}, | ||
{ | ||
field: "Due", | ||
headerName: "Due Date", | ||
width: 150, | ||
}, | ||
{field: "actions", | ||
headerName: "Actions", | ||
type:"actions", | ||
width: 100, | ||
getActions: (params) => { | ||
const id = String(params.id); | ||
|
||
return[ | ||
<GridActionsCellItem | ||
icon={<EditIcon />} | ||
label="Edit" | ||
onClick={()=>handleEdit(id)} | ||
/>, | ||
<GridActionsCellItem | ||
icon={<DeleteIcon />} | ||
label="Delete" | ||
onClick={()=>handleDelete(id)} | ||
/> ] | ||
} | ||
} | ||
] | ||
// fetch data from Firebase when the component mounts | ||
useEffect(()=>{ | ||
const fetchData = async() => { | ||
const data: GoalFormType[] = await getData("goals"); | ||
const formattedData: GoalFormType[] = data.map((item)=>({ | ||
...item, | ||
Amount: Number(item.Amount), // ensure Amount has type number | ||
})) | ||
dispatch(setGoalsData(formattedData)); | ||
}; | ||
fetchData(); | ||
},[dispatch]); | ||
|
||
return ( | ||
<div className="subpage"> | ||
<h1>Goals</h1> | ||
<Box mt="1rem" p="0 0.5rem" sx={{ width: '40%'}}> | ||
<DataGrid autoHeight rows={goalsData} columns={goalsColumns} hideFooter={true}/> | ||
</Box> | ||
<AddButton linkurl="/goals/add" btntxt="Add Goal" /> | ||
</div> | ||
) | ||
} | ||
|
||
export default Goals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React, { useState } from 'react'; | ||
import { useNavigate } from 'react-router-dom'; | ||
import SubmitButton from '../../components/layout/SubmitButton'; | ||
import FormRow from '../../components/FormRow'; | ||
import { GoalFormType } from '../../state/types'; | ||
import { TextField } from '@mui/material'; | ||
import { addGoalsData } from '../../api'; | ||
|
||
const GoalsAdd = () => { | ||
const [formData, setFormData] = useState<GoalFormType>({ | ||
Name: '', | ||
Amount: 0, | ||
Due: new Date(), | ||
}) | ||
|
||
// handleAdd() function updates formData as we receive user input | ||
const handleAdd =(e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement>) => { | ||
const {name, value, type} = e.target; | ||
setFormData(prevData => ({ | ||
...prevData, | ||
[name]: type==='number'?parseFloat(value):value, | ||
})) | ||
} | ||
// useNavigate() redirects users back to Assets page upon successful form submission | ||
const navigate = useNavigate(); | ||
|
||
// handleSubmit() sends formData to the database | ||
const handleSubmit = async (e: React.FormEvent<HTMLFormElement>) => { | ||
e.preventDefault(); | ||
try { | ||
await addGoalsData(formData); | ||
} catch(error){ | ||
console.log(error); | ||
} | ||
navigate("/goals"); //redirects back to Goals page | ||
}; | ||
return ( | ||
<div className="subpage"> | ||
<h1>Add Goal</h1> | ||
<form onSubmit={handleSubmit}> | ||
<FormRow type="text" name="Name" value={formData.Name} handleChange={handleAdd} textLabel="Name"/> | ||
<FormRow type="number" name="Amount" value={formData.Amount} handleChange={handleAdd} textLabel="Amount"/> | ||
<br/> | ||
<TextField type="date" label="Due Date" name="Due" value={formData.Due} onChange={handleAdd} fullWidth required></TextField> | ||
<SubmitButton textLabel="Save" /> | ||
</form> | ||
</div> | ||
) | ||
} | ||
|
||
export default GoalsAdd |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters