Skip to content

Commit 01ad445

Browse files
committed
Improve searching by member number in new message form.
1 parent 2468cc1 commit 01ad445

File tree

2 files changed

+17
-13
lines changed

2 files changed

+17
-13
lines changed

admin/src/Components/MessageForm.tsx

Lines changed: 10 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,16 +66,6 @@ const MessageForm = ({
6666
(MemberOption | GroupOption)[]
6767
>([]);
6868
const [bodyLength, setBodyLength] = useState(message.body.length);
69-
const memberCache = React.useRef(new Map<number, Member>()).current;
70-
71-
const getMember = async (id: number): Promise<Member> => {
72-
if (memberCache.has(id)) {
73-
return memberCache.get(id)!;
74-
}
75-
const { data } = await get({ url: `/membership/member/${id}` });
76-
memberCache.set(id, data);
77-
return data;
78-
};
7969

8070
useEffect(() => {
8171
const unsubscribe = message.subscribe(() => {
@@ -97,14 +87,22 @@ const MessageForm = ({
9787
options: (MemberOption | GroupOption | CombinedOption)[],
9888
) => void,
9989
) => {
100-
const intListMatch = inputValue.match(/^(\d+[\s,]*)+$/);
90+
const intListMatch = inputValue.match(/^\s*(\d+[\s,]*)+\s*$/);
10191
if (intListMatch) {
10292
const ids = inputValue
10393
.split(/[\s,]+/)
10494
.map((v) => parseInt(v, 10))
10595
.filter((v) => !isNaN(v));
10696
if (ids.length > 0) {
107-
Promise.all(ids.map(getMember)).then((members) => {
97+
get({
98+
url: "/membership/member",
99+
params: {
100+
search: ids.join(" "),
101+
search_column: "member_number",
102+
sort_by: "member_number",
103+
sort_order: "asc",
104+
},
105+
}).then(({ data: members }: { data: Member[] }) => {
108106
const options = members.map(memberOption);
109107
callback([
110108
{

api/src/service/entity.py

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,6 +217,7 @@ def list(
217217
relation=None,
218218
related_entity_id=None,
219219
include_deleted=Arg(boolean, required=False),
220+
search_column=Arg(str, required=False),
220221
):
221222
query = db_session.query(self.model)
222223

@@ -229,9 +230,14 @@ def list(
229230
if relation and related_entity_id:
230231
query = relation.filter(query, related_entity_id)
231232

233+
if search_column is not None and search_column not in self.search_columns:
234+
raise UnprocessableEntity(f"Search column '{search_column}' not allowed. Allowed values are {self.search_columns}", fields="search_column", what=BAD_VALUE)
235+
236+
search_columns = self.search_columns if search_column is None else [search_column]
237+
232238
if search:
233239
for term in search.split():
234-
expression = or_(*[self.columns[column_name].like(f"%{term}%") for column_name in self.search_columns])
240+
expression = or_(*[self.columns[column_name].like(f"%{term}%") for column_name in search_columns])
235241
query = query.filter(expression)
236242

237243
if expand:

0 commit comments

Comments
 (0)