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

View file

@ -918,7 +918,6 @@ export function doFetchUserMemberships(claimIdCsv) {
});
let updatedResponse = {};
let checkedMemberships = window.checkedMemberships;
// loop through returned users
for (const user in response) {
@ -930,18 +929,16 @@ export function doFetchUserMemberships(claimIdCsv) {
for (const membership of response[user]) {
if (membership.channel_name) {
updatedResponse[user] = membership.name;
checkedMemberships[user] = membership.name;
window.checkedMemberships[user] = membership.name;
}
}
} else {
// note the user has been fetched but is 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 } });
};
}