Skip to content

Commit

Permalink
fixes and add ability to delete goals
Browse files Browse the repository at this point in the history
  • Loading branch information
sdevalapurkar committed Oct 20, 2021
1 parent ce44b0c commit e1d462c
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 1 deletion.
31 changes: 31 additions & 0 deletions api/src/paths/goal/{goalId}/delete.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
const { getCustomError } = require("../../../helpers/errorHandler");
const { getAuthenticatedUser } = require("../../../helpers/authenticationHelpers");

async function deleteGoal(req, res, db) {
if (
!req.params ||
!req.params.goal_id
) {
return res.status(400).json(getCustomError(400));
}

const authenticatedUser = getAuthenticatedUser(req.headers);

if (!authenticatedUser) {
return res.status(401).json(getCustomError(401));
}

await db('goals').where({
user_email: authenticatedUser.email,
id: req.params.goal_id
}).del();

return res.json({
type: "Goal deleted",
attributes: {
value: req.params.goal_id
}
});
}

module.exports = deleteGoal;
5 changes: 5 additions & 0 deletions api/src/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const addGoal = require("./paths/goal/add");
const getGoals = require("./paths/goal/get");
const getGoal = require("./paths/goal/{goalId}/get");
const updateGoalProgress = require("./paths/goal/{goalId}/updateProgress");
const deleteGoal = require("./paths/goal/{goalId}/delete");

const db = knex({
client: "postgres",
Expand Down Expand Up @@ -73,5 +74,9 @@ app.put("/v1/goals/:goal_id/progress", async (req, res) => {
return updateGoalProgress(req, res, db);
});

app.delete("/v1/goals/:goal_id", async (req, res) => {
return deleteGoal(req, res, db);
});

// API is listening and server is up
app.listen(PORT, () => console.log(`Server up at http:https://localhost:${PORT}`));
18 changes: 17 additions & 1 deletion app/src/components/Homepage.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ function Homepage() {
setExistingGoals(value);
};

const deleteGoal = async (goal) => {
const response = await axios.delete(
`http:https://${apiHost}:${apiPort}/v1/goals/${goal.id}`, {
headers: {
Authorization: `Bearer ${sessionStorage.getItem("jwt")}`
}
}
);

if (!response || !response.data) {
return;
}

getExistingGoals();
};

const handleSubmitNewGoal = async () => {
if (!goalName || !goalTimes || !goalStartDate || !goalEndDate) {
setGoalDialogError('All fields of the form are required. Please fill them out.');
Expand Down Expand Up @@ -173,7 +189,7 @@ function Homepage() {
color="primary"
aria-label="delete-goal"
data-testid="delete-goal"
onClick={() => console.log(row)}>
onClick={() => deleteGoal(row)}>
<Icon path={mdiTrashCanOutline} size={1} />
</IconButton>
</Box>
Expand Down

0 comments on commit e1d462c

Please sign in to comment.