|
| 1 | +import { Banner, IOToast, VSpacer } from "@pagopa/io-app-design-system"; |
| 2 | +import * as pot from "@pagopa/ts-commons/lib/pot"; |
| 3 | +import Animated, { |
| 4 | + FadeIn, |
| 5 | + FadeOut, |
| 6 | + LinearTransition |
| 7 | +} from "react-native-reanimated"; |
| 8 | +import { useEffect } from "react"; |
| 9 | +import { useIODispatch, useIOSelector } from "../../../../store/hooks"; |
| 10 | +import { isIdPayEnabledSelector } from "../../../../store/reducers/backendStatus/remoteConfig"; |
| 11 | +import { isIdPayOnboardingSucceededSelector } from "../../../idpay/wallet/store/reducers"; |
| 12 | +import { |
| 13 | + isEmailEnabledSelector, |
| 14 | + profileSelector |
| 15 | +} from "../../../settings/common/store/selectors"; |
| 16 | +import { profileUpsert } from "../../../settings/common/store/actions"; |
| 17 | +import I18n from "../../../../i18n"; |
| 18 | +import { usePrevious } from "../../../../utils/hooks/usePrevious"; |
| 19 | +import { setIdPayOnboardingSucceeded } from "../../../idpay/wallet/store/actions"; |
| 20 | + |
| 21 | +export const EmailNotificationBanner = () => { |
| 22 | + const dispatch = useIODispatch(); |
| 23 | + const isIdPayOnboardingSucceeded = useIOSelector( |
| 24 | + isIdPayOnboardingSucceededSelector |
| 25 | + ); |
| 26 | + const isIdPayEnabled = useIOSelector(isIdPayEnabledSelector); |
| 27 | + const isEmailChannelEnabled = useIOSelector(isEmailEnabledSelector); |
| 28 | + const profile = useIOSelector(profileSelector); |
| 29 | + const prevProfile = usePrevious(profile); |
| 30 | + |
| 31 | + const canShowBanner = |
| 32 | + isIdPayEnabled && isIdPayOnboardingSucceeded && !isEmailChannelEnabled; |
| 33 | + |
| 34 | + const handleOnEnableEmailChannel = () => { |
| 35 | + dispatch( |
| 36 | + profileUpsert.request({ |
| 37 | + is_email_enabled: true |
| 38 | + }) |
| 39 | + ); |
| 40 | + }; |
| 41 | + |
| 42 | + useEffect(() => { |
| 43 | + if (prevProfile && pot.isUpdating(prevProfile)) { |
| 44 | + if (pot.isError(profile)) { |
| 45 | + IOToast.error(I18n.t("global.genericError")); |
| 46 | + return; |
| 47 | + } |
| 48 | + if (pot.isSome(profile)) { |
| 49 | + dispatch(setIdPayOnboardingSucceeded(false)); |
| 50 | + IOToast.hideAll(); |
| 51 | + IOToast.success( |
| 52 | + I18n.t( |
| 53 | + "idpay.onboarding.preferences.enableEmailBanner.successOutcome" |
| 54 | + ) |
| 55 | + ); |
| 56 | + return; |
| 57 | + } |
| 58 | + } |
| 59 | + }, [profile, prevProfile, canShowBanner, dispatch]); |
| 60 | + |
| 61 | + const handleOnCloseBanner = () => { |
| 62 | + dispatch(setIdPayOnboardingSucceeded(false)); |
| 63 | + }; |
| 64 | + |
| 65 | + if (!canShowBanner) { |
| 66 | + return null; |
| 67 | + } |
| 68 | + |
| 69 | + return ( |
| 70 | + <Animated.View |
| 71 | + entering={FadeIn.duration(300)} |
| 72 | + exiting={FadeOut.duration(300)} |
| 73 | + layout={LinearTransition.duration(300)} |
| 74 | + > |
| 75 | + <VSpacer size={16} /> |
| 76 | + <Banner |
| 77 | + labelClose={I18n.t("global.buttons.close")} |
| 78 | + onClose={handleOnCloseBanner} |
| 79 | + color="turquoise" |
| 80 | + pictogramName="emailDotNotif" |
| 81 | + title={I18n.t("idpay.onboarding.preferences.enableEmailBanner.title")} |
| 82 | + content={I18n.t( |
| 83 | + "idpay.onboarding.preferences.enableEmailBanner.content" |
| 84 | + )} |
| 85 | + action={I18n.t("idpay.onboarding.preferences.enableEmailBanner.cta")} |
| 86 | + onPress={handleOnEnableEmailChannel} |
| 87 | + /> |
| 88 | + </Animated.View> |
| 89 | + ); |
| 90 | +}; |
0 commit comments