1
- import React , { useState } from "react" ;
1
+ import React , { useEffect , useMemo , useState } from "react" ;
2
2
import { Controller , useForm } from "react-hook-form" ;
3
3
import { useNavigate } from "react-router-dom" ;
4
4
import {
@@ -31,7 +31,10 @@ import useFunctionStore from "../../../store";
31
31
import { TFunctionTemplate , TMethod } from "@/apis/typing" ;
32
32
import FunctionTemplate from "@/pages/functionTemplate" ;
33
33
import TemplateCard from "@/pages/functionTemplate/Mods/TemplateCard" ;
34
- import { useGetRecommendFunctionTemplatesQuery } from "@/pages/functionTemplate/service" ;
34
+ import {
35
+ useGetFunctionTemplatesQuery ,
36
+ useGetRecommendFunctionTemplatesQuery ,
37
+ } from "@/pages/functionTemplate/service" ;
35
38
import useTemplateStore from "@/pages/functionTemplate/store" ;
36
39
import useGlobalStore from "@/pages/globalStore" ;
37
40
@@ -54,6 +57,13 @@ const CreateModal = (props: {
54
57
useFunctionStore ( ) ;
55
58
const { setShowTemplateItem } = useTemplateStore ( ) ;
56
59
60
+ useEffect ( ( ) => {
61
+ const searchParams = new URLSearchParams ( window . location . search ) ;
62
+ const templateId = searchParams . get ( "templateId" ) ;
63
+ if ( ! templateId ) return ;
64
+ localStorage . setItem ( "templateId" , templateId ) ;
65
+ } , [ ] ) ;
66
+
57
67
const defaultValues = {
58
68
name : functionItem ?. name || "" ,
59
69
description : functionItem ?. desc || "" ,
@@ -100,6 +110,31 @@ const CreateModal = (props: {
100
110
enabled : ! isOpen && ! isEdit ,
101
111
} ,
102
112
) ;
113
+ const searchedTemplateList = useGetFunctionTemplatesQuery (
114
+ {
115
+ page : 1 ,
116
+ pageSize : 3 ,
117
+ keyword : localStorage . getItem ( "templateId" ) || "" ,
118
+ type : "default" ,
119
+ asc : 1 ,
120
+ sort : null ,
121
+ } ,
122
+ {
123
+ enabled : ! ! localStorage . getItem ( "templateId" ) && ! isEdit ,
124
+ } ,
125
+ ) ;
126
+
127
+ const recommendedList = useMemo (
128
+ ( ) => InitialTemplateList . data ?. data . list || [ ] ,
129
+ [ InitialTemplateList ] ,
130
+ ) ;
131
+ const searchedList = useMemo (
132
+ ( ) => searchedTemplateList . data ?. data . list || [ ] ,
133
+ [ searchedTemplateList ] ,
134
+ ) ;
135
+ const showedList = useMemo ( ( ) => {
136
+ return [ ...searchedList , ...recommendedList ] . slice ( 0 , 3 ) ;
137
+ } , [ searchedList , recommendedList ] ) ;
103
138
104
139
const onSubmit = async ( data : any ) => {
105
140
let res : any = { } ;
@@ -226,7 +261,6 @@ const CreateModal = (props: {
226
261
/>
227
262
</ div >
228
263
</ FormControl >
229
-
230
264
< Button
231
265
type = "submit"
232
266
onClick = { handleSubmit ( onSubmit ) }
@@ -246,7 +280,7 @@ const CreateModal = (props: {
246
280
{ t ( "Template.Recommended" ) }
247
281
</ div >
248
282
< div className = "mb-11 flex w-full" >
249
- { InitialTemplateList . data ?. data . list . map ( ( item : TFunctionTemplate ) => (
283
+ { showedList . map ( ( item : TFunctionTemplate ) => (
250
284
< section
251
285
className = "h-28 w-1/3 px-1.5 py-1"
252
286
key = { item . _id }
0 commit comments