feat: set role shortcut in context menu

This commit is contained in:
Tristan Yang
2023-06-09 16:38:26 +08:00
parent a81c087163
commit ffe4d83c6c
5 changed files with 48 additions and 10 deletions
+9 -1
View File
@@ -29,7 +29,10 @@ const Profile: FC<Props> = ({ uid, type = "embed", cid }) => {
removeFromChannel,
canRemoveFromChannel,
canRemove,
removeUser
removeUser,
isAdmin,
canUpdateRole,
updateRole
} = useUserOperation({ uid, cid });
const { data } = useAppSelector((store) => {
@@ -99,6 +102,11 @@ const Profile: FC<Props> = ({ uid, type = "embed", cid }) => {
{t("copy_email")}
</li>
)}
{canUpdateRole && (
<li className="item" onClick={updateRole}>
{isAdmin ? t("set_normal") : t("set_admin")}
</li>
)}
{canRemoveFromChannel && (
<li className="item danger" onClick={removeFromChannel.bind(null, uid)}>
{t("remove_from_channel")}
+8 -1
View File
@@ -28,7 +28,10 @@ const UserContextMenu: FC<Props> = ({ enable = false, uid, cid, visible, hide, c
canBlock,
canRemoveFromChannel,
removeFromChannel,
removeUser
removeUser,
isAdmin,
canUpdateRole,
updateRole
} = useUserOperation({
uid,
cid
@@ -56,6 +59,10 @@ const UserContextMenu: FC<Props> = ({ enable = false, uid, cid, visible, hide, c
title: t("copy_email"),
handler: copyEmail
},
canUpdateRole && {
title: isAdmin ? t("set_normal") : t("set_admin"),
handler: updateRole
},
canRemoveFromChannel && {
danger: true,
title: t("remove_from_channel"),
+20 -1
View File
@@ -7,7 +7,11 @@ import { useMatch, useNavigate } from "react-router-dom";
import { hideAll } from "tippy.js";
import { useRemoveMembersMutation } from "@/app/services/channel";
import { useLazyDeleteUserQuery, useUpdateContactStatusMutation } from "@/app/services/user";
import {
useLazyDeleteUserQuery,
useUpdateContactStatusMutation,
useUpdateUserMutation
} from "@/app/services/user";
import { updateDMVisibleAside } from "@/app/slices/footprint";
import { updateCallInfo } from "@/app/slices/voice";
import { useAppSelector } from "@/app/store";
@@ -30,6 +34,7 @@ const useUserOperation = ({ uid, cid }: IProps) => {
const [passedUid, setPassedUid] = useState<number | undefined>(undefined);
const isUserDetailPath = useMatch(`/users/${uid}`);
const [updateContactStatus] = useUpdateContactStatusMutation();
const [updateUser, { isSuccess: updateUserSuccess }] = useUpdateUserMutation();
const [removeUser, { isSuccess: removeUserSuccess }] = useLazyDeleteUserQuery();
const [removeInChannel, { isSuccess: removeSuccess }] = useRemoveMembersMutation();
const navigateTo = useNavigate();
@@ -46,6 +51,12 @@ const useUserOperation = ({ uid, cid }: IProps) => {
setPassedUid(uid ?? loginUser?.uid);
}, [uid, loginUser]);
useEffect(() => {
if (updateUserSuccess) {
toast.success(ct("tip.update"));
}
}, [updateUserSuccess]);
useEffect(() => {
if (removeSuccess || removeUserSuccess) {
toast.success(ct("tip.delete"));
@@ -99,6 +110,11 @@ const useUserOperation = ({ uid, cid }: IProps) => {
const startChat = () => {
navigateTo(`/chat/dm/${uid}`);
};
const updateRole = () => {
if (uid) {
updateUser({ id: uid, is_admin: !user?.is_admin });
}
};
const removeFromContact = () => {
if (uid) {
updateContactStatus({ target_uid: uid, action: "remove" });
@@ -126,6 +142,9 @@ const useUserOperation = ({ uid, cid }: IProps) => {
const canRemoveFromContact: boolean = loginUid != uid;
const canInviteChannel = !!cid && (loginUser?.is_admin || channel?.owner == loginUser?.uid);
return {
isAdmin: !!user?.is_admin,
updateRole,
canUpdateRole: isAdmin && loginUid != uid && uid != 1,
removeFromContact,
canBlock,
canRemoveFromContact,