Skip to content

Commit

Permalink
fixed send notif delegate issue (#1822)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
mishramonalisha76 and abhishek-01k authored Aug 22, 2024
1 parent 51e30be commit a77f285
Show file tree
Hide file tree
Showing 5 changed files with 47 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/modules/sendNotification/SendNotification.form.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
19 changes: 12 additions & 7 deletions src/modules/sendNotification/SendNotification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down
17 changes: 10 additions & 7 deletions src/modules/sendNotification/SendNotification.utils.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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: (
<Box
width="24px"
Expand Down Expand Up @@ -66,7 +70,6 @@ export const getRecipients = (type: NotificationType, recipient: string) => {
};

export const getChannelAddress = (channelOption: ChannelDetails, chainId: string, onCoreNetwork: boolean) => {
console.debug(channelOption);
if (onCoreNetwork) return convertAddressToAddrCaip(channelOption.channel, parseInt(chainId));
else {
const aliasAddress =
Expand Down
21 changes: 11 additions & 10 deletions src/modules/sendNotification/components/SendNotificationForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,28 +34,29 @@ type SendNotificationFormProps = {
};
const SendNotificationForm: FC<SendNotificationFormProps> = ({ channelDetails }) => {
const [subsetRecipients, setSubsetRecipients] = useState<Array<string>>([]);
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<NotficationValue>({
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;

Expand Down Expand Up @@ -169,7 +170,7 @@ const SendNotificationForm: FC<SendNotificationFormProps> = ({ channelDetails })
/>
</Box>
<Select
options={alaisChainOptions}
options={aliasChainOptions}
value={formik.values.chainId}
onSelect={(value) => {
formik.setFieldValue('chainId', value);
Expand Down
22 changes: 12 additions & 10 deletions src/structure/Navigation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -187,18 +187,20 @@ function Navigation() {
}, [canSend, channelDetails, navigationSetup, processingState, account]);

useEffect(() => {
/**
* If its a delegate
* If the channel Details is present on the core network
*/
if (delegatees && delegatees.length > 0) {
dispatch(setCanSend(SEND_NOTIFICATION_STATES.SEND));
if (!onActiveNetwork) {
if (delegatees && delegatees.length > 0) {
dispatch(setCanSend(SEND_NOTIFICATION_STATES.SEND));
} else {
dispatch(setCanSend(SEND_NOTIFICATION_STATES.HIDE));
}
}

if (onActiveNetwork && channelDetails && channelDetails?.name !== null) {
dispatch(setCanSend(SEND_NOTIFICATION_STATES.SEND));
} else {
dispatch(setCanSend(SEND_NOTIFICATION_STATES.HIDE));
if (onActiveNetwork) {
if ((channelDetails && channelDetails?.name !== null) || (delegatees && delegatees.length > 0)) {
dispatch(setCanSend(SEND_NOTIFICATION_STATES.SEND));
} else {
dispatch(setCanSend(SEND_NOTIFICATION_STATES.HIDE));
}
}
}, [channelDetails, delegatees, canSend, account, onActiveNetwork]);

Expand Down

0 comments on commit a77f285

Please sign in to comment.