-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathactions.ts
More file actions
60 lines (57 loc) · 1.66 KB
/
actions.ts
File metadata and controls
60 lines (57 loc) · 1.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
/**
* Async actions that orchestrate services, adapters, and the store.
*/
import { useTabbedListStore } from "./store";
import type { FilterSpec, SortSpec } from "./types";
import { fetchProperties } from "./services";
import { toPropertySummary } from "./adapters";
/**
* Load property list with current store params, override via args.
*/
export async function loadProperties(opts?: {
page?: number;
pageSize?: number;
sort?: SortSpec | null;
filters?: Partial<FilterSpec>;
}) {
const s = useTabbedListStore.getState();
s.setLoading(true);
s.setError(null);
try {
if (opts?.filters) s.setFilters(opts.filters);
if (
typeof opts?.page !== "undefined" ||
typeof opts?.pageSize !== "undefined"
) {
s.setPagination({
...s.pagination,
page: opts.page ?? s.pagination.page,
pageSize: opts.pageSize ?? s.pagination.pageSize,
total: s.pagination.total,
});
}
if (typeof opts?.sort !== "undefined") s.setSort(opts.sort ?? null);
const { items, pagination } = await fetchProperties({
page: s.pagination.page,
pageSize: s.pagination.pageSize,
sort: s.sort,
filters: s.filters,
});
const mapped = items.map(toPropertySummary);
s.setItems(mapped);
s.setPagination({ ...pagination, total: pagination.total });
} catch (e: unknown) {
const errorMessage = e instanceof Error ? e.message : "Failed to load properties";
s.setError(errorMessage);
} finally {
s.setLoading(false);
}
}
/**
* Example action: open drawer for a given id if present in items.
*/
export function openDrawerById(id: string) {
const s = useTabbedListStore.getState();
const item = s.items.find((i) => i.id === id) || null;
if (item) s.openDrawer(item);
}