From a77f2854f8ccb5225a3ec768be9e2fc81116af4d Mon Sep 17 00:00:00 2001 From: Monalisha Mishra <42746736+mishramonalisha76@users.noreply.github.com> Date: Thu, 22 Aug 2024 14:07:54 +0530 Subject: [PATCH] fixed send notif delegate issue (#1822) * fixed send notif delegate issue * Fixed the send notification button on the navigation for delegatee list * added fixes for alias * added fixes for alias * fixed review comment --------- Co-authored-by: abhishek-01k --- .../sendNotification/SendNotification.form.ts | 4 ++-- .../sendNotification/SendNotification.tsx | 19 ++++++++++------ .../SendNotification.utils.tsx | 17 ++++++++------ .../components/SendNotificationForm.tsx | 21 +++++++++--------- src/structure/Navigation.tsx | 22 ++++++++++--------- 5 files changed, 47 insertions(+), 36 deletions(-) diff --git a/src/modules/sendNotification/SendNotification.form.ts b/src/modules/sendNotification/SendNotification.form.ts index ae55a25493..f40857a0dd 100644 --- a/src/modules/sendNotification/SendNotification.form.ts +++ b/src/modules/sendNotification/SendNotification.form.ts @@ -29,10 +29,10 @@ export const getValidationSchema = (isSubsetRecipientPresent: boolean) => { }); }; -export const getFormInitialValues = (delegateesOptions: SelectOption[]) => { +export const getFormInitialValues = (delegateesOptions: SelectOption[], aliasChainOptions: SelectOption[]) => { return { channelAddress: delegateesOptions[0]?.value || '', - chainId: appConfig.coreContractChain.toString(), + chainId: aliasChainOptions[0]?.value || appConfig.coreContractChain.toString(), type: 'BROADCAST' as NotificationType, recipient: '', titleChecked: false, diff --git a/src/modules/sendNotification/SendNotification.tsx b/src/modules/sendNotification/SendNotification.tsx index e40d6e7c21..cc8f27c099 100644 --- a/src/modules/sendNotification/SendNotification.tsx +++ b/src/modules/sendNotification/SendNotification.tsx @@ -8,22 +8,27 @@ import { SendNotificationForm } from './components/SendNotificationForm'; import UnlockProfileWrapper, { UNLOCK_PROFILE_TYPE } from 'components/chat/unlockProfile/UnlockProfileWrapper'; import { UserStoreType } from 'types'; -import { useGetChannelDetails } from 'queries'; +import { useGetAliasInfo, useGetChannelDetails } from 'queries'; import { useAccount } from 'hooks'; import { useNavigate } from 'react-router-dom'; - -//add formik -//add conditon for /send url +import { aliasChainIdToChainName } from 'helpers/UtilityHelper'; +import { ALIAS_CHAIN } from '@pushprotocol/restapi/src/lib/config'; const SendNotification: FC = () => { const [isAuthModalVisible, setIsAuthModalVisible] = useState(true); const { userPushSDKInstance } = useSelector((state: UserStoreType) => state.user); - const { account } = useAccount(); - const { data: channelDetails } = useGetChannelDetails(account); + const { account, chainId } = useAccount(); + + const { data: alaisDetails } = useGetAliasInfo({ + alias: account, + aliasChain: aliasChainIdToChainName[chainId as keyof typeof aliasChainIdToChainName] as ALIAS_CHAIN, + }); + const { data: channelDetails } = useGetChannelDetails(alaisDetails?.channel || account); + const { delegatees } = useSelector((state: any) => state.admin); const nagivate = useNavigate(); useEffect(() => { - if (!channelDetails) nagivate('/channels'); + if (!channelDetails && !delegatees?.length && !alaisDetails) nagivate('/channels'); }, [channelDetails]); return ( diff --git a/src/modules/sendNotification/SendNotification.utils.tsx b/src/modules/sendNotification/SendNotification.utils.tsx index aff6ae980f..b50c6b088e 100644 --- a/src/modules/sendNotification/SendNotification.utils.tsx +++ b/src/modules/sendNotification/SendNotification.utils.tsx @@ -2,23 +2,27 @@ import { NotificationType } from '@pushprotocol/restapi'; import { Box } from 'blocks'; import { appConfig } from 'config'; -import { convertAddressToAddrCaip } from 'helpers/CaipHelper'; +import { convertAddrCaipToAddress, convertAddressToAddrCaip } from 'helpers/CaipHelper'; import { ChannelSetting } from 'helpers/channel/types'; import { ChannelDetails } from 'queries'; -export const getChannelChainList = (channelDetails: ChannelDetails) => { +export const getChannelChainList = (channelDetails: ChannelDetails, account: string) => { const aliases = channelDetails?.aliases - ?.filter((alias) => alias.is_alias_verified) + ?.filter((alias) => alias.is_alias_verified && convertAddrCaipToAddress(alias.alias_address) === account) ?.map((alias) => parseInt(alias.alias_blockchain_id)) || []; return [...aliases, appConfig.coreContractChain]; }; -export const getChannelDelegatesOptions = (delegatees: [ChannelDetails]) => { - if (delegatees && delegatees.length) { - return delegatees?.map((channel) => ({ +export const getChannelDelegatesOptions = ( + delegatees: [ChannelDetails], + channelDetails: ChannelDetails | undefined +) => { + const delegateesArray = [...(delegatees || []), ...(channelDetails ? [channelDetails] : [])]; + if (delegateesArray && delegateesArray.length) { + return delegateesArray?.map((channel) => ({ icon: ( { }; export const getChannelAddress = (channelOption: ChannelDetails, chainId: string, onCoreNetwork: boolean) => { - console.debug(channelOption); if (onCoreNetwork) return convertAddressToAddrCaip(channelOption.channel, parseInt(chainId)); else { const aliasAddress = diff --git a/src/modules/sendNotification/components/SendNotificationForm.tsx b/src/modules/sendNotification/components/SendNotificationForm.tsx index 0c4140d757..e95bc54f6a 100644 --- a/src/modules/sendNotification/components/SendNotificationForm.tsx +++ b/src/modules/sendNotification/components/SendNotificationForm.tsx @@ -34,28 +34,29 @@ type SendNotificationFormProps = { }; const SendNotificationForm: FC = ({ channelDetails }) => { const [subsetRecipients, setSubsetRecipients] = useState>([]); - const { chainId, switchChain } = useAccount(); + const { chainId, switchChain, account } = useAccount(); const { mutate: sendNotification, isPending } = useSendNotification(); const toast = useToast(); const { userPushSDKInstance } = useSelector((state: UserStoreType) => state.user); /** replace the delegatees with high level sdk function once it is available */ const { delegatees } = useSelector((state: any) => state.admin); const onCoreNetwork = CORE_CHAIN_ID === chainId; - const delegateesOptions = getChannelDelegatesOptions( - delegatees?.length ? delegatees : channelDetails ? [channelDetails] : [] - ); + + const delegateesOptions = getChannelDelegatesOptions(delegatees, channelDetails); + + const selectedChannel = + delegatees?.find((delegatee: ChannelDetails) => delegatee.channel === delegateesOptions[0]?.value) || + channelDetails; + + const aliasChainOptions = getSelectChains(getChannelChainList(selectedChannel, account)); const formik = useFormik({ - initialValues: getFormInitialValues(delegateesOptions), + initialValues: getFormInitialValues(delegateesOptions, aliasChainOptions), validationSchema: getValidationSchema(!!subsetRecipients.length), onSubmit: (values) => { handleSendNotification(values); }, }); - const selectedChannel = - delegatees?.find((delegatee: ChannelDetails) => delegatee.channel === formik.values.channelAddress) || - channelDetails; - const alaisChainOptions = getSelectChains(getChannelChainList(selectedChannel)); const showPreview = formik.values.body || formik.values.title || formik.values.ctaLink || formik.values.mediaUrl; @@ -169,7 +170,7 @@ const SendNotificationForm: FC = ({ channelDetails }) />