Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: store tags API #2678

Merged
merged 6 commits into from
Jul 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/frontend/src/controllers/API/queries/store/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./use-get-tags";
export * from "./use-post-like-component";
31 changes: 31 additions & 0 deletions src/frontend/src/controllers/API/queries/store/use-get-tags.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { useQueryFunctionType } from "@/types/api";
import { api } from "../../api";
import { getURL } from "../../helpers/constants";
import { UseRequestProcessor } from "../../services/request-processor";

interface ITagsDataArray {
id: string;
name: string;
}

type tagsQueryResponse = Array<ITagsDataArray>;

export const useGetTagsQuery: useQueryFunctionType<
undefined,
tagsQueryResponse
> = (_, options) => {
const { query } = UseRequestProcessor();

const getTagsFn = async () => {
return await api.get<tagsQueryResponse>(`${getURL("STORE")}/tags`);
};

const responseFn = async () => {
const { data } = await getTagsFn();
return data;
};

const queryResult = query(["useGetTagsQuery"], responseFn, { ...options });

return queryResult;
};
40 changes: 16 additions & 24 deletions src/frontend/src/modals/shareModal/index.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { useGetTagsQuery } from "@/controllers/API/queries/store";
import { cloneDeep } from "lodash";
import { ReactNode, useEffect, useMemo, useState } from "react";
import EditFlowSettings from "../../components/editFlowSettingsComponent";
import IconComponent from "../../components/genericIconComponent";
Expand Down Expand Up @@ -53,15 +55,13 @@ export default function ShareModal({
: useState(children ? false : true);
const [openConfirmationModal, setOpenConfirmationModal] = useState(false);
const nameComponent = is_component ? "component" : "workflow";

const [tags, setTags] = useState<{ id: string; name: string }[]>([]);
const [loadingTags, setLoadingTags] = useState<boolean>(false);
const [sharePublic, setSharePublic] = useState(true);
const [selectedTags, setSelectedTags] = useState<string[]>([]);
const [unavaliableNames, setUnavaliableNames] = useState<
{ id: string; name: string }[]
>([]);
const saveFlow = useFlowsManagerStore((state) => state.saveFlow);
const { data, isFetching } = useGetTagsQuery();

const [loadingNames, setLoadingNames] = useState(false);

Expand All @@ -71,20 +71,11 @@ export default function ShareModal({
useEffect(() => {
if (internalOpen) {
if (hasApiKey && hasStore) {
handleGetTags();
handleGetNames();
}
}
}, [internalOpen, hasApiKey, hasStore]);

function handleGetTags() {
setLoadingTags(true);
getStoreTags().then((res) => {
setTags(res);
setLoadingTags(false);
});
}

async function handleGetNames() {
setLoadingNames(true);
const unavaliableNames: Array<{ id: string; name: string }> = [];
Expand Down Expand Up @@ -124,19 +115,20 @@ export default function ShareModal({
}

if (!update)
saveFlowStore(flow!, getTagsIds(selectedTags, tags), sharePublic).then(
successShare,
(err) => {
setErrorData({
title: "Error sharing " + (is_component ? "component" : "flow"),
list: [err["response"]["data"]["detail"]],
});
},
);
saveFlowStore(
flow!,
getTagsIds(selectedTags, cloneDeep(data) ?? []),
sharePublic,
).then(successShare, (err) => {
setErrorData({
title: "Error sharing " + (is_component ? "component" : "flow"),
list: [err["response"]["data"]["detail"]],
});
});
else
updateFlowStore(
flow!,
getTagsIds(selectedTags, tags),
getTagsIds(selectedTags, cloneDeep(data) ?? []),
sharePublic,
unavaliableNames.find((e) => e.name === name)!.id,
).then(successShare, (err) => {
Expand Down Expand Up @@ -237,8 +229,8 @@ export default function ShareModal({
</div>
<div className="mt-3 flex h-8 w-full">
<TagsSelector
tags={tags}
loadingTags={loadingTags}
tags={data ?? []}
loadingTags={isFetching}
disabled={false}
selectedTags={selectedTags}
setSelectedTags={setSelectedTags}
Expand Down
22 changes: 4 additions & 18 deletions src/frontend/src/pages/StorePage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import ShadTooltip from "../../components/shadTooltipComponent";
import { SkeletonCardComponent } from "../../components/skeletonCardComponent";
import { Button } from "../../components/ui/button";

import { useGetTagsQuery } from "@/controllers/API/queries/store";
import { Link, useNavigate, useParams } from "react-router-dom";
import PaginatorComponent from "../../components/paginatorComponent";
import { TagsSelector } from "../../components/tagsSelectorComponent";
Expand Down Expand Up @@ -50,7 +51,6 @@ export default function StorePage(): JSX.Element {
);
const currentFlowId = useFlowsManagerStore((state) => state.currentFlowId);
const [loading, setLoading] = useState(true);
const [loadingTags, setLoadingTags] = useState(true);
const { id } = useParams();
const [filteredCategories, setFilterCategories] = useState<any[]>([]);
const [inputText, setInputText] = useState<string>("");
Expand All @@ -59,10 +59,10 @@ export default function StorePage(): JSX.Element {
const [pageSize, setPageSize] = useState(12);
const [pageIndex, setPageIndex] = useState(1);
const [pageOrder, setPageOrder] = useState("Popular");
const [tags, setTags] = useState<{ id: string; name: string }[]>([]);
const [tabActive, setTabActive] = useState("All");
const [searchNow, setSearchNow] = useState("");
const [selectFilter, setSelectFilter] = useState("all");
const { isFetching, data } = useGetTagsQuery();

const navigate = useNavigate();

Expand All @@ -84,7 +84,6 @@ export default function StorePage(): JSX.Element {
}, [loadingApiKey, validApiKey, hasApiKey, currentFlowId]);

useEffect(() => {
handleGetTags();
handleGetComponents();
}, [
tabActive,
Expand All @@ -101,19 +100,6 @@ export default function StorePage(): JSX.Element {
id,
]);

function handleGetTags() {
setLoadingTags(true);
getStoreTags()
.then((res) => {
setTags(res);
setLoadingTags(false);
})
.catch((err) => {
console.log(err);
setLoadingTags(false);
});
}

function handleGetComponents() {
if (loadingApiKey) return;
setLoading(true);
Expand Down Expand Up @@ -297,8 +283,8 @@ export default function StorePage(): JSX.Element {
</Select>
{id === undefined ? (
<TagsSelector
tags={tags}
loadingTags={loadingTags}
tags={data ?? []}
loadingTags={isFetching}
disabled={loading}
selectedTags={filteredCategories}
setSelectedTags={setFilterCategories}
Expand Down
Loading