fixed ratings getting stuck in some places

This commit is contained in:
Victor committed 2024-01-19 15:10:57 -08:00
1 parent bd50656dba
commit ac599cbd50
3 files changed
+40 -27

No files matched your search

@@ -28,7 +28,7 @@ export default function useBatchContent({
fetcher,
modLibrary = (current: any, res: any, entity_token: string ) => current,
onPass,
onFail,
onFail = { fetch: () => new Promise(() => {}) },
resultsKey,
toggleCheck
}: Props) {
@@ -36,26 +36,31 @@ export default function useBatchContent({
const [library, librarySet] = useState<Library>({});
const [busyList, busyListSet] = useState<Library>({});
const [status, statusSet] = useState(FetchStatus.ready);
const [tokenType,tokenTypeSet] = useState("");
const gather = ({ expand, key, res }: Gather) => {
let tokens = res.results ? res.results.map((item: any) => item[key]) : [res[key]];
let abc = tokens.reduce((obj = {},token = "") => ({ ...obj, [token]: true }),{})
// console.log("🪙",fetcher);
busyListSet(tokens.reduce((obj = {},token = "") => ({ ...obj, [token]: true }),{})); // add current batch to busy list
tokenTypeSet(key)
busyListSet(abc); // add current batch to busy list
fetcher("",{},{ tokens }).then((batchRes: any) => {
// console.log("🪙", res, modLibrary);
// console.log("🦄",resultsKey, res, modLibrary);
if (batchRes.success && !!batchRes[resultsKey]) {
// console.log("🥏",batchRes[resultsKey]);
// console.log("🥏",resultsKey);
let newBatch = batchRes[resultsKey].reduce((obj = {}, { entity_token = "", ...current }) => {
// console.log("🧲",current, res, entity_token, obj);
// console.log("🧲", resultsKey, {current, res, entity_token, obj});
let newCurrent = {
...obj,
[entity_token]: { ...modLibrary(current, res, entity_token) }
[entity_token]: { ...modLibrary(current, res, entity_token, key) }
};
// console.log("😡", resultsKey, newCurrent);
return newCurrent;
},{});
// console.log("😡", newBatch);
// console.log("🐸",busyList);
busyListSet({}); // this should be a for each key in tokens delete from busyList, but this is fine for now
librarySet((library: any) => expand ? { ...library, ...newBatch } : newBatch);
}
@@ -80,6 +85,7 @@ export default function useBatchContent({
console.log(`⏳ toggling entity ${ entity_token }, in library?: ${ !!inLibrary }`);
if (inLibrary && checker(inLibrary)) {
// console.log("⭕️");
return onPass.fetch(entity_token, entity_type, library)
.then((res: any) => {
console.log("⭕️",res);
@@ -89,6 +95,7 @@ export default function useBatchContent({
return false;
});
} else {
// console.log("❌");
return onFail.fetch(entity_token, entity_type, library)
.then((res: any) => {
console.log("❌",res);
@@ -123,6 +130,7 @@ export default function useBatchContent({
makeProps,
status,
statusSet,
tokenType,
toggle,
toggled
};
@@ -4,40 +4,45 @@ import { useBatchContent } from "hooks";
export default function useRatings() {
const fetch = (entity_token: string, entity_type: string, lib: any) => {
return SetRating("",{
const newRating = {
entity_token,
entity_type,
rating_value: lib[entity_token].rating_value === "neutral" ? "positive" : "neutral"
});
rating_value: lib[entity_token]?.rating_value !== "positive" ? "positive" : "neutral"
}
// console.log("😎",lib[entity_token], newRating);
return SetRating("", newRating);
};
const modLibrary = (res: any, entity_token: string, entity_type: string, lib: any) => {
return {
...lib,
[entity_token]: {
entity_type,
rating_value: lib[entity_token].rating_value === "neutral" ? "positive" : "neutral",
positive_rating_count: res.new_positive_rating_count_for_entity
}
};
};
const ratings = useBatchContent({
fetcher: GetRatings,
checker: () => true,
modLibrary: (current: any, res: any, entity_token: string) => {
modLibrary: (current: any, res: any, entity_token: string, tokenType: string) => {
// console.log("🪼", tokenType, res );
let { positive_rating_count } = res.results ? res.results.find((item: any, i: number) =>
item.weight_token === entity_token
item[tokenType] === entity_token
).stats : res.stats;
return { ...current, positive_rating_count };
},
onPass: {
fetch,
modLibrary: (res: any, entity_token: string, entity_type: string, lib: any) => {
return {
...lib,
[entity_token]: {
entity_type,
rating_value: lib[entity_token].rating_value === "neutral" ? "positive" : "neutral",
positive_rating_count: res.new_positive_rating_count_for_entity
}
};
}
},
onFail: { fetch, modLibrary },
onPass: { fetch, modLibrary },
resultsKey: "ratings",
toggleCheck: (entity: any) => (entity?.rating_value || "") === "positive"
});
// console.log("🍎",ratings.busyList);
return {
...ratings,
makeProps: ({ entityToken, entityType }: { entityToken: string, entityType: string }) => ({
@@ -39,7 +39,7 @@ export default function MediaTab() {
listSet,
onInputChange: () => setShowMasonryGrid(false),
onSuccess: res => {
bookmarks.gather({ res, expand: true, key: "token" }); // expand rather than replace for lazy loading
ratings.gather({ res, expand: true, key: "token" }); // expand rather than replace for lazy loading
setShowMasonryGrid(true);
},
requestList: true,