Merge pull request #302 from bluewave-labs/feat/account-deletion
Account deletion
This commit is contained in:
2 files changed
+59
-8
No files matched your search
@@ -12,11 +12,16 @@ import {
|
|||||||
imageValidation,
|
imageValidation,
|
||||||
} from "../../../Validation/validation";
|
} from "../../../Validation/validation";
|
||||||
import { useDispatch, useSelector } from "react-redux";
|
import { useDispatch, useSelector } from "react-redux";
|
||||||
import { update } from "../../../Features/Auth/authSlice";
|
import {
|
||||||
|
clearAuthState,
|
||||||
|
deleteUser,
|
||||||
|
update,
|
||||||
|
} from "../../../Features/Auth/authSlice";
|
||||||
import ImageField from "../../TextFields/Image";
|
import ImageField from "../../TextFields/Image";
|
||||||
import ImageIcon from "@mui/icons-material/Image";
|
import ImageIcon from "@mui/icons-material/Image";
|
||||||
import ProgressUpload from "../../ProgressBars";
|
import ProgressUpload from "../../ProgressBars";
|
||||||
import { formatBytes } from "../../../Utils/fileUtils";
|
import { formatBytes } from "../../../Utils/fileUtils";
|
||||||
|
import { clearMonitorState } from "../../../Features/Monitors/monitorsSlice";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* ProfilePanel component displays a form for editing user profile information
|
* ProfilePanel component displays a form for editing user profile information
|
||||||
@@ -178,8 +183,12 @@ const ProfilePanel = () => {
|
|||||||
};
|
};
|
||||||
|
|
||||||
// Initiates the account deletion process
|
// Initiates the account deletion process
|
||||||
const handleDeleteAccount = () => {
|
const handleDeleteAccount = async () => {
|
||||||
//TODO - implement delete account function
|
const action = await dispatch(deleteUser(authToken));
|
||||||
|
if (action.payload.success) {
|
||||||
|
dispatch(clearAuthState());
|
||||||
|
dispatch(clearMonitorState());
|
||||||
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
// Modal state and control functions
|
// Modal state and control functions
|
||||||
|
|||||||
@@ -52,6 +52,7 @@ export const update = createAsyncThunk(
|
|||||||
if (form.file && form.file !== "") {
|
if (form.file && form.file !== "") {
|
||||||
const imageResult = await axiosInstance.get(form.file, {
|
const imageResult = await axiosInstance.get(form.file, {
|
||||||
responseType: "blob",
|
responseType: "blob",
|
||||||
|
baseURL: "",
|
||||||
});
|
});
|
||||||
fd.append("profileImage", imageResult.data);
|
fd.append("profileImage", imageResult.data);
|
||||||
}
|
}
|
||||||
@@ -74,6 +75,25 @@ export const update = createAsyncThunk(
|
|||||||
}
|
}
|
||||||
);
|
);
|
||||||
|
|
||||||
|
export const deleteUser = createAsyncThunk(
|
||||||
|
"auth/delete",
|
||||||
|
async (data, thunkApi) => {
|
||||||
|
const user = jwtDecode(data);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await axiosInstance.delete(`/auth/user/${user._id}`, {
|
||||||
|
headers: { Authorization: `Bearer ${data}` },
|
||||||
|
});
|
||||||
|
return res.data;
|
||||||
|
} catch (error) {
|
||||||
|
if (error.response && error.response.data) {
|
||||||
|
return thunkApi.rejectWithValue(error.response.data);
|
||||||
|
}
|
||||||
|
return thunkApi.rejectWithValue(error.message);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
);
|
||||||
|
|
||||||
const handleAuthFulfilled = (state, action) => {
|
const handleAuthFulfilled = (state, action) => {
|
||||||
state.isLoading = false;
|
state.isLoading = false;
|
||||||
state.success = action.payload.success;
|
state.success = action.payload.success;
|
||||||
@@ -101,6 +121,16 @@ const handleUpdateRejected = (state, action) => {
|
|||||||
? action.payload.msg
|
? action.payload.msg
|
||||||
: "Failed to update profile data.";
|
: "Failed to update profile data.";
|
||||||
};
|
};
|
||||||
|
const handleDeleteFulfilled = (state, action) => {
|
||||||
|
state.isLoading = false;
|
||||||
|
state.success = action.payload.success;
|
||||||
|
state.msg = action.payload.msg;
|
||||||
|
};
|
||||||
|
const handleDeleteRejected = (state, action) => {
|
||||||
|
state.isLoading = false;
|
||||||
|
state.success = false;
|
||||||
|
state.msg = action.payload ? action.payload.msg : "Failed to delete account.";
|
||||||
|
};
|
||||||
|
|
||||||
const authSlice = createSlice({
|
const authSlice = createSlice({
|
||||||
name: "auth",
|
name: "auth",
|
||||||
@@ -115,25 +145,37 @@ const authSlice = createSlice({
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
extraReducers: (builder) => {
|
extraReducers: (builder) => {
|
||||||
|
// Register thunk
|
||||||
builder
|
builder
|
||||||
// Register thunk
|
|
||||||
.addCase(register.pending, (state) => {
|
.addCase(register.pending, (state) => {
|
||||||
state.isLoading = true;
|
state.isLoading = true;
|
||||||
})
|
})
|
||||||
.addCase(register.fulfilled, handleAuthFulfilled)
|
.addCase(register.fulfilled, handleAuthFulfilled)
|
||||||
.addCase(register.rejected, handleAuthRejected)
|
.addCase(register.rejected, handleAuthRejected);
|
||||||
// Login thunk
|
|
||||||
|
// Login thunk
|
||||||
|
builder
|
||||||
.addCase(login.pending, (state) => {
|
.addCase(login.pending, (state) => {
|
||||||
state.isLoading = true;
|
state.isLoading = true;
|
||||||
})
|
})
|
||||||
.addCase(login.fulfilled, handleAuthFulfilled)
|
.addCase(login.fulfilled, handleAuthFulfilled)
|
||||||
.addCase(login.rejected, handleAuthRejected)
|
.addCase(login.rejected, handleAuthRejected);
|
||||||
// Update thunk
|
|
||||||
|
// Update thunk
|
||||||
|
builder
|
||||||
.addCase(update.pending, (state) => {
|
.addCase(update.pending, (state) => {
|
||||||
state.isLoading = true;
|
state.isLoading = true;
|
||||||
})
|
})
|
||||||
.addCase(update.fulfilled, handleUpdateFulfilled)
|
.addCase(update.fulfilled, handleUpdateFulfilled)
|
||||||
.addCase(update.rejected, handleUpdateRejected);
|
.addCase(update.rejected, handleUpdateRejected);
|
||||||
|
|
||||||
|
// Delete thunk
|
||||||
|
builder
|
||||||
|
.addCase(deleteUser.pending, (state) => {
|
||||||
|
state.isLoading = true;
|
||||||
|
})
|
||||||
|
.addCase(deleteUser.fulfilled, handleDeleteFulfilled)
|
||||||
|
.addCase(deleteUser.rejected, handleDeleteRejected);
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user