Skip to content

Commit 69c3358

Browse files
committed
Add default search params and refactor App.tsx
Introduces default search parameters (query, rows, page, sort) in .env and updates App.tsx to use these values. Refactors parameter handling and fetch logic to improve configurability and maintainability.
1 parent 629e6a9 commit 69c3358

File tree

2 files changed

+33
-21
lines changed

2 files changed

+33
-21
lines changed

turing-js-sdk/js-sdk-sample/.env

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
VITE_API_URL: 'http://localhost:2700'
22
VITE_SN_SITE: 'wknd-author'
3+
VITE_SN_DEFAULT_QUERY: '*'
4+
VITE_SN_DEFAULT_ROWS: 10
5+
VITE_SN_DEFAULT_PAGE: 1
6+
VITE_SN_DEFAULT_SORT: 'relevance'
37
VITE_LOCALE: 'en-US'

turing-js-sdk/js-sdk-sample/src/App.tsx

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,42 @@ import { TurSNSiteSearchService, type TurSNSiteSearchDocument } from "@openvigle
22
import { useEffect, useState } from "react";
33
import { useSearchParams } from "react-router-dom";
44

5+
const QUERY_PARAM = "q";
6+
7+
const ROWS_PARAM = "rows";
8+
const PAGE_PARAM = "p";
9+
const SORT_PARAM = "sort";
510
function App() {
611
const [totalDocuments, setTotalDocuments] = useState(0);
712
const [searchDocuments, setSearchDocuments] = useState<TurSNSiteSearchDocument[]>([]);
813
const [loading, setLoading] = useState(false);
914
const [error, setError] = useState<string | null>(null);
1015
const [searchParams] = useSearchParams();
11-
const query = searchParams.get("q") || "*";
12-
16+
const query = searchParams.get(QUERY_PARAM) || import.meta.env.VITE_SN_DEFAULT_QUERY;
17+
const rows = searchParams.get(ROWS_PARAM) || import.meta.env.VITE_SN_DEFAULT_ROWS;
18+
const currentPage = searchParams.get(PAGE_PARAM) || import.meta.env.VITE_SN_DEFAULT_PAGE;
19+
const sort = searchParams.get(SORT_PARAM) || import.meta.env.VITE_SN_DEFAULT_PAGE;
20+
const fetchDocuments = async () => {
21+
setLoading(true);
22+
setError(null);
23+
try {
24+
const searchService = new TurSNSiteSearchService(import.meta.env.VITE_API_URL);
25+
const res = await searchService.search(import.meta.env.VITE_SN_SITE, {
26+
q: query,
27+
rows: rows,
28+
currentPage: currentPage,
29+
sort: sort,
30+
localeRequest: import.meta.env.VITE_LOCALE,
31+
});
32+
setSearchDocuments(res.results?.document || []);
33+
setTotalDocuments(res.queryContext?.count || 0);
34+
} catch (err: any) {
35+
setError("Search failed: " + (err?.message || "Unknown error"));
36+
} finally {
37+
setLoading(false);
38+
}
39+
};
1340
useEffect(() => {
14-
const fetchDocuments = async () => {
15-
setLoading(true);
16-
setError(null);
17-
try {
18-
const searchService = new TurSNSiteSearchService(import.meta.env.VITE_API_URL);
19-
const res = await searchService.search(import.meta.env.VITE_SN_SITE, {
20-
q: query,
21-
rows: 10,
22-
currentPage: 1,
23-
localeRequest: import.meta.env.VITE_LOCALE,
24-
});
25-
setSearchDocuments(res.results?.document || []);
26-
setTotalDocuments(res.queryContext?.count || 0);
27-
} catch (err: any) {
28-
setError("Search failed: " + (err?.message || "Unknown error"));
29-
} finally {
30-
setLoading(false);
31-
}
32-
};
3341
fetchDocuments();
3442
}, [query]);
3543

0 commit comments

Comments
 (0)