Skip to content

Commit

Permalink
fixed settings issue
Browse files Browse the repository at this point in the history
  • Loading branch information
mishramonalisha76 committed Apr 12, 2024
1 parent 4931d9b commit 9860137
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 108 deletions.
156 changes: 55 additions & 101 deletions src/components/dropdowns/OptinNotifSettingDropdown.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
// React + Web3 Essentials
import React, { useContext, useMemo, useState } from 'react';
import * as PushAPI from '@pushprotocol/restapi';
import { ethers } from 'ethers';

// External Packages
import Switch from 'react-switch';
Expand Down Expand Up @@ -71,7 +69,7 @@ const OptinNotifSettingDropdownContainer: React.FC<OptinNotifSettingDropdownCont
}
setModifiedSettings(updatedSettings);
};

console.log('channel settings', modifiedSettings);
return (
<DropdownOuterContainer>
{modifiedSettings.map((setting, index) => (
Expand Down Expand Up @@ -114,7 +112,7 @@ const OptinNotifSettingDropdownContainer: React.FC<OptinNotifSettingDropdownCont
val={setting.default}
max={setting.upperLimit}
min={setting.lowerLimit}
step={setting.ticker || 1}
step={setting.ticker ?? 1}
defaultVal={setting.default}
onChange={({ x }) => handleSliderChange(index, x)}
/>
Expand All @@ -135,7 +133,7 @@ const OptinNotifSettingDropdownContainer: React.FC<OptinNotifSettingDropdownCont
endVal={setting.default.upper}
max={setting.upperLimit}
min={setting.lowerLimit}
step={setting.ticker || 1}
step={setting.ticker ?? 1}
defaultStartVal={setting.default.lower}
defaultEndVal={setting.default.upper}
onChange={({ startVal, endVal }) => handleSliderChange(index, { lower: startVal, upper: endVal })}
Expand Down Expand Up @@ -174,14 +172,14 @@ const OptinNotifSettingDropdownContainer: React.FC<OptinNotifSettingDropdownCont
const OptinNotifSettingDropdown: React.FC<OptinNotifSettingDropdownProps> = (options) => {
const { children, channelDetail, setLoading, onSuccessOptin } = options;

const { chainId, provider, account, wallet } = useAccount();
const { chainId } = useAccount();
const { userPushSDKInstance } = useSelector((state: any) => {
return state.user;
});
const [isOpen, setIsOpen] = useState(false);
const dispatch = useDispatch();

const { handleConnectWallet, connectWallet } = useContext(AppContext);
const { handleConnectWallet } = useContext(AppContext);

const onCoreNetwork = chainId === appConfig.coreContractChain;

Expand Down Expand Up @@ -213,21 +211,13 @@ const OptinNotifSettingDropdown: React.FC<OptinNotifSettingDropdownProps> = (opt
setLoadingFunc(true);

let userPushInstance = userPushSDKInstance;
// if (!userPushInstance.signer) {
// userPushInstance = await handleConnectWallet();
// if (!userPushInstance) {
// setLoadingFunc(false);
// return;
// }
// }

let walletAddress = account;
let web3Provider = provider;

if (!(wallet?.accounts?.length > 0)) {
const connectedWallet = await connectWallet();
walletAddress = connectedWallet.accounts[0].address;
web3Provider = new ethers.providers.Web3Provider(connectedWallet.provider, 'any');

if (!userPushInstance.signer) {
userPushInstance = await handleConnectWallet();
if (!userPushInstance) {
setLoadingFunc(false);
return;
}
}

try {
Expand All @@ -238,85 +228,49 @@ const OptinNotifSettingDropdown: React.FC<OptinNotifSettingDropdownProps> = (opt

subscribeToast.showLoaderToast({ loaderMessage: 'Waiting for Confirmation...' });

const _signer = await web3Provider?.getSigner(walletAddress);

await PushAPI.channels.subscribe({
signer: _signer,
channelAddress: convertAddressToAddrCaip(channelAddress, chainId), // channel address in CAIP
userAddress: convertAddressToAddrCaip(walletAddress, chainId), // user address in CAIP
onSuccess: () => {
dispatch(updateSubscriptionStatus({ channelAddress, status: true }));
dispatch(
updateUserSetting({
channelAddress,
settings: userSettingsFromDefaultChannelSetting({ channelSetting: channelSettings }),
})
);

subscribeToast.showMessageToast({
toastTitle: 'Success',
toastMessage: 'Successfully opted into channel !',
toastType: 'SUCCESS',
getToastIcon: (size) => (
<MdCheckCircle
size={size}
color="green"
/>
),
});
},
onError: () => {
console.error('opt in error');
subscribeToast.showMessageToast({
toastTitle: 'Error',
toastMessage: `There was an error opting into channel`,
toastType: 'ERROR',
getToastIcon: (size) => (
<MdError
size={size}
color="red"
/>
),
});
},
env: appConfig.pushNodesEnv,
});

// await PUSHAPI.channels.subscribe(convertAddressToAddrCaip(channelAddress, chainId), {
// settings: notifChannelSettingFormatString({ settings: channelSettings }),
// // settings: [],
// onSuccess: () => {
// onSuccessOptin();
// dispatch(updateSubscriptionStatus({ channelAddress, status: true }));
// dispatch(updateUserSetting({ channelAddress, settings: userSettingsFromDefaultChannelSetting({ channelSetting: channelSettings }) }));

// subscribeToast.showMessageToast({
// toastTitle: 'Success',
// toastMessage: 'Successfully opted into channel !',
// toastType: 'SUCCESS',
// getToastIcon: (size) => (
// <MdCheckCircle
// size={size}
// color="green"
// />
// ),
// });
// },
// onError: () => {
// console.error('opt in error');
// subscribeToast.showMessageToast({
// toastTitle: 'Error',
// toastMessage: `There was an error opting into channel`,
// toastType: 'ERROR',
// getToastIcon: (size) => (
// <MdError
// size={size}
// color="red"
// />
// ),
// });
// },
// });
await userPushInstance.notification.subscribe(
convertAddressToAddrCaip(channelAddress, chainId), // channel address in CAIP
{
settings: notifChannelSettingFormatString({ settings: channelSettings }),
onSuccess: () => {
onSuccessOptin();
dispatch(updateSubscriptionStatus({ channelAddress, status: true }));
dispatch(
updateUserSetting({
channelAddress,
settings: userSettingsFromDefaultChannelSetting({ channelSetting: channelSettings }),
})
);

subscribeToast.showMessageToast({
toastTitle: 'Success',
toastMessage: 'Successfully opted into channel !',
toastType: 'SUCCESS',
getToastIcon: (size) => (
<MdCheckCircle
size={size}
color="green"
/>
),
});
},
onError: () => {
console.error('opt in error');
subscribeToast.showMessageToast({
toastTitle: 'Error',
toastMessage: `There was an error opting into channel`,
toastType: 'ERROR',
getToastIcon: (size) => (
<MdError
size={size}
color="red"
/>
),
});
},
env: appConfig.pushNodesEnv,
}
);
} catch (err) {
subscribeToast.showMessageToast({
toastTitle: 'Error',
Expand Down
14 changes: 7 additions & 7 deletions src/components/dropdowns/UpdateNotifSettingDropdown.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -113,13 +113,13 @@ const UpdateNotifSettingDropdownContainer: React.FC<UpdateNotifSettingDropdownCo
fontWeight="600"
alignSelf="flex-start"
>
{setting.user || setting.default}
{setting.user ?? setting.default}
</SpanV2>
<InputSlider
val={setting.user}
max={setting.upperLimit}
min={setting.lowerLimit}
step={setting.ticker || 1}
step={setting.ticker ?? 1}
defaultVal={setting.default}
onChange={({ x }) => handleSliderChange(index, x)}
/>
Expand All @@ -133,14 +133,14 @@ const UpdateNotifSettingDropdownContainer: React.FC<UpdateNotifSettingDropdownCo
fontWeight="600"
alignSelf="flex-start"
>
{setting.user.lower || setting.default.lower} - {setting.user.upper || setting.default.upper}
{setting.user.lower ?? setting.default.lower} - {setting.user.upper ?? setting.default.upper}
</SpanV2>
<RangeSlider
startVal={setting.user.lower || setting.default.lower}
endVal={setting.user.upper || setting.default.upper}
startVal={setting.user.lower ?? setting.default.lower}
endVal={setting.user.upper ?? setting.default.upper}
max={setting.upperLimit}
min={setting.lowerLimit}
step={setting.ticker || 1}
step={setting.ticker ?? 1}
defaultStartVal={setting.default.lower}
defaultEndVal={setting.default.upper}
onChange={({ startVal, endVal }) => handleSliderChange(index, { lower: startVal, upper: endVal })}
Expand Down Expand Up @@ -287,7 +287,7 @@ const UpdateNotifSettingDropdown: React.FC<UpdateNotifSettingDropdownProps> = ({
setLoadingFunc(false);
}
};

console.log('user settings', userSetting, channelSetting);
// render
return (
<DropdownBtnHandler
Expand Down

0 comments on commit 9860137

Please sign in to comment.