This commit is contained in:
Anthony 2022-03-17 18:53:46 +01:00 committed by Thomas Zarebczan
parent 7508dc2970
commit 70b801e867
2 changed files with 20 additions and 35 deletions

View file

@ -1,5 +1,5 @@
// @flow // @flow
import { useState, useEffect } from 'react'; import { useEffect } from 'react';
import { getChannelFromClaim } from 'util/claim'; import { getChannelFromClaim } from 'util/claim';
export default function useGetUserMemberships( export default function useGetUserMemberships(
@ -8,10 +8,10 @@ export default function useGetUserMemberships(
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([]); // instantiate variable if it doesn't exist yet
const checkedMemberships = window.checkedMemberships || {}; if (!window.checkedMemberships) window.checkedMemberships = {};
useEffect(() => { useEffect(() => {
// run if there's uris to check // run if there's uris to check
@ -22,52 +22,40 @@ export default function useGetUserMemberships(
// convert content urls to channel claim ids // 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
const claimUrlsToId = convertClaimUrlsToIds[uri]; const contentClaimId = convertClaimUrlsToIds[uri];
// return channel claim id
if (claimUrlsToId) { if (contentClaimId) return getChannelFromClaim(contentClaimId)?.claim_id;
const { claim_id: claimId } = getChannelFromClaim(claimUrlsToId) || {};
return claimId;
}
}); });
} else { } else {
// call already comes with an array of channel claim ids // call already comes with an array of channel claim ids
claimIds = arrayOfContentUris; claimIds = arrayOfContentUris;
} }
const dedupedChannelIds = [...new Set(claimIds)]; // remove dupes and falsey values
const dedupedChannelIds = [...new Set(claimIds)].filter(Boolean);
console.log('running here!');
// TODO: change here
// check if channel id has already been fetched // check if channel id has already been fetched
const channelsToFetch = dedupedChannelIds.filter( const channelsToFetch = dedupedChannelIds.filter(
(channelClaimId) => channelClaimId && !checkedMemberships[channelClaimId] // if value exists or is null it's been through the backend
// (channelClaimId) => channelClaimId && !userMemberships.includes(channelClaimId) (channelClaimId) =>
!window.checkedMemberships[channelClaimId] && window.checkedMemberships[channelClaimId] !== null
); );
console.log('just checked'); // create csv string for backend
const commaSeparatedStringOfIds = channelsToFetch.join(','); const commaSeparatedStringOfIds = channelsToFetch.join(',');
// new channels to hit, hit check api and save in state // new channels to hit, hit check api and save in state
if (channelsToFetch && channelsToFetch.length > 0) { if (channelsToFetch && channelsToFetch.length > 0) {
console.log('channels to fetch!'); // setup object to be added to window
console.log(channelsToFetch);
// save updated checked channels in state
const combinedArray = [...userMemberships, ...channelsToFetch];
setUserMemberships(combinedArray);
let membershipsToAdd = {}; let membershipsToAdd = {};
for (const channelToFetch of channelsToFetch) { for (const channelToFetch of channelsToFetch) {
// mark as waiting while waiting for backend, so won't recall
membershipsToAdd[channelToFetch] = 'waiting'; membershipsToAdd[channelToFetch] = 'waiting';
} }
const combinedMemberships = Object.assign(checkedMemberships, membershipsToAdd); // update checked memberships
console.log(combinedMemberships); window.checkedMemberships = Object.assign(window.checkedMemberships, membershipsToAdd);
window.checkedMemberships = combinedMemberships;
// hit membership/check and save it in redux // hit membership/check and save it in redux
if (doFetchUserMemberships) { if (doFetchUserMemberships) {

View file

@ -918,7 +918,6 @@ 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) {
@ -930,18 +929,16 @@ 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; window.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[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 } });
}; };
} }