Skip to content

Commit 4e8346b

Browse files
oliwiergawlikpsrok1KWMORALE
authored
Optional result counting in Search (#718)
Co-authored-by: Paweł Srokosz <pawel.srokosz@cert.pl> Co-authored-by: krzysztof.wielocha@nask.pl <krzysztof.wielocha@nask.pl>
1 parent 18a5479 commit 4e8346b

File tree

2 files changed

+130
-49
lines changed

2 files changed

+130
-49
lines changed

mwdb/web/src/components/RecentView/RecentView.js

Lines changed: 129 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
import React, { useState, useEffect, useContext } from "react";
1+
import React, { useCallback, useContext, useEffect, useState } from "react";
22
import { useSearchParams } from "react-router-dom";
33

44
import { APIContext } from "@mwdb-web/commons/api/context";
5-
import { multiFromHashes, addFieldToQuery } from "@mwdb-web/commons/helpers";
5+
import { addFieldToQuery, multiFromHashes } from "@mwdb-web/commons/helpers";
66
import { View } from "@mwdb-web/commons/ui";
77

88
import RecentViewList from "./RecentViewList";
@@ -31,61 +31,108 @@ export default function RecentView(props) {
3131
// Query error shown under the query bar
3232
const [queryError, setQueryError] = useState(null);
3333
const [objectCount, setObjectCount] = useState(null);
34-
34+
// const [countingEnabled, setCountingEnabled] = useState(true);
35+
const countingEnabled = searchParams.get("count") === "1" ? 1 : 0;
3536
const isLocked = !queryError && submittedQuery !== currentQuery;
3637

3738
function resetErrors() {
3839
setError(null);
3940
setQueryError(null);
4041
}
4142

42-
function setCurrentQuery(query) {
43-
// If query is already submitted: do nothing
44-
if (query === submittedQuery) return;
45-
// Optionally convert query if only hash or hashes were provided
46-
query = multiFromHashes(query);
47-
// Set query in URL (currentQuery)
48-
setSearchParams({ q: query });
49-
}
43+
const setCurrentQuery = useCallback(
44+
({ query, enableCounting = countingEnabled }) => {
45+
// Optionally convert query if only hash or hashes were provided
46+
query = multiFromHashes(query);
47+
// Set query in URL (currentQuery, countingEnabled)
48+
setSearchParams({ q: query, count: enableCounting });
49+
},
50+
[countingEnabled, setSearchParams]
51+
);
5052

51-
const addToQuery = (field, value) => {
52-
return setCurrentQuery(addFieldToQuery(submittedQuery, field, value));
53-
};
53+
const addToQuery = useCallback(
54+
(field, value) => {
55+
return setCurrentQuery({
56+
query: addFieldToQuery(submittedQuery, field, value),
57+
});
58+
},
59+
[submittedQuery, setCurrentQuery]
60+
);
5461

5562
// Synchronize input if currentQuery was changed
5663
useEffect(() => {
5764
setQueryInput(currentQuery);
5865
resetErrors();
5966
}, [currentQuery]);
6067

61-
// Submit query if currentQuery was changed
62-
useEffect(() => {
63-
let cancelled = false;
64-
// If query is already submitted: do nothing
65-
if (submittedQuery === currentQuery) return;
66-
// If query is empty, submit immediately
67-
if (!currentQuery) setSubmittedQuery("");
68-
else {
68+
const submitQueryWithoutCount = useCallback(
69+
(query) => {
70+
let cancelled = false;
71+
// Only check if query is correct
72+
api.getObjectList(props.type, "", query)
73+
.then(() => {
74+
if (cancelled) return;
75+
// If ok: commit query
76+
setSubmittedQuery(query);
77+
})
78+
.catch((error) => {
79+
if (cancelled) return;
80+
setQueryError(error);
81+
});
82+
return () => {
83+
cancelled = true;
84+
};
85+
},
86+
[api, props.type]
87+
);
88+
89+
const submitQueryWithCount = useCallback(
90+
(query) => {
91+
let cancelled = false;
6992
// Make preflight query to get count of results
7093
// and check if query is correct
71-
api.getObjectCount(props.type, currentQuery)
94+
// First nullify the current count
95+
setObjectCount(null);
96+
api.getObjectCount(props.type, query)
7297
.then((response) => {
7398
if (cancelled) return;
7499
// If ok: commit query
75-
setSubmittedQuery(currentQuery);
100+
setSubmittedQuery(query);
76101
setObjectCount(response.data["count"]);
77102
})
78103
.catch((error) => {
79104
if (cancelled) return;
80105
setQueryError(error);
81106
});
82-
}
83-
return () => {
84-
// Cancel pending requests after unmount/remount
85-
cancelled = true;
86-
};
87-
// eslint-disable-next-line react-hooks/exhaustive-deps
88-
}, [currentQuery]);
107+
108+
return () => {
109+
cancelled = true;
110+
};
111+
},
112+
[api, props.type]
113+
);
114+
115+
const submitQuery = useCallback(
116+
(query) => {
117+
setCurrentQuery({
118+
query,
119+
});
120+
return countingEnabled
121+
? submitQueryWithCount(query)
122+
: submitQueryWithoutCount(query);
123+
},
124+
[
125+
countingEnabled,
126+
setCurrentQuery,
127+
submitQueryWithCount,
128+
submitQueryWithoutCount,
129+
]
130+
);
131+
132+
useEffect(() => {
133+
if (!currentQuery) setSubmittedQuery("");
134+
return submitQuery(currentQuery);
135+
}, [currentQuery, submitQuery]);
89136

90137
const canAddQuickQuery =
91138
queryInput && !isLocked && queryInput === submittedQuery;
@@ -100,23 +147,30 @@ export default function RecentView(props) {
100147
[]
101148
);
102149

103-
const objectCountMessage =
104-
submittedQuery && objectCount !== null ? (
105-
<div className="form-hint">
106-
{objectCount}
107-
{" results found"}
108-
</div>
109-
) : (
110-
[]
111-
);
150+
let objectCountMessage = [];
151+
if (submittedQuery && countingEnabled) {
152+
if (objectCount === null) {
153+
objectCountMessage = <div className="form-hint">Counting...</div>;
154+
} else {
155+
objectCountMessage = (
156+
<div className="form-hint">
157+
{objectCount}
158+
{" results found"}
159+
</div>
160+
);
161+
}
162+
}
163+
112164
return (
113165
<View fluid ident="recentObjects" error={error}>
114166
<div className="table-responsive">
115167
<form
116168
className="searchForm"
117169
onSubmit={(ev) => {
118170
ev.preventDefault();
119-
setCurrentQuery(queryInput);
171+
setCurrentQuery({
172+
query: queryInput,
173+
});
120174
}}
121175
>
122176
<div className="input-group">
@@ -127,7 +181,9 @@ export default function RecentView(props) {
127181
value="X"
128182
onClick={(ev) => {
129183
ev.preventDefault();
130-
setCurrentQuery("");
184+
setCurrentQuery({
185+
query: "",
186+
});
131187
}}
132188
/>
133189
</div>
@@ -140,11 +196,31 @@ export default function RecentView(props) {
140196
onChange={(evt) => setQueryInput(evt.target.value)}
141197
/>
142198
<div className="input-group-append">
143-
<input
144-
className="btn btn-outline-success"
145-
type="submit"
146-
value="Search"
147-
/>
199+
<div className="btn-group">
200+
<input
201+
className="btn btn-outline-success rounded-0"
202+
type="submit"
203+
value="Search"
204+
/>
205+
</div>
206+
<div className="btn-group">
207+
<input
208+
type="submit"
209+
className={`btn btn-outline-info rounded-0 ${
210+
searchParams.get("count") === "1"
211+
? "active"
212+
: ""
213+
}`}
214+
value="Count"
215+
onClick={() => {
216+
setSearchParams({
217+
q: currentQuery,
218+
count: countingEnabled ? "0" : "1",
219+
});
220+
}}
221+
/>
222+
</div>
223+
148224
<a
149225
href="https://mwdb.readthedocs.io/en/latest/user-guide/7-Lucene-search.html"
150226
className="btn btn-outline-primary"
@@ -159,7 +235,11 @@ export default function RecentView(props) {
159235
type={props.type}
160236
query={submittedQuery}
161237
canAddQuickQuery={canAddQuickQuery}
162-
submitQuery={(q) => setCurrentQuery(q)}
238+
submitQuery={(query) =>
239+
setCurrentQuery({
240+
query,
241+
})
242+
}
163243
addToQuery={addToQuery}
164244
setQueryError={setQueryError}
165245
/>
@@ -173,6 +253,7 @@ export default function RecentView(props) {
173253
locked={isLocked}
174254
disallowEmpty={props.disallowEmpty}
175255
setError={setError}
256+
setQueryError={setQueryError}
176257
addToQuery={addToQuery}
177258
/>
178259
</div>

mwdb/web/src/components/RecentView/RecentViewList.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ export default function RecentViewList(props) {
7979
})
8080
.catch((error) => {
8181
if (cancelled) return;
82-
props.setError(error);
82+
props.setQueryError(error);
8383
listDispatch({
8484
type: "pageLoaded",
8585
elements: [],

0 commit comments

Comments
 (0)