Browse Source

Merge branch 'stage' of gitlab.work-jetup.site:task-me/rws-web-app into stage

pull/25/head
Vitalik 8 months ago
parent
commit
88c9e45508
  1. 58
      src/containers/Chats/hooks/use-edit-group-chat.hook.ts
  2. 5
      src/containers/Chats/hooks/use-selected-messages.hook.tsx
  3. 8
      src/containers/User/hooks/use-fetch-chat-users.hook.ts
  4. 1
      src/shared/events/index.ts
  5. 8
      src/widgets/send-notifications/hooks/use-send-notifications.hook.ts
  6. 28
      src/widgets/send-notifications/send-notifications.widget.tsx

58
src/containers/Chats/hooks/use-edit-group-chat.hook.ts

@ -5,7 +5,7 @@ import { @@ -5,7 +5,7 @@ import {
IChatDetails,
IChatMember,
useEventsListener,
useSocketListener
useSocketListener,
} from "@/shared";
import { getProfile } from "@/store/account";
import { selectSelectedChatId } from "@/store/chats";
@ -15,6 +15,7 @@ import _ from "lodash"; @@ -15,6 +15,7 @@ import _ from "lodash";
import { useCallback, useEffect, useState } from "react";
import { useSelector } from "react-redux";
import { transformShrotUserToChatMember } from "../transforms";
import { appEvents } from "@/shared/events";
export const useEditGroupChat = (id?: number) => {
const [chatDetails, setChatDetails] = useState<IChatDetails>(null);
@ -47,11 +48,11 @@ export const useEditGroupChat = (id?: number) => { @@ -47,11 +48,11 @@ export const useEditGroupChat = (id?: number) => {
setMembers(members);
} else {
const admins = members?.filter(
it => it.role === ChatMemberRole.Admin && !it.isDeleted
(it) => it.role === ChatMemberRole.Admin && !it.isDeleted
);
const adminsIds = admins?.map(it => it.userId);
const adminsIds = admins?.map((it) => it.userId);
const membersWithoutAdmins = members?.filter(
it => !adminsIds?.includes(it.userId) && !it.isDeleted
(it) => !adminsIds?.includes(it.userId) && !it.isDeleted
);
const membersWithAdminsAtBeginning = admins?.concat(
@ -76,19 +77,16 @@ export const useEditGroupChat = (id?: number) => { @@ -76,19 +77,16 @@ export const useEditGroupChat = (id?: number) => {
const onAddUsersToChat = async () => {
try {
if (!_.isEmpty(membersIdsToAdd))
console.log("mmebers ids to add", membersIdsToAdd);
// const usersIds = selectUsers.map((user) => user.id);
await chatMembersService.addMember({
chatId,
usersIds: membersIdsToAdd
});
if (!_.isEmpty(membersIdsToAdd)) {
await chatMembersService.addMember({
chatId,
usersIds: membersIdsToAdd,
});
appEvents.emit("onAddMember", {});
fetchDetails();
}
simpleDispatch(new UnselectAllUsers());
fetchDetails();
} catch (error) {
console.log(error);
}
@ -98,13 +96,13 @@ export const useEditGroupChat = (id?: number) => { @@ -98,13 +96,13 @@ export const useEditGroupChat = (id?: number) => {
memberId: number;
role: ChatMemberRole;
}) => {
const member = _.find(chatMembers, member => member.id === data.memberId);
const member = _.find(chatMembers, (member) => member.id === data.memberId);
if (member) {
const changeMember = (member: IChatMember) => {
if (member.id === data.memberId)
return {
...member,
role: data.role
role: data.role,
};
return member;
@ -114,7 +112,7 @@ export const useEditGroupChat = (id?: number) => { @@ -114,7 +112,7 @@ export const useEditGroupChat = (id?: number) => {
setMembers(
_.sortBy(
changedMembers,
member => member.role === ChatMemberRole.Member
(member) => member.role === ChatMemberRole.Member
)
);
@ -124,38 +122,40 @@ export const useEditGroupChat = (id?: number) => { @@ -124,38 +122,40 @@ export const useEditGroupChat = (id?: number) => {
};
const onDeleteMember = (data: { memberId: number }) => {
const member = _.find(chatMembers, member => member.id === data.memberId);
const member = _.find(chatMembers, (member) => member.id === data.memberId);
if (member) {
const changedMembers = chatMembers.filter(
member => member.id !== data.memberId
(member) => member.id !== data.memberId
);
setMembers(changedMembers);
}
const allMembers = chatDetails.chatMembers.filter(
member => member.id !== data.memberId
(member) => member.id !== data.memberId
);
setChatDetails({ ...chatDetails, chatMembers: allMembers });
};
useEffect(() => {
const activeMembers = chatDetails?.chatMembers?.filter(it => !it.isDeleted);
const activeMembersIds = activeMembers?.map(it => it.userId);
const activeMembers = chatDetails?.chatMembers?.filter(
(it) => !it.isDeleted
);
const activeMembersIds = activeMembers?.map((it) => it.userId);
const selectedMembersToAdd = selectedUsers.map(it =>
const selectedMembersToAdd = selectedUsers.map((it) =>
transformShrotUserToChatMember(chatId, it)
);
const chatMembersToAddIds = selectedMembersToAdd?.map(it => it.userId);
const chatMembersToAddIds = selectedMembersToAdd?.map((it) => it.userId);
const membersIdsToAdd = chatMembersToAddIds?.filter(
id => !activeMembersIds?.includes(id)
(id) => !activeMembersIds?.includes(id)
);
setMembersIdsToAdd(membersIdsToAdd);
}, [chatDetails, chatMembers]);
const setMemberRole = (members: IChatMember[]) => {
const member = _.find(members, member => member?.userId === account.id);
const member = _.find(members, (member) => member?.userId === account.id);
setRole(member?.role ? member?.role : ChatMemberRole.Member);
};
@ -186,7 +186,7 @@ export const useEditGroupChat = (id?: number) => { @@ -186,7 +186,7 @@ export const useEditGroupChat = (id?: number) => {
useEventsListener("onChangeMemberRole", onChangeMemberRole, [chatMembers]);
useEventsListener("onDeleteMember", onDeleteMember, [
chatMembers,
selectedUsers
selectedUsers,
]);
useSocketListener("chat/edit-chat", onEditChat, [chatDetails]);
@ -199,6 +199,6 @@ export const useEditGroupChat = (id?: number) => { @@ -199,6 +199,6 @@ export const useEditGroupChat = (id?: number) => {
role,
previewImg,
setPreviewImg,
onAddUsersToChat
onAddUsersToChat,
};
};

5
src/containers/Chats/hooks/use-selected-messages.hook.tsx

@ -1,7 +1,7 @@ @@ -1,7 +1,7 @@
import _ from "lodash";
import { IChatMessage } from "../plugins/interfaces";
import { create } from "zustand";
import { ChatMemberRole, MessageType } from "@/shared";
import { ChatMemberRole, MessageType, useEventsListener } from "@/shared";
import { ChatMessageActionEnum, ChatViewModeEnum } from "../enums";
import { useChatViewModeState } from "./use-chat-view-mode-state.hook";
import { appEvents } from "@/shared/events";
@ -159,6 +159,9 @@ export const useChatSelectedMessages = ({ infoMessageApi }: IProps) => { @@ -159,6 +159,9 @@ export const useChatSelectedMessages = ({ infoMessageApi }: IProps) => {
appEvents.emit("openSelectedMessagesMenuOptions", { items: options });
};
useEventsListener("onDeleteMember", afterAction, []);
useEventsListener("onAddMember", afterAction, []);
return {
openSelectedMessagesMenu,
};

8
src/containers/User/hooks/use-fetch-chat-users.hook.ts

@ -1,10 +1,12 @@ @@ -1,10 +1,12 @@
import { chatsApi } from "@/api";
import { transformToShortUsers } from "@/api/chats/transform";
import { IShortUser, usePaginationList } from "@/shared";
import _ from "lodash";
import { useEffect, useState } from "react";
export const useFetchChatUsers = () => {
const [searchString, setSearchVal] = useState<string>("");
const [totalUsersCount, setTotalUsersCount] = useState<number>(0);
const {
items,
@ -28,6 +30,11 @@ export const useFetchChatUsers = () => { @@ -28,6 +30,11 @@ export const useFetchChatUsers = () => {
if (searchString !== null) setLoadParams({ searchString });
}, [searchString]);
useEffect(() => {
if (!_.isEmpty(items) && loadParams?.count && !loadParams?.searchString)
setTotalUsersCount(loadParams?.count);
}, [items, loadParams?.searchString, loadParams?.count]);
return {
items: items as IShortUser[],
isLoading,
@ -35,5 +42,6 @@ export const useFetchChatUsers = () => { @@ -35,5 +42,6 @@ export const useFetchChatUsers = () => {
loadMore,
setSearchVal,
loadParams,
totalUsersCount,
};
};

1
src/shared/events/index.ts

@ -21,6 +21,7 @@ export type AppEvents = { @@ -21,6 +21,7 @@ export type AppEvents = {
onChangeMemberRole: { memberId: number; role: ChatMemberRole };
onDeleteMember: { memberId: number };
onAddMember: {};
onFirstTaskComment: { taskId: number };
onFirstTaskDocument: { taskId: number };
onDeleteAllTaskDocs: { taskId: number };

8
src/widgets/send-notifications/hooks/use-send-notifications.hook.ts

@ -62,6 +62,12 @@ export const useSendNotifications = (onSuccess?: () => void) => { @@ -62,6 +62,12 @@ export const useSendNotifications = (onSuccess?: () => void) => {
}
};
const handleSetAllSelected = (selected: boolean) => {
if (selected) form.setError("users", null);
setAllSelected(selected);
};
return {
form,
submit: form.handleSubmit(submit),
@ -71,6 +77,6 @@ export const useSendNotifications = (onSuccess?: () => void) => { @@ -71,6 +77,6 @@ export const useSendNotifications = (onSuccess?: () => void) => {
selectedUsers,
isLoading,
allSelected,
setAllSelected,
setAllSelected: handleSetAllSelected,
};
};

28
src/widgets/send-notifications/send-notifications.widget.tsx

@ -35,6 +35,7 @@ export const SendNotificationsWidget: FC<Props> = ({ isOpen, onClose }) => { @@ -35,6 +35,7 @@ export const SendNotificationsWidget: FC<Props> = ({ isOpen, onClose }) => {
setSearchVal,
loadMore,
loadParams,
totalUsersCount,
} = useFetchChatUsers();
const toggleSelectAll = () => {
@ -59,8 +60,15 @@ export const SendNotificationsWidget: FC<Props> = ({ isOpen, onClose }) => { @@ -59,8 +60,15 @@ export const SendNotificationsWidget: FC<Props> = ({ isOpen, onClose }) => {
}, [allSelected, items]);
useEffect(() => {
if (selectedUsers.length === loadParams?.count) setAllSelected(true);
}, [loadParams?.count, selectedUsers]);
if (totalUsersCount > 0 && selectedUsers.length === totalUsersCount)
setAllSelected(true);
}, [selectedUsers, totalUsersCount]);
const handleUnselectUser = (userId: number) => {
unselectUser(userId);
if (allSelected) setAllSelected(false);
};
return (
<Modal show={isOpen} toggle={() => onClose()} title="Нове пуш-сповіщення">
@ -108,11 +116,15 @@ export const SendNotificationsWidget: FC<Props> = ({ isOpen, onClose }) => { @@ -108,11 +116,15 @@ export const SendNotificationsWidget: FC<Props> = ({ isOpen, onClose }) => {
name="users"
control={form.control}
rules={{
required: "Потрібно вибрати принаймні 1 користувача",
minLength: {
message: "Потрібно вибрати принаймні 1 користувача",
value: 1,
},
required: allSelected
? null
: "Потрібно вибрати принаймні 1 користувача",
minLength: allSelected
? null
: {
message: "Потрібно вибрати принаймні 1 користувача",
value: 1,
},
}}
render={({ field: { value }, fieldState }) => (
<>
@ -120,7 +132,7 @@ export const SendNotificationsWidget: FC<Props> = ({ isOpen, onClose }) => { @@ -120,7 +132,7 @@ export const SendNotificationsWidget: FC<Props> = ({ isOpen, onClose }) => {
items={items}
selectedUsers={value}
selectOne={selectUser}
unSelectOne={unselectUser}
unSelectOne={handleUnselectUser}
excludeIds={[]}
loadMore={loadMore}
count={loadParams.count}

Loading…
Cancel
Save