From 7508dc2970a02b99332ae51f70a9d7fa847cac7b Mon Sep 17 00:00:00 2001 From: Anthony Date: Wed, 16 Mar 2022 21:03:08 +0100 Subject: [PATCH] use window instead of component state --- ui/effects/use-get-user-memberships.js | 38 +++++++++++++++++++------- ui/page/odyseeMembership/view.jsx | 4 +-- ui/redux/actions/user.js | 7 +++++ 3 files changed, 37 insertions(+), 12 deletions(-) diff --git a/ui/effects/use-get-user-memberships.js b/ui/effects/use-get-user-memberships.js index 38daddf12..590cd88db 100644 --- a/ui/effects/use-get-user-memberships.js +++ b/ui/effects/use-get-user-memberships.js @@ -5,18 +5,21 @@ import { getChannelFromClaim } from 'util/claim'; export default function useGetUserMemberships( shouldFetchUserMemberships: ?boolean, arrayOfContentUris: ?Array, - convertClaimUrlsToIds: any, + convertClaimUrlsToIds: any, // doFetchUserMemberships: (string) => void, // fetch membership values and save in redux dependency?: any, alreadyClaimIds?: boolean, ) { const [userMemberships, setUserMemberships] = useState([]); + const checkedMemberships = window.checkedMemberships || {}; useEffect(() => { + // run if there's uris to check if (shouldFetchUserMemberships && arrayOfContentUris && arrayOfContentUris.length > 0) { const urisToFetch = arrayOfContentUris; let claimIds; + // convert content urls to channel claim ids if (!alreadyClaimIds) { claimIds = urisToFetch.map((uri) => { // get claim id from array @@ -28,30 +31,45 @@ export default function useGetUserMemberships( } }); } else { + // call already comes with an array of channel claim ids claimIds = arrayOfContentUris; } const dedupedChannelIds = [...new Set(claimIds)]; - const channelClaimIdsToCheck = dedupedChannelIds.filter( - // not in fetched claims but exists in array - (claimId) => claimId && !userMemberships.includes(claimId) + console.log('running here!'); + + // TODO: change here + // check if channel id has already been fetched + const channelsToFetch = dedupedChannelIds.filter( + (channelClaimId) => channelClaimId && !checkedMemberships[channelClaimId] + // (channelClaimId) => channelClaimId && !userMemberships.includes(channelClaimId) ); - const channelsToFetch = channelClaimIdsToCheck.filter( - // not in fetched claims but exists in array - (uri) => uri && !userMemberships.includes(uri) - ); + console.log('just checked'); const commaSeparatedStringOfIds = channelsToFetch.join(','); + // new channels to hit, hit check api and save in state if (channelsToFetch && channelsToFetch.length > 0) { - // hit membership/check and save it in redux + console.log('channels to fetch!'); + console.log(channelsToFetch); + // save updated checked channels in state const combinedArray = [...userMemberships, ...channelsToFetch]; - setUserMemberships(combinedArray); + let membershipsToAdd = {}; + for (const channelToFetch of channelsToFetch) { + membershipsToAdd[channelToFetch] = 'waiting'; + } + + const combinedMemberships = Object.assign(checkedMemberships, membershipsToAdd); + console.log(combinedMemberships); + + window.checkedMemberships = combinedMemberships; + + // hit membership/check and save it in redux if (doFetchUserMemberships) { doFetchUserMemberships(commaSeparatedStringOfIds); } diff --git a/ui/page/odyseeMembership/view.jsx b/ui/page/odyseeMembership/view.jsx index 438062084..2ad4d9eee 100644 --- a/ui/page/odyseeMembership/view.jsx +++ b/ui/page/odyseeMembership/view.jsx @@ -22,7 +22,7 @@ let stripeEnvironment = getStripeEnvironment(); const isDev = process.env.NODE_ENV !== 'production'; -let log = function (input) {}; +let log = function(input) {}; if (isDev) log = console.log; // odysee channel information since the memberships are only for Odysee @@ -418,7 +418,7 @@ const OdyseeMembershipPage = (props: Props) => {