Skip to content

Commit

Permalink
Merge pull request #838 from rainlanguage/2024-09-06-active-order-filter
Browse files Browse the repository at this point in the history
  • Loading branch information
findolor authored Sep 8, 2024
2 parents d49df2a + 6ef0f42 commit 6be7f1f
Show file tree
Hide file tree
Showing 11 changed files with 94 additions and 16 deletions.
3 changes: 3 additions & 0 deletions crates/cli/src/commands/order/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ mod tests {
},
filter_args: CliFilterArgs {
owners: vec!["addr1".to_string()],
active: Some(true),
},
};

Expand All @@ -146,6 +147,7 @@ mod tests {
},
filter_args: CliFilterArgs {
owners: vec!["addr1".to_string()],
active: Some(true),
},
};

Expand All @@ -172,6 +174,7 @@ mod tests {
},
filter_args: CliFilterArgs {
owners: vec!["addr1".to_string()],
active: Some(true),
},
};

Expand Down
3 changes: 3 additions & 0 deletions crates/cli/src/commands/vault/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,7 @@ mod tests {
},
filter_args: CliFilterArgs {
owners: vec!["addr1".to_string()],
active: Some(true),
},
};

Expand All @@ -145,6 +146,7 @@ mod tests {
},
filter_args: CliFilterArgs {
owners: vec!["addr1".to_string()],
active: Some(true),
},
};

Expand All @@ -165,6 +167,7 @@ mod tests {
},
filter_args: CliFilterArgs {
owners: vec!["addr1".to_string()],
active: Some(true),
},
};

Expand Down
4 changes: 4 additions & 0 deletions crates/cli/src/subgraph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,16 @@ pub struct CliFilterArgs {
value_delimiter = ','
)]
pub owners: Vec<String>,

#[arg(long, help = "Filter orders by active status", default_value = "true")]
pub active: Option<bool>,
}

