Removed unused API callouts and get saved components transplanted into context
This commit is contained in:
parent
00c7ff8cd3
commit
8f0348a147
6 changed files with 59 additions and 91 deletions
|
|
@ -1,5 +1,9 @@
|
||||||
import { createContext, useEffect, useState } from "react";
|
import { createContext, useEffect, useState } from "react";
|
||||||
import { checkHasApiKey, checkHasStore } from "../controllers/API";
|
import {
|
||||||
|
checkHasApiKey,
|
||||||
|
checkHasStore,
|
||||||
|
getStoreComponents,
|
||||||
|
} from "../controllers/API";
|
||||||
import { storeContextType } from "../types/contexts/store";
|
import { storeContextType } from "../types/contexts/store";
|
||||||
|
|
||||||
//store context to share user components and update them
|
//store context to share user components and update them
|
||||||
|
|
@ -10,6 +14,9 @@ const initialValue = {
|
||||||
setHasStore: () => {},
|
setHasStore: () => {},
|
||||||
hasApiKey: false,
|
hasApiKey: false,
|
||||||
setHasApiKey: () => {},
|
setHasApiKey: () => {},
|
||||||
|
getSavedComponents: () => {},
|
||||||
|
errorApiKey: false,
|
||||||
|
loadingSaved: false,
|
||||||
};
|
};
|
||||||
|
|
||||||
export const StoreContext = createContext<storeContextType>(initialValue);
|
export const StoreContext = createContext<storeContextType>(initialValue);
|
||||||
|
|
@ -20,6 +27,8 @@ export function StoreProvider({ children }) {
|
||||||
const [hasStore, setHasStore] = useState(true);
|
const [hasStore, setHasStore] = useState(true);
|
||||||
const [hasApiKey, setHasApiKey] = useState(false);
|
const [hasApiKey, setHasApiKey] = useState(false);
|
||||||
const [storeChecked, setStoreChecked] = useState(false);
|
const [storeChecked, setStoreChecked] = useState(false);
|
||||||
|
const [loadingSaved, setLoadingSaved] = useState(false);
|
||||||
|
const [errorApiKey, setErrorApiKey] = useState(false);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchStoreData = async () => {
|
const fetchStoreData = async () => {
|
||||||
|
|
@ -34,8 +43,33 @@ export function StoreProvider({ children }) {
|
||||||
};
|
};
|
||||||
|
|
||||||
fetchStoreData();
|
fetchStoreData();
|
||||||
|
getSavedComponents();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
function getSavedComponents() {
|
||||||
|
setLoadingSaved(true);
|
||||||
|
getStoreComponents(null, null, null, null, null, true)
|
||||||
|
.then((data) => {
|
||||||
|
if (data?.authorized === false) {
|
||||||
|
setErrorApiKey(true);
|
||||||
|
setSavedFlows(new Set<string>());
|
||||||
|
} else {
|
||||||
|
let savedIds = new Set<string>();
|
||||||
|
let results = data?.results ?? [];
|
||||||
|
results.forEach((flow) => {
|
||||||
|
savedIds.add(flow.id);
|
||||||
|
});
|
||||||
|
setSavedFlows(savedIds);
|
||||||
|
setErrorApiKey(false);
|
||||||
|
setLoadingSaved(false);
|
||||||
|
}
|
||||||
|
})
|
||||||
|
.catch((err) => {
|
||||||
|
setSavedFlows(new Set<string>());
|
||||||
|
setErrorApiKey(true);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchStoreData = async () => {
|
const fetchStoreData = async () => {
|
||||||
try {
|
try {
|
||||||
|
|
@ -59,6 +93,9 @@ export function StoreProvider({ children }) {
|
||||||
setHasStore,
|
setHasStore,
|
||||||
hasApiKey,
|
hasApiKey,
|
||||||
setHasApiKey,
|
setHasApiKey,
|
||||||
|
getSavedComponents,
|
||||||
|
errorApiKey,
|
||||||
|
loadingSaved,
|
||||||
}}
|
}}
|
||||||
>
|
>
|
||||||
{children}
|
{children}
|
||||||
|
|
|
||||||
|
|
@ -591,8 +591,8 @@ export async function getFlowsStore(): Promise<AxiosResponse<FlowType[]>> {
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getStoreComponents(
|
export async function getStoreComponents(
|
||||||
page: number = 1,
|
page?: number | null,
|
||||||
limit: number = 10,
|
limit?: number | null,
|
||||||
is_component?: boolean | null,
|
is_component?: boolean | null,
|
||||||
sort?: string | null,
|
sort?: string | null,
|
||||||
tags?: string[] | null,
|
tags?: string[] | null,
|
||||||
|
|
@ -624,10 +624,10 @@ export async function getStoreComponents(
|
||||||
}
|
}
|
||||||
|
|
||||||
if (page !== undefined) {
|
if (page !== undefined) {
|
||||||
queryParams.push(`page=${page}`);
|
queryParams.push(`page=${page ?? 1}`);
|
||||||
}
|
}
|
||||||
if (limit !== undefined) {
|
if (limit !== undefined) {
|
||||||
queryParams.push(`limit=${limit}`);
|
queryParams.push(`limit=${limit ?? 9999999}`);
|
||||||
}
|
}
|
||||||
if (is_component !== null && is_component !== undefined) {
|
if (is_component !== null && is_component !== undefined) {
|
||||||
queryParams.push(`is_component=${is_component}`);
|
queryParams.push(`is_component=${is_component}`);
|
||||||
|
|
@ -647,22 +647,6 @@ export async function getStoreComponents(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getStoreSavedComponents(): Promise<
|
|
||||||
StoreComponentResponse | undefined
|
|
||||||
> {
|
|
||||||
try {
|
|
||||||
const res = await api.get(
|
|
||||||
`${BASE_URL_API}store/components/?filter_by_user=true`
|
|
||||||
);
|
|
||||||
if (res.status === 200) {
|
|
||||||
return res.data;
|
|
||||||
}
|
|
||||||
} catch (error) {
|
|
||||||
console.log("Error:", error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export async function postStoreComponents(component: Component) {
|
export async function postStoreComponents(component: Component) {
|
||||||
try {
|
try {
|
||||||
const res = await api.post(`${BASE_URL_API}store/components/`, component);
|
const res = await api.post(`${BASE_URL_API}store/components/`, component);
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,13 @@
|
||||||
import { useContext, useEffect, useState } from "react";
|
import { useContext, useEffect, useState } from "react";
|
||||||
import CardsWrapComponent from "../../../../components/cardsWrapComponent";
|
import CardsWrapComponent from "../../../../components/cardsWrapComponent";
|
||||||
import { alertContext } from "../../../../contexts/alertContext";
|
import { alertContext } from "../../../../contexts/alertContext";
|
||||||
import {
|
import { getStoreComponents } from "../../../../controllers/API";
|
||||||
getStoreComponents,
|
|
||||||
getStoreSavedComponents,
|
|
||||||
} from "../../../../controllers/API";
|
|
||||||
import { storeComponent } from "../../../../types/store";
|
import { storeComponent } from "../../../../types/store";
|
||||||
import { MarketCardComponent } from "../../../StorePage/components/market-card";
|
import { MarketCardComponent } from "../../../StorePage/components/market-card";
|
||||||
|
|
||||||
export default function AddedComponents(): JSX.Element {
|
export default function AddedComponents(): JSX.Element {
|
||||||
const [data, setData] = useState<storeComponent[]>([]);
|
const [data, setData] = useState<storeComponent[]>([]);
|
||||||
const [loading, setLoading] = useState(false);
|
const [loading, setLoading] = useState(false);
|
||||||
const [filteredCategories, setFilteredCategories] = useState(new Set());
|
|
||||||
const { setErrorData } = useContext(alertContext);
|
const { setErrorData } = useContext(alertContext);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
|
|
@ -20,11 +16,13 @@ export default function AddedComponents(): JSX.Element {
|
||||||
|
|
||||||
const handleGetComponents = () => {
|
const handleGetComponents = () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
getStoreComponents(0, 10000)
|
getStoreComponents(null, null, true, "name", null, true)
|
||||||
.then((res) => {
|
.then((res) => {
|
||||||
handleAddedOnly(res);
|
setLoading(false);
|
||||||
|
setData(res?.results ?? []);
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
|
setData([]);
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
setErrorData({
|
setErrorData({
|
||||||
title: "Error to get components.",
|
title: "Error to get components.",
|
||||||
|
|
@ -33,34 +31,11 @@ export default function AddedComponents(): JSX.Element {
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
function handleAddedOnly(components: storeComponent[]) {
|
|
||||||
getStoreSavedComponents()
|
|
||||||
.then((res) => {
|
|
||||||
const idSet = new Set(res.map((item) => item.id));
|
|
||||||
const filteredArray = components.filter((item) => idSet.has(item.id));
|
|
||||||
setData(filteredArray);
|
|
||||||
setLoading(false);
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
setLoading(false);
|
|
||||||
setErrorData({
|
|
||||||
title: "Error to get components.",
|
|
||||||
list: [err["response"]["data"]["detail"]],
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<CardsWrapComponent isLoading={loading}>
|
<CardsWrapComponent isLoading={loading}>
|
||||||
{data
|
{data.map((item, idx) => (
|
||||||
.filter(
|
<MarketCardComponent key={idx} data={item} />
|
||||||
(f) =>
|
))}
|
||||||
Array.from(filteredCategories).length === 0 ||
|
|
||||||
filteredCategories.has(f.is_component)
|
|
||||||
)
|
|
||||||
.map((item, idx) => (
|
|
||||||
<MarketCardComponent key={idx} data={item} />
|
|
||||||
))}
|
|
||||||
</CardsWrapComponent>
|
</CardsWrapComponent>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -25,11 +25,11 @@ import { classNames } from "../../../utils/utils";
|
||||||
|
|
||||||
export const MarketCardComponent = ({
|
export const MarketCardComponent = ({
|
||||||
data,
|
data,
|
||||||
authorized,
|
authorized = true,
|
||||||
disabled = false,
|
disabled = false,
|
||||||
}: {
|
}: {
|
||||||
data: storeComponent;
|
data: storeComponent;
|
||||||
authorized: boolean;
|
authorized?: boolean;
|
||||||
disabled?: boolean;
|
disabled?: boolean;
|
||||||
}) => {
|
}) => {
|
||||||
const { savedFlows } = useContext(StoreContext);
|
const { savedFlows } = useContext(StoreContext);
|
||||||
|
|
|
||||||
|
|
@ -17,11 +17,7 @@ import {
|
||||||
import { alertContext } from "../../contexts/alertContext";
|
import { alertContext } from "../../contexts/alertContext";
|
||||||
import { StoreContext } from "../../contexts/storeContext";
|
import { StoreContext } from "../../contexts/storeContext";
|
||||||
import { TabsContext } from "../../contexts/tabsContext";
|
import { TabsContext } from "../../contexts/tabsContext";
|
||||||
import {
|
import { getStoreComponents, getStoreTags } from "../../controllers/API";
|
||||||
getStoreComponents,
|
|
||||||
getStoreSavedComponents,
|
|
||||||
getStoreTags,
|
|
||||||
} from "../../controllers/API";
|
|
||||||
import StoreApiKeyModal from "../../modals/StoreApiKeyModal";
|
import StoreApiKeyModal from "../../modals/StoreApiKeyModal";
|
||||||
import { storeComponent } from "../../types/store";
|
import { storeComponent } from "../../types/store";
|
||||||
import { cn } from "../../utils/utils";
|
import { cn } from "../../utils/utils";
|
||||||
|
|
@ -31,11 +27,11 @@ export default function StorePage(): JSX.Element {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setTabId("");
|
setTabId("");
|
||||||
}, []);
|
}, []);
|
||||||
const { setSavedFlows, hasApiKey } = useContext(StoreContext);
|
const { getSavedComponents, loadingSaved, errorApiKey, hasApiKey } =
|
||||||
|
useContext(StoreContext);
|
||||||
const { setErrorData } = useContext(alertContext);
|
const { setErrorData } = useContext(alertContext);
|
||||||
const [loading, setLoading] = useState(true);
|
const [loading, setLoading] = useState(true);
|
||||||
const [loadingTags, setLoadingTags] = useState(true);
|
const [loadingTags, setLoadingTags] = useState(true);
|
||||||
const [loadingSaved, setLoadingSaved] = useState(false);
|
|
||||||
const [filteredCategories, setFilterCategories] = useState<any[]>([]);
|
const [filteredCategories, setFilterCategories] = useState<any[]>([]);
|
||||||
const [inputText, setInputText] = useState<string>("");
|
const [inputText, setInputText] = useState<string>("");
|
||||||
const [searchData, setSearchData] = useState<storeComponent[]>([]);
|
const [searchData, setSearchData] = useState<storeComponent[]>([]);
|
||||||
|
|
@ -43,13 +39,11 @@ export default function StorePage(): JSX.Element {
|
||||||
const [pageSize, setPageSize] = useState(12);
|
const [pageSize, setPageSize] = useState(12);
|
||||||
const [pageIndex, setPageIndex] = useState(1);
|
const [pageIndex, setPageIndex] = useState(1);
|
||||||
const [pageOrder, setPageOrder] = useState("Popular");
|
const [pageOrder, setPageOrder] = useState("Popular");
|
||||||
const [errorApiKey, setErrorApiKey] = useState(false);
|
|
||||||
const [tags, setTags] = useState<{ id: string; name: string }[]>([]);
|
const [tags, setTags] = useState<{ id: string; name: string }[]>([]);
|
||||||
const [tabActive, setTabActive] = useState("Flows");
|
const [tabActive, setTabActive] = useState("Flows");
|
||||||
const [searchText, setSearchText] = useState("");
|
const [searchText, setSearchText] = useState("");
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
handleGetSavedComponents();
|
|
||||||
handleGetTags();
|
handleGetTags();
|
||||||
}, []);
|
}, []);
|
||||||
|
|
||||||
|
|
@ -72,30 +66,6 @@ export default function StorePage(): JSX.Element {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
function handleGetSavedComponents() {
|
|
||||||
setLoadingSaved(true);
|
|
||||||
getStoreSavedComponents()
|
|
||||||
.then((data) => {
|
|
||||||
if (data?.authorized === false) {
|
|
||||||
setErrorApiKey(true);
|
|
||||||
setSavedFlows(new Set<string>());
|
|
||||||
} else {
|
|
||||||
let savedIds = new Set<string>();
|
|
||||||
let results = data?.results ?? [];
|
|
||||||
results.forEach((flow) => {
|
|
||||||
savedIds.add(flow.id);
|
|
||||||
});
|
|
||||||
setSavedFlows(savedIds);
|
|
||||||
setErrorApiKey(false);
|
|
||||||
setLoadingSaved(false);
|
|
||||||
}
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
setSavedFlows(new Set<string>());
|
|
||||||
setErrorApiKey(true);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
const handleGetComponents = () => {
|
const handleGetComponents = () => {
|
||||||
setLoading(true);
|
setLoading(true);
|
||||||
getStoreComponents(
|
getStoreComponents(
|
||||||
|
|
@ -112,7 +82,6 @@ export default function StorePage(): JSX.Element {
|
||||||
setLoading(false);
|
setLoading(false);
|
||||||
setSearchData(res?.results ?? []);
|
setSearchData(res?.results ?? []);
|
||||||
setTotalRowsCount(Number(res?.count ?? 0));
|
setTotalRowsCount(Number(res?.count ?? 0));
|
||||||
setErrorApiKey(true);
|
|
||||||
})
|
})
|
||||||
.catch((err) => {
|
.catch((err) => {
|
||||||
setSearchData([]);
|
setSearchData([]);
|
||||||
|
|
@ -151,7 +120,7 @@ export default function StorePage(): JSX.Element {
|
||||||
<div className="community-page-nav-button">
|
<div className="community-page-nav-button">
|
||||||
<StoreApiKeyModal
|
<StoreApiKeyModal
|
||||||
onCloseModal={() => {
|
onCloseModal={() => {
|
||||||
handleGetSavedComponents();
|
getSavedComponents();
|
||||||
handleGetTags();
|
handleGetTags();
|
||||||
handleGetComponents();
|
handleGetComponents();
|
||||||
}}
|
}}
|
||||||
|
|
|
||||||
|
|
@ -5,4 +5,7 @@ export type storeContextType = {
|
||||||
hasStore: boolean;
|
hasStore: boolean;
|
||||||
setHasApiKey: (key: boolean) => void;
|
setHasApiKey: (key: boolean) => void;
|
||||||
hasApiKey: boolean;
|
hasApiKey: boolean;
|
||||||
|
getSavedComponents: () => void;
|
||||||
|
errorApiKey: boolean;
|
||||||
|
loadingSaved: boolean;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue