Browse Source

FIX | Send notifications: correct set allUsersSelected flag when searchString is not empty (#21)

BANK-1166: Доробити функціонал "Відправка сповіщення обраній групі осіб (або одній особі)"
Reviewed-on: #21
Co-authored-by: Oksana Stepanenko <oksana.stepanenko@jetup.team>
Co-committed-by: Oksana Stepanenko <oksana.stepanenko@jetup.team>
pull/23/head
Oksana Stepanenko 8 months ago committed by Vitalik Yatsenko
parent
commit
913febf6df
  1. 8
      src/containers/User/hooks/use-fetch-chat-users.hook.ts
  2. 8
      src/widgets/send-notifications/hooks/use-send-notifications.hook.ts
  3. 28
      src/widgets/send-notifications/send-notifications.widget.tsx

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,
};
};

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