Skip to content

Commit

Permalink
chore: add revocation object in wallet instance global state, includi…
Browse files Browse the repository at this point in the history
…ng also alert shown that handle only the first visualizazion of the revocation alert
  • Loading branch information
RiccardoMolinari95 committed Dec 17, 2024
1 parent e37c2fc commit 1728866
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 21 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ import React from "react";
import { IOToast } from "@pagopa/io-app-design-system";
import I18n from "../../../../i18n";
import { WalletInstanceRevocationReason } from "../../common/utils/itwTypesUtils";
import { useIODispatch, useIOSelector } from "../../../../store/hooks";
import { itwWalletInstanceAlertShownSelector } from "../store/selectors";
import { itwWalletInstanceSetAlertShown } from "../store/actions";

const closeButtonText = I18n.t(
"features.itWallet.walletInstanceRevoked.alert.closeButton"
Expand All @@ -21,11 +24,15 @@ export const useItwWalletInstanceRevocationAlert = (walletInstanceStatus: {
isRevoked: boolean;
revocationReason?: WalletInstanceRevocationReason;
}) => {
const dispatch = useIODispatch();
const alertShown = useIOSelector(itwWalletInstanceAlertShownSelector);

React.useEffect(() => {
if (walletInstanceStatus.isRevoked) {
if (walletInstanceStatus.isRevoked && !alertShown) {
showWalletRevocationAlert(walletInstanceStatus.revocationReason);
dispatch(itwWalletInstanceSetAlertShown());
}
}, [walletInstanceStatus]);
}, [walletInstanceStatus, alertShown, dispatch]);
};

/**
Expand Down
10 changes: 9 additions & 1 deletion ts/features/itwallet/walletInstance/store/actions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,14 @@ export const itwUpdateWalletInstanceStatus = createStandardAction(
"ITW_WALLET_INSTANCE_STATUS_UPDATE"
)<WalletInstanceStatus>();

/**
* This action sets the wallet instance alert shown
*/
export const itwWalletInstanceSetAlertShown = createStandardAction(
"ITW_WALLET_INSTANCE_SET_ALERT_SHOWN"
)<void>();

export type ItwWalletInstanceActions =
| ActionType<typeof itwWalletInstanceAttestationStore>
| ActionType<typeof itwUpdateWalletInstanceStatus>;
| ActionType<typeof itwUpdateWalletInstanceStatus>
| ActionType<typeof itwWalletInstanceSetAlertShown>;
33 changes: 26 additions & 7 deletions ts/features/itwallet/walletInstance/store/reducers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,27 @@ import itwCreateSecureStorage from "../../../common/store/storages/itwSecureStor
import { itwLifecycleStoresReset } from "../../../lifecycle/store/actions";
import {
itwWalletInstanceAttestationStore,
itwUpdateWalletInstanceStatus
itwUpdateWalletInstanceStatus,
itwWalletInstanceSetAlertShown
} from "../actions";
import { WalletInstanceRevocationReason } from "../../../common/utils/itwTypesUtils";

export type ItwWalletInstanceState = {
attestation: string | undefined;
isRevoked: boolean;
revocationReason?: WalletInstanceRevocationReason;
revocation: {
isRevoked: boolean;
revocationReason?: WalletInstanceRevocationReason;
alertShown: boolean;
};
};

export const itwWalletInstanceInitialState: ItwWalletInstanceState = {
attestation: undefined,
isRevoked: false,
revocationReason: undefined
revocation: {
isRevoked: false,
revocationReason: undefined,
alertShown: false
}
};

const CURRENT_REDUX_ITW_WALLET_INSTANCE_STORE_VERSION = -1;
Expand All @@ -38,11 +45,23 @@ const reducer = (
case getType(itwUpdateWalletInstanceStatus): {
return {
...state,
isRevoked: action.payload.is_revoked,
revocationReason: action.payload.revocation_reason
revocation: {
...state.revocation,
isRevoked: action.payload.is_revoked,
revocationReason: action.payload.revocation_reason
}
};
}

case getType(itwWalletInstanceSetAlertShown):
return {
...state,
revocation: {
...state.revocation,
alertShown: true
}
};

case getType(itwLifecycleStoresReset):
return { ...itwWalletInstanceInitialState };

Expand Down
16 changes: 11 additions & 5 deletions ts/features/itwallet/walletInstance/store/selectors/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,22 @@ export const itwIsWalletInstanceAttestationValidSelector = createSelector(
)
);

