Skip to content

Commit

Permalink
#119 Fetch subscription in RMP after redeem
Browse files Browse the repository at this point in the history
  • Loading branch information
thekingofcity committed Sep 15, 2024
1 parent 75aab38 commit 442147d
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 5 deletions.
2 changes: 2 additions & 0 deletions src/components/modal/redeem-modal.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { MdOpenInNew } from 'react-icons/md';
import { useRootDispatch, useRootSelector } from '../../redux';
import { setToken } from '../../redux/account/account-slice';
import { API_ENDPOINT } from '../../util/constants';
import { notifyRMPTokenUpdate } from '../../util/local-storage-save';
import { apiFetch } from '../../util/utils';

const RedeemModal = (props: { isOpen: boolean; onClose: () => void; getSubscriptions: () => Promise<void> }) => {
Expand Down Expand Up @@ -60,6 +61,7 @@ const RedeemModal = (props: { isOpen: boolean; onClose: () => void; getSubscript
return;
}
await getSubscriptions();
notifyRMPTokenUpdate(updatedToken);
onClose();
};

Expand Down
15 changes: 10 additions & 5 deletions src/util/local-storage-save.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,15 @@ const onSaveUpdate = async (currentSaveId: number, token: string, refreshToken:
);
};

// This should triggger RMP to refetch subscription info.
export const notifyRMPTokenUpdate = (token: string) => {
channel.postMessage({
type: SaveManagerEventType.TOKEN_REQUEST,
token: token,
from: 'rmt',
} as SaveManagerEvent);
};

export const registerOnTokenRequest = (store: ReturnType<typeof createStore>) => {
const eventHandler = async (ev: MessageEvent<SaveManagerEvent>) => {
const { type, from } = ev.data;
Expand Down Expand Up @@ -128,11 +137,7 @@ export const registerOnTokenRequest = (store: ReturnType<typeof createStore>) =>
return;
}

channel.postMessage({
type: SaveManagerEventType.TOKEN_REQUEST,
token: tokenAfterFetch,
from: 'rmt',
} as SaveManagerEvent);
notifyRMPTokenUpdate(tokenAfterFetch);
}
};

Expand Down

0 comments on commit 442147d

Please sign in to comment.