-
Notifications
You must be signed in to change notification settings - Fork 23
added admin controllerwhere admin can delete user id #99
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Dynamic-Aryan
wants to merge
8
commits into
alvarotorrestx:dev
Choose a base branch
from
Dynamic-Aryan:adminpart
base: dev
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 1 commit
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
2882be3
added admin controllerwhere admin can delete user id
aryanpachchigar 007797d
added admin controller updated as required
aryanpachchigar 452c778
added admin controller updated as required
aryanpachchigar 6a1ed2f
updated new things for admin and owner
aryanpachchigar 74e50e8
updated new things for admin and owner
aryanpachchigar eb77279
as per requiremnt ,new changes are done hereby for admin
aryanpachchigar a180767
as per requiremnt ,new changes are done hereby for admin
aryanpachchigar 61e1da1
Merge branch 'dev' into adminpart
alvarotorrestx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains hidden or 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 hidden or 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 hidden or 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,156 @@ | ||
| import React, { useEffect, useState } from "react"; | ||
| import { Link } from "react-router-dom"; | ||
| import Loading from "../subcomponents/Loading"; | ||
| import useAuth from "../../../auth/useAuth"; | ||
| import { axiosPrivate } from "../../../api/axios"; | ||
|
|
||
| const Adminpage = () => { | ||
| const { auth } = useAuth(); | ||
| const [users, setUsers] = useState([]); | ||
| const [loading, setLoading] = useState(true); | ||
|
|
||
| const [showModal, setShowModal] = useState(false); | ||
| const [userToDelete, setUserToDelete] = useState(null); | ||
| const [statusMessage, setStatusMessage] = useState(null); | ||
|
|
||
| const fetchUsers = async () => { | ||
| setLoading(true); | ||
| try { | ||
| const response = await axiosPrivate.get("/api/users", { | ||
| headers: { | ||
| Authorization: `Bearer ${auth?.accessToken}`, | ||
| }, | ||
| }); | ||
| setUsers(response.data); | ||
| } catch (err) { | ||
| console.error("Error fetching users:", err); | ||
| } finally { | ||
| setLoading(false); | ||
| } | ||
| }; | ||
|
|
||
| useEffect(() => { | ||
| fetchUsers(); | ||
| }, [auth?.accessToken]); | ||
|
|
||
| const confirmDelete = (user) => { | ||
| setUserToDelete(user); | ||
| setShowModal(true); | ||
| }; | ||
|
|
||
| const handleDeleteConfirmed = async () => { | ||
| if (!userToDelete) return; | ||
|
|
||
| try { | ||
| const res = await axiosPrivate.delete(`/api/users/${userToDelete._id}`, { | ||
| headers: { | ||
| Authorization: `Bearer ${auth?.accessToken}`, | ||
| }, | ||
| }); | ||
|
|
||
| if (res.status === 200 || res.status === 204) { | ||
| setStatusMessage({ | ||
| type: "success", | ||
| text: `User "${userToDelete.username}" deleted successfully.`, | ||
| }); | ||
| fetchUsers(); | ||
| } else { | ||
| throw new Error("Unexpected response status"); | ||
| } | ||
| } catch (err) { | ||
| console.error("Error deleting user:", err); | ||
| setStatusMessage({ | ||
| type: "error", | ||
| text: "Failed to delete user.", | ||
| }); | ||
| } finally { | ||
| setShowModal(false); | ||
| setUserToDelete(null); | ||
| } | ||
| }; | ||
|
|
||
| return ( | ||
| <div className="max-w-6xl mx-auto p-6 bg-base-100 rounded-xl shadow-lg mt-10"> | ||
| <h2 className="text-2xl font-semibold mb-6 text-primary">User Management</h2> | ||
|
|
||
|
|
||
| {statusMessage && ( | ||
| <div | ||
| className={`mb-4 p-3 rounded ${ | ||
| statusMessage.type === "success" | ||
| ? "bg-green-100 text-green-800" | ||
| : "bg-red-100 text-red-800" | ||
| }`} | ||
| > | ||
| {statusMessage.text} | ||
| </div> | ||
| )} | ||
|
|
||
| {loading ? ( | ||
| <Loading /> | ||
| ) : ( | ||
| <div className="grid gap-6 md:grid-cols-2"> | ||
| {users.map((user) => ( | ||
| <div | ||
| key={user._id} | ||
| className="p-5 bg-base-200 rounded-lg shadow-sm hover:shadow-md transition duration-300 flex flex-col justify-between" | ||
| > | ||
| <div> | ||
| <p className="text-lg font-medium mb-1"> | ||
| {user.firstName} {user.lastName} | ||
| </p> | ||
|
|
||
| <p className="text-sm text-base-content/70 mb-1"> | ||
| <strong>Username:</strong> {user.username} | ||
| </p> | ||
|
|
||
| <Link | ||
| to={`/profile/${user.username}`} | ||
| className="inline-block mt-2 text-sm text-primary hover:underline" | ||
| > | ||
| View Profile | ||
| </Link> | ||
| </div> | ||
|
|
||
| <button | ||
| onClick={() => confirmDelete(user)} | ||
| className="mt-4 text-sm text-red-600 hover:text-red-800 self-start" | ||
| > | ||
| Delete User | ||
| </button> | ||
| </div> | ||
| ))} | ||
| </div> | ||
| )} | ||
|
|
||
| {/* Modal */} | ||
| {showModal && userToDelete && ( | ||
| <div className="fixed inset-0 bg-black bg-opacity-50 flex items-center justify-center z-50"> | ||
| <div className="bg-base-100 p-6 rounded-lg shadow-xl w-80"> | ||
| <h3 className="text-lg font-semibold text-center text-red-600 mb-4">Confirm Deletion</h3> | ||
| <p className="text-sm text-center mb-4"> | ||
| Are you sure you want to delete <strong>{userToDelete.username}</strong>? | ||
| This action cannot be undone. | ||
| </p> | ||
| <div className="flex justify-between mt-6"> | ||
| <button | ||
| onClick={() => setShowModal(false)} | ||
| className="px-4 py-2 bg-gray-200 rounded hover:bg-gray-300" | ||
| > | ||
| Cancel | ||
| </button> | ||
|
Comment on lines
+136
to
+299
Owner
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Button is hard to read in dark theme |
||
| <button | ||
| onClick={handleDeleteConfirmed} | ||
| className="px-4 py-2 bg-red-600 text-white rounded hover:bg-red-700" | ||
| > | ||
| Delete | ||
| </button> | ||
| </div> | ||
| </div> | ||
| </div> | ||
| )} | ||
| </div> | ||
| ); | ||
| }; | ||
|
|
||
| export default Adminpage; | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can be removed as mentioned earlier