-
Notifications
You must be signed in to change notification settings - Fork 1.4k
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
feat-#178: added admin UI to frontend #369
Merged
krishnaacharyaa
merged 6 commits into
krishnaacharyaa:main
from
devsharmagit:bugfix/178-add-admin-ui
Jun 18, 2024
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
8af331c
feat-#178: added admin UI to frontend
devsharmag99 e3177a0
feat-#178: react import bug fix
devsharmag99 5a19104
feat-#178: enum added
devsharmag99 52f6ae3
feat-#178: clean redis cache on edit and delete
devsharmag99 55ed4f3
feat-#178: manually prettier format in both frontend and backend
devsharmagit bf99c48
feat-#178: manually prettier format in both frontend and backend
devsharmagit 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
This file contains 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 |
---|---|---|
|
@@ -135,8 +135,11 @@ export const updatePostHandler = async (req, res) => { | |
if (!updatedPost) { | ||
return res.status(HTTP_STATUS.NOT_FOUND).json({ message: RESPONSE_MESSAGES.POSTS.NOT_FOUND }); | ||
} | ||
|
||
res.status(HTTP_STATUS.OK).json(updatedPost); | ||
// invalidate the redis cache | ||
await deleteDataFromCache(REDIS_KEYS.ALL_POSTS), | ||
await deleteDataFromCache(REDIS_KEYS.FEATURED_POSTS), | ||
await deleteDataFromCache(REDIS_KEYS.LATEST_POSTS), | ||
await res.status(HTTP_STATUS.OK).json(updatedPost); | ||
} catch (err) { | ||
res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({ message: err.message }); | ||
} | ||
|
@@ -152,7 +155,11 @@ export const deletePostByIdHandler = async (req, res) => { | |
} | ||
await User.findByIdAndUpdate(post.authorId, { $pull: { posts: req.params.id } }); | ||
|
||
res.status(HTTP_STATUS.OK).json({ message: RESPONSE_MESSAGES.POSTS.DELETED }); | ||
// invalidate the redis cache | ||
await deleteDataFromCache(REDIS_KEYS.ALL_POSTS), | ||
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. Same here |
||
await deleteDataFromCache(REDIS_KEYS.FEATURED_POSTS), | ||
await deleteDataFromCache(REDIS_KEYS.LATEST_POSTS), | ||
res.status(HTTP_STATUS.OK).json({ message: RESPONSE_MESSAGES.POSTS.DELETED }); | ||
} catch (err) { | ||
res.status(HTTP_STATUS.INTERNAL_SERVER_ERROR).json({ message: err.message }); | ||
} | ||
|
This file contains 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 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 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 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 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 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 |
---|---|---|
@@ -1,22 +1,88 @@ | ||
import axiosInstance from '@/helpers/axios-instance'; | ||
import { useEffect, useState } from 'react'; | ||
import { toast } from 'react-toastify'; | ||
import 'react-toastify/dist/ReactToastify.css'; | ||
|
||
enum role { | ||
admin = 'ADMIN', | ||
user = 'USER', | ||
} | ||
|
||
type User = { | ||
_id: string; | ||
fullName: string; | ||
role: role; | ||
email: string; | ||
}; | ||
|
||
const AdminUsers = () => { | ||
const [users, setUsers] = useState<User[]>([]); | ||
|
||
const fetchData = async () => { | ||
try { | ||
const response = await axiosInstance.get('/api/user'); | ||
setUsers(response?.data?.users); | ||
} catch (error) { | ||
toast.error('Something went wrong! Please try again!'); | ||
} | ||
}; | ||
|
||
const handleClick = async (userId: string, role: role) => { | ||
try { | ||
const response = await axiosInstance.patch('/api/user/' + userId, { role: role }); | ||
if (response.status === 200) { | ||
fetchData(); | ||
toast.success('User updated successfully!'); | ||
} | ||
} catch (error) { | ||
toast.error('Something went wrong! Please try again later.'); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
fetchData(); | ||
}, []); | ||
|
||
return ( | ||
<> | ||
<div className="w-full p-3 px-5 sm:p-12"> | ||
<h1 className="absolute left-16 top-3 text-2xl font-bold text-light-title dark:text-dark-title sm:static"> | ||
Users | ||
</h1> | ||
<div className="mt-2 sm:mt-12"> | ||
<div className="flex w-full flex-row items-center justify-between gap-5 rounded-lg border-b border-gray-300 bg-light px-3 py-4 shadow-md dark:border-gray-700 dark:bg-dark-card"> | ||
<div className="flex flex-col gap-[10px] "> | ||
<p className="text-base font-medium text-light-title dark:text-dark-title">Hemant</p> | ||
<p className="text-base font-medium text-light-description dark:text-dark-description"> | ||
[email protected] | ||
</p> | ||
</div> | ||
<button className="h-fit rounded-xl border border-black bg-black px-4 py-2 text-sm font-semibold text-white"> | ||
Admin | ||
</button> | ||
</div> | ||
{users?.map((user: User) => { | ||
return ( | ||
<div | ||
key={user?._id} | ||
className="mb-3 flex w-full flex-row items-center justify-between gap-5 rounded-lg border-b border-gray-300 bg-light px-3 py-4 shadow-md dark:border-gray-700 dark:bg-dark-card" | ||
> | ||
<div className="flex flex-col gap-[10px] "> | ||
<p className="text-base font-medium text-light-title dark:text-dark-title"> | ||
{user?.fullName} | ||
</p> | ||
<p className="text-base font-medium text-light-description dark:text-dark-description"> | ||
{user?.email} | ||
</p> | ||
</div> | ||
{user.role === role.admin && ( | ||
<button | ||
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. Use enum instead, kindly replace all the places this is used to use enum |
||
onClick={() => handleClick(user._id, role.user)} | ||
className="h-fit rounded-xl border border-black bg-black px-4 py-2 text-sm font-semibold text-white" | ||
> | ||
Admin | ||
</button> | ||
)} | ||
{user.role === role.user && ( | ||
<button | ||
onClick={() => handleClick(user._id, role.admin)} | ||
className="h-fit rounded-xl border border-black bg-transparent px-4 py-2 text-sm font-semibold text-white" | ||
> | ||
Admin | ||
</button> | ||
)} | ||
</div> | ||
); | ||
})} | ||
</div> | ||
</div> | ||
</> | ||
|
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.
It should ideally format, I'm confused, why isn't it working for you !!
we have a husky hook which actually formats the code,
Isn't it working for you?
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.
i guess its not working for me.
i will manually do "npm run format" in both frontend and backend and push the code
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.
Sure @devsharmagit