impl From<CliFilterArgs> for OrdersListFilterArgs {
fn from(val: CliFilterArgs) -> Self {
Self {
owners: val.owners.into_iter().map(Bytes).collect(),
active: val.active,
}
}
}
Expand Down
22 changes: 14 additions & 8 deletions crates/subgraph/src/orderbook_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,19 @@ impl OrderbookSubgraphClient {
) -> Result<Vec<Order>, OrderbookSubgraphClientError> {
let pagination_variables = Self::parse_pagination_args(pagination_args);

let filters = if !filter_args.owners.is_empty() || filter_args.active.is_some() {
Some(OrdersListQueryFilters {
owner_in: filter_args.owners,
active: filter_args.active,
})
} else {
None
};

let variables = OrdersListQueryVariables {
first: pagination_variables.first,
skip: pagination_variables.skip,
filters: if filter_args.owners.is_empty() {
None
} else {
Some(OrdersListQueryFilters {
owner_in: filter_args.owners,
})
},
filters,
};

let data = self
Expand All @@ -100,7 +103,10 @@ impl OrderbookSubgraphClient {
loop {
let page_data = self
.orders_list(
OrdersListFilterArgs { owners: vec![] },
OrdersListFilterArgs {
owners: vec![],
active: None,
},
PaginationArgs {
page,
page_size: ALL_PAGES_QUERY_PAGE_SIZE,
Expand Down
5 changes: 4 additions & 1 deletion crates/subgraph/src/types/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ pub struct IdQueryVariables<'a> {
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct OrdersListFilterArgs {
pub owners: Vec<Bytes>,
pub active: Option<bool>,
}

#[derive(cynic::QueryVariables, Debug, Clone)]
Expand All @@ -24,8 +25,10 @@ pub struct PaginationQueryVariables {
#[cynic(graphql_type = "Order_filter")]
#[typeshare]
pub struct OrdersListQueryFilters {
#[cynic(rename = "owner_in")]
#[cynic(rename = "owner_in", skip_serializing_if = "Vec::is_empty")]
pub owner_in: Vec<Bytes>,
#[cynic(rename = "active", skip_serializing_if = "Option::is_none")]
pub active: Option<bool>,
}

#[derive(cynic::QueryVariables, Debug, Clone)]
Expand Down
2 changes: 2 additions & 0 deletions tauri-app/src/lib/components/ListViewOrderbookSelector.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import DropdownActiveNetwork from './DropdownActiveNetwork.svelte';
import DropdownActiveOrderbook from './DropdownActiveOrderbook.svelte';
import DropdownOrderListWatchlist from './dropdown/DropdownOrderListWatchlist.svelte';
import DropdownOrderStatus from './dropdown/DropdownOrderStatus.svelte';
import { settings } from '$lib/stores/settings';
import { Alert } from 'flowbite-svelte';
</script>
Expand All @@ -13,6 +14,7 @@
>No networks added to <a class="underline" href="/settings">settings</a></Alert
>
{:else}
<DropdownOrderStatus />
<DropdownOrderListWatchlist />
<DropdownActiveNetwork />
<DropdownActiveOrderbook />
Expand Down
8 changes: 6 additions & 2 deletions tauri-app/src/lib/components/dropdown/DropdownCheckbox.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,9 @@
export let value: Record<string, string> = {};
export let label: string = 'Select items';
export let allLabel: string = 'All items';
export let showAllLabel: boolean = true;
export let emptyMessage: string = 'No items available';
export let onlyTitle: boolean = false;
$: selectedCount = Object.keys(value).length;
$: allSelected = selectedCount === Object.keys(options).length;
Expand Down Expand Up @@ -57,7 +59,7 @@
<Dropdown class="w-full min-w-72 py-0" data-testid="dropdown-checkbox">
{#if isEmpty(options)}
<div class="ml-2 w-full rounded-lg p-3">{emptyMessage}</div>
{:else if Object.keys(options).length > 1}
{:else if Object.keys(options).length > 1 && showAllLabel}
<Checkbox
data-testid="dropdown-checkbox-option"
class="w-full rounded-lg p-3 hover:bg-gray-100 dark:hover:bg-gray-600"
Expand All @@ -77,7 +79,9 @@
>
<div class="ml-2">
<div class="text-sm font-medium">{key}</div>
<div class="text-xs text-gray-500">{optionValue}</div>
{#if !onlyTitle}
<div class="text-xs text-gray-500">{optionValue}</div>
{/if}
</div>
</Checkbox>
{/each}
Expand Down
32 changes: 32 additions & 0 deletions tauri-app/src/lib/components/dropdown/DropdownOrderStatus.svelte
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<script lang="ts">
import DropdownCheckbox from './DropdownCheckbox.svelte';
import { activeOrderStatus } from '$lib/stores/settings';
const orderStatusOptions = {
Active: 'active',
Inactive: 'inactive',
};
function handleStatusChange(event: CustomEvent<Record<string, string>>) {
let status: boolean | undefined = undefined;
let items = Object.keys(event.detail);
if (items.length === 0 || items.length === 2) {
status = undefined;
} else if (items.includes('Active')) {
status = true;
} else if (items.includes('Inactive')) {
status = false;
}
activeOrderStatus.set(status);
}
</script>

<DropdownCheckbox
options={orderStatusOptions}
on:change={handleStatusChange}
label="Status"
showAllLabel={false}
onlyTitle={true}
/>
11 changes: 8 additions & 3 deletions tauri-app/src/lib/components/tables/OrdersListTable.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,18 @@
import { subgraphUrl } from '$lib/stores/settings';
import { formatTimestampSecondsAsLocal } from '$lib/utils/time';
import { handleOrderRemoveModal } from '$lib/services/modal';
import { activeWatchlist } from '$lib/stores/settings';
import { activeWatchlist, activeOrderStatus } from '$lib/stores/settings';
import { get } from 'svelte/store';
$: query = createInfiniteQuery({
queryKey: [QKEY_ORDERS, $activeWatchlist],
queryKey: [QKEY_ORDERS, $activeWatchlist, $activeOrderStatus],
queryFn: ({ pageParam }) => {
return ordersList($subgraphUrl, Object.values(get(activeWatchlist)), pageParam);
return ordersList(
$subgraphUrl,
Object.values(get(activeWatchlist)),
$activeOrderStatus,
pageParam,
);
},
initialPageParam: 0,
getNextPageParam(lastPage, _allPages, lastPageParam) {
Expand Down
6 changes: 4 additions & 2 deletions tauri-app/src/lib/queries/ordersList.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export type OrdersListArgs = {
export const ordersList = async (
url: string | undefined,
owners: string[] = [],
active: boolean | undefined = undefined,
pageParam: number,
pageSize: number = DEFAULT_PAGE_SIZE,
) => {
Expand All @@ -26,6 +27,7 @@ export const ordersList = async (
subgraphArgs: { url },
filterArgs: {
owners,
active,
},
paginationArgs: { page: pageParam + 1, page_size: pageSize },
} as OrdersListArgs);
Expand Down Expand Up @@ -54,10 +56,10 @@ if (import.meta.vitest) {
});

// check for a result with no URL
expect(await ordersList(undefined, [], 0)).toEqual([]);
expect(await ordersList(undefined, [], undefined, 0)).toEqual([]);

// check for a result with a URL
expect(await ordersList('http://localhost:8000', [], 0)).toEqual([
expect(await ordersList('http://localhost:8000', [], undefined, 0)).toEqual([
{
id: '1',
order_bytes: '0x123',
Expand Down
14 changes: 14 additions & 0 deletions tauri-app/src/lib/stores/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,17 @@ export async function resetActiveNetworkRef() {
activeNetworkRef.set(undefined);
}
}

export const activeOrderStatus = cachedWritableStore<boolean | undefined>(
'settings.activeOrderStatus',
undefined,
(value) => JSON.stringify(value),
(str) => {
try {
const parsed = JSON.parse(str);
return typeof parsed === 'boolean' ? parsed : undefined;
} catch {
return undefined;
}
},
);

0 comments on commit 6be7f1f

Please sign in to comment.