use window instead of component state

This commit is contained in:
Anthony 2022-03-16 21:03:08 +01:00 committed by Thomas Zarebczan
parent c294fda09d
commit 7508dc2970
3 changed files with 37 additions and 12 deletions

View file

@ -5,18 +5,21 @@ import { getChannelFromClaim } from 'util/claim';
export default function useGetUserMemberships(
shouldFetchUserMemberships: ?boolean,
arrayOfContentUris: ?Array<string>,
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);
}

View file

@ -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) => {
<ul>
<li>
{__(
'Exclusive and early access features include: recommended content on homepage, livestreaming, and the ability to post odysee hyperlinks + images in comments. Account is also automatically eligible for Rewards. More to come later.'
'Exclusive and early access features include: recommended content on homepage, livestreaming, and the ability to post Odysee hyperlinks + images in comments. Account is also automatically eligible for Rewards. More to come later.'
)}
</li>
<li>

View file

@ -908,6 +908,8 @@ export function doCheckYoutubeTransfer() {
*/
export function doFetchUserMemberships(claimIdCsv) {
return async (dispatch) => {
if (!claimIdCsv || (claimIdCsv.length && claimIdCsv.length < 1)) return;
// check if users have odysee memberships (premium/premium+)
const response = await Lbryio.call('membership', 'check', {
channel_id: ODYSEE_CHANNEL_ID,
@ -916,6 +918,7 @@ export function doFetchUserMemberships(claimIdCsv) {
});
let updatedResponse = {};
let checkedMemberships = window.checkedMemberships;
// loop through returned users
for (const user in response) {
@ -927,14 +930,18 @@ export function doFetchUserMemberships(claimIdCsv) {
for (const membership of response[user]) {
if (membership.channel_name) {
updatedResponse[user] = membership.name;
checkedMemberships[user] = membership.name;
}
}
} else {
// note the user has been fetched but is null
updatedResponse[user] = null;
checkedMemberships[user] = null;
}
}
window.checkedMemberships = Object.assign(window.checkedMemberships, checkedMemberships);
dispatch({ type: ACTIONS.ADD_CLAIMIDS_MEMBERSHIP_DATA, data: { response: updatedResponse } });
};
}