Skip to content

Commit

Permalink
feat(web): recommend function template config with templateid (#1947)
Browse files Browse the repository at this point in the history
* feat(web): recommend function template config with templateid
  • Loading branch information
newfish-cmyk committed Apr 25, 2024
1 parent 9743063 commit f70a7be
Showing 1 changed file with 38 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState } from "react";
import React, { useEffect, useMemo, useState } from "react";
import { Controller, useForm } from "react-hook-form";
import { useNavigate } from "react-router-dom";
import {
Expand Down Expand Up @@ -31,7 +31,10 @@ import useFunctionStore from "../../../store";
import { TFunctionTemplate, TMethod } from "@/apis/typing";
import FunctionTemplate from "@/pages/functionTemplate";
import TemplateCard from "@/pages/functionTemplate/Mods/TemplateCard";
import { useGetRecommendFunctionTemplatesQuery } from "@/pages/functionTemplate/service";
import {
useGetFunctionTemplatesQuery,
useGetRecommendFunctionTemplatesQuery,
} from "@/pages/functionTemplate/service";
import useTemplateStore from "@/pages/functionTemplate/store";
import useGlobalStore from "@/pages/globalStore";

Expand All @@ -54,6 +57,13 @@ const CreateModal = (props: {
useFunctionStore();
const { setShowTemplateItem } = useTemplateStore();

useEffect(() => {
const searchParams = new URLSearchParams(window.location.search);
const templateId = searchParams.get("templateId");
if (!templateId) return;
localStorage.setItem("templateId", templateId);
}, []);

const defaultValues = {
name: functionItem?.name || "",
description: functionItem?.desc || "",
Expand Down Expand Up @@ -100,6 +110,31 @@ const CreateModal = (props: {
enabled: !isOpen && !isEdit,
},
);
const searchedTemplateList = useGetFunctionTemplatesQuery(
{
page: 1,
pageSize: 3,
keyword: localStorage.getItem("templateId") || "",
type: "default",
asc: 1,
sort: null,
},
{
enabled: !!localStorage.getItem("templateId") && !isEdit,
},
);

const recommendedList = useMemo(
() => InitialTemplateList.data?.data.list || [],
[InitialTemplateList],
);
const searchedList = useMemo(
() => searchedTemplateList.data?.data.list || [],
[searchedTemplateList],
);
const showedList = useMemo(() => {
return [...searchedList, ...recommendedList].slice(0, 3);
}, [searchedList, recommendedList]);

const onSubmit = async (data: any) => {
let res: any = {};
Expand Down Expand Up @@ -226,7 +261,6 @@ const CreateModal = (props: {
/>
</div>
</FormControl>

<Button
type="submit"
onClick={handleSubmit(onSubmit)}
Expand All @@ -246,7 +280,7 @@ const CreateModal = (props: {
{t("Template.Recommended")}
</div>
<div className="mb-11 flex w-full">
{InitialTemplateList.data?.data.list.map((item: TFunctionTemplate) => (
{showedList.map((item: TFunctionTemplate) => (
<section
className="h-28 w-1/3 px-1.5 py-1"
key={item._id}
Expand Down

0 comments on commit f70a7be

Please sign in to comment.