feat: list blocked users

This commit is contained in:
Zamitto 2024-08-14 19:45:48 -03:00
parent 7e6b9ca825
commit fbe3c1973a
7 changed files with 119 additions and 3 deletions

View File

@ -43,6 +43,7 @@ import "./auth/sign-out";
import "./auth/open-auth-window";
import "./auth/get-session-hash";
import "./user/get-user";
import "./user/get-user-blocks";
import "./user/block-user";
import "./user/unblock-user";
import "./user/get-user-friends";

View File

@ -0,0 +1,13 @@
import { registerEvent } from "../register-event";
import { HydraApi } from "@main/services";
import { UserBlocks } from "@types";
export const getUserBlocks = async (
_event: Electron.IpcMainInvokeEvent,
take: number,
skip: number
): Promise<UserBlocks> => {
return HydraApi.get(`/profile/blocks`, { take, skip });
};
registerEvent("getUserBlocks", getUserBlocks);

View File

@ -152,6 +152,8 @@ contextBridge.exposeInMainWorld("electron", {
unblockUser: (userId: string) => ipcRenderer.invoke("unblockUser", userId),
getUserFriends: (userId: string, take: number, skip: number) =>
ipcRenderer.invoke("getUserFriends", userId, take, skip),
getUserBlocks: (take: number, skip: number) =>
ipcRenderer.invoke("getUserBlocks", take, skip),
/* Auth */
signOut: () => ipcRenderer.invoke("signOut"),

View File

@ -17,6 +17,7 @@ import type {
FriendRequest,
FriendRequestAction,
UserFriends,
UserBlocks,
} from "@types";
import type { DiskSpace } from "check-disk-space";
@ -135,6 +136,7 @@ declare global {
take: number,
skip: number
) => Promise<UserFriends>;
getUserBlocks: (take: number, skip: number) => Promise<UserBlocks>;
/* Profile */
getMe: () => Promise<UserProfile | null>;

View File

@ -0,0 +1,92 @@
import { SPACING_UNIT } from "@renderer/theme.css";
import { UserFriend } from "@types";
import { useEffect, useState } from "react";
import { useNavigate } from "react-router-dom";
import { useToast, useUserDetails } from "@renderer/hooks";
import { useTranslation } from "react-i18next";
import { UserFriendItem } from "@renderer/pages/shared-modals/user-friend-modal/user-friend-item";
export interface UserEditProfileBlockListProps {
closeModal: () => void;
}
const pageSize = 12;
export const UserEditProfileBlockList = ({
closeModal,
}: UserEditProfileBlockListProps) => {
const { t } = useTranslation("user_profile");
const { showErrorToast } = useToast();
const navigate = useNavigate();
const [page, setPage] = useState(0);
const [maxPage, setMaxPage] = useState(0);
const [blocks, setBlocks] = useState<UserFriend[]>([]);
const { unblockUser } = useUserDetails();
const loadNextPage = () => {
if (page > maxPage) return;
window.electron
.getUserBlocks(pageSize, page * pageSize)
.then((newPage) => {
if (page === 0) {
setMaxPage(newPage.totalBlocks / pageSize);
}
setBlocks([...blocks, ...newPage.blocks]);
setPage(page + 1);
})
.catch(() => {});
};
const reloadList = () => {
setPage(0);
setMaxPage(0);
setBlocks([]);
loadNextPage();
};
useEffect(() => {
reloadList();
}, []);
const handleClickBlocked = (userId: string) => {
closeModal();
navigate(`/user/${userId}`);
};
const handleUnblock = (userId: string) => {
unblockUser(userId)
.then(() => {
reloadList();
})
.catch(() => {
showErrorToast(t("try_again"));
});
};
return (
<div
style={{
display: "flex",
flexDirection: "column",
gap: `${SPACING_UNIT * 2}px`,
}}
>
{blocks.map((friend) => {
return (
<UserFriendItem
userId={friend.id}
displayName={friend.displayName}
profileImageUrl={friend.profileImageUrl}
onClickItem={handleClickBlocked}
onClickUndoFriendship={handleUnblock}
type={"ACCEPTED"}
key={friend.id}
/>
);
})}
</div>
);
};

View File

@ -4,8 +4,9 @@ import { SPACING_UNIT } from "@renderer/theme.css";
import { useState } from "react";
import { useTranslation } from "react-i18next";
import { UserEditProfile } from "./user-edit-profile";
import { UserEditProfileBlockList } from "./user-block-list";
export interface UserEditProfileModalProps {
export interface UserProfileSettingsModalProps {
userProfile: UserProfile;
visible: boolean;
onClose: () => void;
@ -17,7 +18,7 @@ export const UserProfileSettingsModal = ({
visible,
onClose,
updateUserProfile,
}: UserEditProfileModalProps) => {
}: UserProfileSettingsModalProps) => {
const { t } = useTranslation("user_profile");
const tabs = [t("edit_profile"), t("blocked_users")];
@ -35,7 +36,7 @@ export const UserProfileSettingsModal = ({
}
if (currentTabIndex == 1) {
return <></>;
return <UserEditProfileBlockList closeModal={onClose} />;
}
return <></>;

View File

@ -282,6 +282,11 @@ export interface UserFriends {
friends: UserFriend[];
}
export interface UserBlocks {
totalBlocks: number;
blocks: UserFriend[];
}
export interface FriendRequest {
id: string;
displayName: string;