/* Selector to get the alert shown status */
export const itwWalletInstanceAlertShownSelector = createSelector(
(state: GlobalState) => state.features.itWallet.walletInstance.revocation,
revocation => revocation.alertShown
);

/* Selector to get the wallet instance status */
export const itwWalletInstanceStatusSelector = createSelector(
(state: GlobalState) => state.features.itWallet.walletInstance,
walletInstance =>
export const itwWalletInstanceRevocationStatusSelector = createSelector(
(state: GlobalState) => state.features.itWallet.walletInstance.revocation,
revocation =>
pipe(
O.fromNullable(walletInstance.isRevoked),
O.fromNullable(revocation.isRevoked),
O.filter(Boolean),
O.map(() => ({
isRevoked: true,
revocationReason: walletInstance.revocationReason
revocationReason: revocation.revocationReason
})),
O.getOrElse(() => ({ isRevoked: false }))
)
Expand Down
6 changes: 4 additions & 2 deletions ts/features/wallet/components/WalletCardsContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ import {
} from "../store/selectors";
import { WalletCardCategoryFilter } from "../types";
import { useItwWalletInstanceRevocationAlert } from "../../itwallet/walletInstance/hook/useItwWalletInstanceRevocationAlert";
import { itwWalletInstanceStatusSelector } from "../../itwallet/walletInstance/store/selectors";
import { itwWalletInstanceRevocationStatusSelector } from "../../itwallet/walletInstance/store/selectors";
import { WalletCardsCategoryContainer } from "./WalletCardsCategoryContainer";
import { WalletCardsCategoryRetryErrorBanner } from "./WalletCardsCategoryRetryErrorBanner";
import { WalletCardSkeleton } from "./WalletCardSkeleton";
Expand All @@ -49,7 +49,9 @@ const WalletCardsContainer = () => {
const shouldRenderEmptyState = useIOSelector(
shouldRenderWalletEmptyStateSelector
);
const walletInstanceStatus = useIOSelector(itwWalletInstanceStatusSelector);
const walletInstanceStatus = useIOSelector(
itwWalletInstanceRevocationStatusSelector
);

useItwWalletInstanceRevocationAlert(walletInstanceStatus);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,10 @@ describe("OtherWalletCardsContainer", () => {

it("should not show alert if not revoked", () => {
jest
.spyOn(itwWalletInstanceSelectors, "itwWalletInstanceStatusSelector")
.spyOn(
itwWalletInstanceSelectors,
"itwWalletInstanceRevocationStatusSelector"
)
.mockImplementation(() => ({
isRevoked: false,
revocationReason: ""
Expand All @@ -377,7 +380,10 @@ describe("OtherWalletCardsContainer", () => {

it("should show alert for NEW_WALLET_INSTANCE_CREATED", () => {
jest
.spyOn(itwWalletInstanceSelectors, "itwWalletInstanceStatusSelector")
.spyOn(
itwWalletInstanceSelectors,
"itwWalletInstanceRevocationStatusSelector"
)
.mockImplementation(() => ({
isRevoked: true,
revocationReason: "NEW_WALLET_INSTANCE_CREATED"
Expand Down Expand Up @@ -408,7 +414,10 @@ describe("OtherWalletCardsContainer", () => {

it("should show alert for CERTIFICATE_REVOKED_BY_ISSUER", () => {
jest
.spyOn(itwWalletInstanceSelectors, "itwWalletInstanceStatusSelector")
.spyOn(
itwWalletInstanceSelectors,
"itwWalletInstanceRevocationStatusSelector"
)
.mockImplementation(() => ({
isRevoked: true,
revocationReason: "CERTIFICATE_REVOKED_BY_ISSUER"
Expand Down Expand Up @@ -439,7 +448,10 @@ describe("OtherWalletCardsContainer", () => {

it("should show alert for REVOKED_BY_USER", () => {
jest
.spyOn(itwWalletInstanceSelectors, "itwWalletInstanceStatusSelector")
.spyOn(
itwWalletInstanceSelectors,
"itwWalletInstanceRevocationStatusSelector"
)
.mockImplementation(() => ({
isRevoked: true,
revocationReason: "REVOKED_BY_USER"
Expand Down

0 comments on commit 1728866

Please sign in to comment.