From 2e6d5a5815865b79d544ec05c9d0d93c91ee848e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?L=C3=AA=20C=C3=B4ng=20L=C3=BD?= <58856636+lecongly@users.noreply.github.com> Date: Thu, 24 Nov 2022 16:37:13 +0700 Subject: [PATCH] feat(client): delete accout --- client/components/user/Security.tsx | 32 +++++++++++++++++++++++++++-- client/services/userApi.ts | 17 +++++++++++++-- 2 files changed, 45 insertions(+), 4 deletions(-) diff --git a/client/components/user/Security.tsx b/client/components/user/Security.tsx index 1592a50..e296dba 100644 --- a/client/components/user/Security.tsx +++ b/client/components/user/Security.tsx @@ -1,6 +1,30 @@ import React from 'react' +import {useDeleteUserMutation} from '../../services/userApi' +import {useAppDispatch, useAppSelector} from '../../store/hooks' +import {logout, setUser} from '../../features/authSlice' +import {toast} from 'react-toastify' +import {useRouter} from 'next/router' const Security = () => { + const router = useRouter() + const dispatch = useAppDispatch() + const {user, token} = useAppSelector((state) => state.persistedReducer.auth) + + const [deleteUser, {isLoading: isDeleting}] = useDeleteUserMutation() + + const handleDeleteMyAccount = () => { + deleteUser(user?._id as string).unwrap() + .then((result) => { + console.log(result) + toast.success(result.message || 'Delete success') + dispatch(logout()) + router.push('/') + }) + .catch((error) => { + console.log(error) + toast.error(error.data.message || 'Delete fail. Something went wrong') + }) + } return (