From 0ff706df11768ae224c7e06576a0625a16aa2d82 Mon Sep 17 00:00:00 2001 From: Matthias Luger Date: Mon, 26 Feb 2024 23:09:24 +0100 Subject: [PATCH] subscribe to coflcoin changes --- api/ApiHelper.tsx | 25 ++++++++++++++----------- api/ApiTypes.d.tsx | 2 +- utils/CoflCoinsUtils.tsx | 1 + 3 files changed, 16 insertions(+), 12 deletions(-) diff --git a/api/ApiHelper.tsx b/api/ApiHelper.tsx index 4945272b..9f447078 100644 --- a/api/ApiHelper.tsx +++ b/api/ApiHelper.tsx @@ -50,11 +50,12 @@ import { storeUsedTagsInLocalStorage } from '../utils/SettingsUtils' import { isClientSideRendering } from '../utils/SSRUtils' -import { HttpApi, RequestType, Subscription, SubscriptionType } from './ApiTypes.d' +import { HttpApi, RequestType, SubscriptionType, CUSTOM_EVENTS, Subscription } from './ApiTypes.d' import { initHttpHelper } from './HttpHelper' import { websocketHelper } from './WebsocketHelper' import { canUseClipBoard, writeToClipboard } from '../utils/ClipboardUtils' import properties from '../properties' +import { getCurrentCoflCoins } from '../utils/CoflCoinsUtils' function getApiEndpoint() { return isClientSideRendering() ? getProperty('apiEndpoint') : process.env.API_ENDPOINT || getProperty('apiEndpoint') @@ -1089,22 +1090,24 @@ export function initAPI(returnSSRResponse: boolean = false): API { } let subscribeCoflCoinChange = () => { - // TODO: Has yet to be implemented by the backend - /* + console.log('subscribe cofl coin change') websocketHelper.subscribe({ - type: RequestType.SUBSCRIBE_COFLCOINS, + type: RequestType.SUBSCRIBE_EVENTS, data: '', + resubscribe: function (subscription) { + subscribeCoflCoinChange() + }, + onError: function (message) { + toast.error(message) + }, callback: function (response) { - switch (response.type) { - case 'coflCoinUpdate': - document.dispatchEvent(new CustomEvent(CUSTOM_EVENTS.COFLCOIN_UPDATE, { detail: { coflCoins: response.data } })) - break - default: - break + if (response.data.sourceType === 'purchase' || response.data.sourceType === 'topup') { + document.dispatchEvent( + new CustomEvent(CUSTOM_EVENTS.COFLCOIN_UPDATE, { detail: { coflCoins: getCurrentCoflCoins() + Math.round(response.data.data.amount) } }) + ) } } }) - */ } let getCoflcoinBalance = (): Promise => { diff --git a/api/ApiTypes.d.tsx b/api/ApiTypes.d.tsx index c90e3278..2706ef71 100644 --- a/api/ApiTypes.d.tsx +++ b/api/ApiTypes.d.tsx @@ -58,7 +58,7 @@ export enum RequestType { GET_KAT_FLIPS = 'kat/profit', GET_TRACKED_FLIPS_FOR_PLAYER = 'flip/stats/player', PURCHASE_WITH_COFLCOiNS = 'service/purchase', - SUBSCRIBE_COFLCOINS = 'subscribeCoflCoins', + SUBSCRIBE_EVENTS = 'subEvents', GET_COFLCOIN_BALANCE = 'getCoflBalance', GET_FLIP_SETTINGS = 'getFlipSettings', SET_FLIP_SETTING = 'setFlipSetting', diff --git a/utils/CoflCoinsUtils.tsx b/utils/CoflCoinsUtils.tsx index fd832adf..16826bed 100644 --- a/utils/CoflCoinsUtils.tsx +++ b/utils/CoflCoinsUtils.tsx @@ -53,6 +53,7 @@ export function initCoflCoinManager() { currentCoflCoins = coflCoins notifyAboutCoflCoinUpdate(coflCoins) }) + document.removeEventListener(CUSTOM_EVENTS.GOOGLE_LOGIN, initCoflCoinBalanceAndSubscriptions) } document.addEventListener(CUSTOM_EVENTS.GOOGLE_LOGIN, initCoflCoinBalanceAndSubscriptions)