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( export default function useGetUserMemberships(
shouldFetchUserMemberships: ?boolean, shouldFetchUserMemberships: ?boolean,
arrayOfContentUris: ?Array<string>, arrayOfContentUris: ?Array<string>,
convertClaimUrlsToIds: any, convertClaimUrlsToIds: any, //
doFetchUserMemberships: (string) => void, // fetch membership values and save in redux doFetchUserMemberships: (string) => void, // fetch membership values and save in redux
dependency?: any, dependency?: any,
alreadyClaimIds?: boolean, alreadyClaimIds?: boolean,
) { ) {
const [userMemberships, setUserMemberships] = useState([]); const [userMemberships, setUserMemberships] = useState([]);
const checkedMemberships = window.checkedMemberships || {};
useEffect(() => { useEffect(() => {
// run if there's uris to check
if (shouldFetchUserMemberships && arrayOfContentUris && arrayOfContentUris.length > 0) { if (shouldFetchUserMemberships && arrayOfContentUris && arrayOfContentUris.length > 0) {
const urisToFetch = arrayOfContentUris; const urisToFetch = arrayOfContentUris;
let claimIds; let claimIds;
// convert content urls to channel claim ids
if (!alreadyClaimIds) { if (!alreadyClaimIds) {
claimIds = urisToFetch.map((uri) => { claimIds = urisToFetch.map((uri) => {
// get claim id from array // get claim id from array
@ -28,30 +31,45 @@ export default function useGetUserMemberships(
} }
}); });
} else { } else {
// call already comes with an array of channel claim ids
claimIds = arrayOfContentUris; claimIds = arrayOfContentUris;
} }
const dedupedChannelIds = [...new Set(claimIds)]; const dedupedChannelIds = [...new Set(claimIds)];
const channelClaimIdsToCheck = dedupedChannelIds.filter( console.log('running here!');
// not in fetched claims but exists in array
(claimId) => claimId && !userMemberships.includes(claimId) // 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( console.log('just checked');
// not in fetched claims but exists in array
(uri) => uri && !userMemberships.includes(uri)
);
const commaSeparatedStringOfIds = channelsToFetch.join(','); const commaSeparatedStringOfIds = channelsToFetch.join(',');
// new channels to hit, hit check api and save in state
if (channelsToFetch && channelsToFetch.length > 0) { 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]; const combinedArray = [...userMemberships, ...channelsToFetch];
setUserMemberships(combinedArray); 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) { if (doFetchUserMemberships) {
doFetchUserMemberships(commaSeparatedStringOfIds); doFetchUserMemberships(commaSeparatedStringOfIds);
} }

View file

@ -22,7 +22,7 @@ let stripeEnvironment = getStripeEnvironment();
const isDev = process.env.NODE_ENV !== 'production'; const isDev = process.env.NODE_ENV !== 'production';
let log = function (input) {}; let log = function(input) {};
if (isDev) log = console.log; if (isDev) log = console.log;
// odysee channel information since the memberships are only for Odysee // odysee channel information since the memberships are only for Odysee
@ -418,7 +418,7 @@ const OdyseeMembershipPage = (props: Props) => {
<ul> <ul>
<li> <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>
<li> <li>

View file

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