Skip to content

Commit

Permalink
Merge pull request #143 from simplitrac/delete-user
Browse files Browse the repository at this point in the history
Temporary delete_user functionality. Doesn't wait for backend response.
  • Loading branch information
anpaulan authored Sep 9, 2024
2 parents 4ab7213 + eb00bcf commit 6c90f09
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 6 deletions.
28 changes: 24 additions & 4 deletions client/src/components/HamburgerMenu.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import React, {useContext, useEffect} from 'react';
import React, { useContext, useEffect } from 'react';
import User from '../models/User.js';
import {
Drawer,
DrawerBody,
Expand All @@ -15,6 +16,7 @@ import {
import {HamburgerIcon} from "@chakra-ui/icons";
import {AppContext} from '../context/AppContext.jsx';
import SignOut from "./SignOut.jsx";
import { set } from 'react-hook-form';

// const HamburgerMenu = ({setRunTour}) => {
const HamburgerMenu = () => {
Expand All @@ -24,9 +26,8 @@ const HamburgerMenu = () => {
renderNewScreen,
showCategories,
toggleCategoriesList,
user,
setRunTour,
setRunChartTour
user, setRunTour, setRunChartTour, setIsUpdating,
setServerResponse, setScreen, resetAppState
} = useContext(AppContext);
const {isOpen, onOpen, onClose} = useDisclosure();

Expand All @@ -43,6 +44,19 @@ const HamburgerMenu = () => {
setRunTour(true);
};

const handleDeleteAccount = async () => {
if (window.confirm("Are you sure you want to delete your account?")) {
setIsUpdating(true)
console.log(user.user_id)
const userToDelete = new User(user)
localStorage.removeItem('user')
// const result = await userToDelete.deleteUser(user.user_id)
userToDelete.deleteUser(user.user_id)
// setServerResponse(result)
resetAppState()
}
}

return (
<>
<IconButton
Expand Down Expand Up @@ -102,6 +116,12 @@ const HamburgerMenu = () => {
onClick={toggleCategoriesList}>
{showCategories ? "Hide Categories" : "Show Categories"}
</Button>
<Button
variant="ghost"
w="100%"
onClick={handleDeleteAccount}>
Delete Account
</Button>
<SignOut
variant="outline"
w="100%"
Expand Down
26 changes: 24 additions & 2 deletions client/src/models/User.js
Original file line number Diff line number Diff line change
Expand Up @@ -177,13 +177,35 @@ class User {
return new User();
}
}
}

async deleteUser(user_id) {
const init = {
method: "DELETE",
headers: {
"Content-Type": "application/json",
Accept: "application/json",
},
};
const endPoint = `${import.meta.env.VITE_PROD_DELETE_USER_ENDPOINT}?user_id=${user_id}`;

try {
const res = await fetch(endPoint, init);
return res.json();
} catch (error) {
console.error('Error deleting account:', error);
return null;
}
}
}




function toProperCase(name) {
if (!name) return;
const lower = name.toLowerCase();
return lower.charAt(0).toUpperCase() + lower.slice(1);
};
}


export default User;

0 comments on commit 6c90f09

Please sign in to comment.