Skip to content

Commit 3b4f4fe

Browse files
committed
group dual properties
1 parent e040eaa commit 3b4f4fe

File tree

4 files changed

+111
-23
lines changed

4 files changed

+111
-23
lines changed

src/routes/category-properties/+page.server.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,41 @@ import { error } from '@sveltejs/kit'
44
import sql from 'sql-template-tag'
55

66
export const load = async () => {
7-
const { rows: properties, err } = await query<PropertyShort>(sql`
8-
SELECT id, relation FROM properties
7+
const { rows: properties, err } = await query<
8+
PropertyShort & { dual_property_id?: string }
9+
>(sql`
10+
SELECT id, relation, dual_property_id FROM properties
911
ORDER BY lower(id)
1012
`)
1113

1214
if (err) error(500, 'Could not load properties')
1315

14-
return { properties }
16+
const seen = new Set()
17+
18+
const grouped_properties: typeof properties = []
19+
20+
for (const p of properties) {
21+
if (seen.has(p.id) || (p.dual_property_id && seen.has(p.dual_property_id))) {
22+
continue
23+
}
24+
25+
if (p.id.startsWith('co') && p.dual_property_id) {
26+
const swap = {
27+
id: p.dual_property_id,
28+
dual_property_id: p.id,
29+
relation: p.relation,
30+
}
31+
grouped_properties.push(swap)
32+
} else {
33+
grouped_properties.push(p)
34+
}
35+
36+
seen.add(p.id)
37+
if (p.dual_property_id) seen.add(p.dual_property_id)
38+
}
39+
40+
const total = properties.length
41+
const grouped_total = grouped_properties.length
42+
43+
return { grouped_properties, total, grouped_total }
1544
}
Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,24 @@
11
<script lang="ts">
22
import MetaData from '$components/MetaData.svelte'
3-
import PropertyList from '$components/PropertyList.svelte'
43
import SearchFilter from '$components/SearchFilter.svelte'
54
import SuggestionForm from '$components/SuggestionForm.svelte'
65
import { pluralize } from '$lib/client/utils'
6+
import { get_property_url } from '$lib/commons/property.url.js'
77
88
let { data } = $props()
99
1010
let search = $state('')
1111
1212
let searched_properties = $derived(
1313
search
14-
? data.properties.filter((property) =>
15-
property.id.toLowerCase().includes(search.toLowerCase()),
14+
? data.grouped_properties.filter(
15+
(property) =>
16+
property.id.toLowerCase().includes(search.toLowerCase()) ||
17+
property.dual_property_id
18+
?.toLowerCase()
19+
.includes(search.toLowerCase()),
1620
)
17-
: data.properties,
21+
: data.grouped_properties,
1822
)
1923
</script>
2024

@@ -25,12 +29,28 @@
2529
<SearchFilter bind:search />
2630

2731
<p class="hint">
28-
{pluralize(searched_properties.length, {
29-
one: 'Found {count} property',
30-
other: 'Found {count} properties',
31-
})}
32+
{#if !search}
33+
Found {data.total} properties ({data.grouped_total} grouped)
34+
{:else}
35+
{pluralize(searched_properties.length, {
36+
one: 'Found {count} group',
37+
other: 'Found {count} groups',
38+
})}
39+
{/if}
3240
</p>
3341

34-
<PropertyList properties={searched_properties} />
42+
<ul>
43+
{#each searched_properties as { id, relation, dual_property_id }}
44+
<li>
45+
{relation}
46+
<a href={get_property_url(id, 'category')}>{id}</a>
47+
{#if dual_property_id && id !== dual_property_id}
48+
/ <a href={get_property_url(dual_property_id, 'category')}>
49+
{dual_property_id}
50+
</a>
51+
{/if}
52+
</li>
53+
{/each}
54+
</ul>
3555

3656
<SuggestionForm />

src/routes/functor-properties/+page.server.ts

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,44 @@ import { query } from '$lib/server/db'
33
import { error } from '@sveltejs/kit'
44
import sql from 'sql-template-tag'
55

6+
// TODO: remove code duplication with category properties list page
7+
68
export const load = async () => {
7-
const { rows: properties, err } = await query<PropertyShort>(sql`
8-
SELECT id, relation
9-
FROM functor_properties
9+
const { rows: properties, err } = await query<
10+
PropertyShort & { dual_property_id?: string }
11+
>(sql`
12+
SELECT id, relation, dual_property_id FROM functor_properties
1013
ORDER BY lower(id)
1114
`)
1215

1316
if (err) error(500, 'Could not load properties')
1417

15-
return { properties }
18+
const seen = new Set()
19+
20+
const grouped_properties: typeof properties = []
21+
22+
for (const p of properties) {
23+
if (seen.has(p.id) || (p.dual_property_id && seen.has(p.dual_property_id))) {
24+
continue
25+
}
26+
27+
if (p.id.startsWith('co') && p.dual_property_id) {
28+
const swap = {
29+
id: p.dual_property_id,
30+
dual_property_id: p.id,
31+
relation: p.relation,
32+
}
33+
grouped_properties.push(swap)
34+
} else {
35+
grouped_properties.push(p)
36+
}
37+
38+
seen.add(p.id)
39+
if (p.dual_property_id) seen.add(p.dual_property_id)
40+
}
41+
42+
const total = properties.length
43+
const grouped_total = grouped_properties.length
44+
45+
return { grouped_properties, total, grouped_total }
1646
}
Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
<script lang="ts">
22
import MetaData from '$components/MetaData.svelte'
3-
import PropertyList from '$components/PropertyList.svelte'
43
import SuggestionForm from '$components/SuggestionForm.svelte'
5-
import { pluralize } from '$lib/client/utils'
4+
import { get_property_url } from '$lib/commons/property.url'
65
76
let { data } = $props()
87
</script>
@@ -12,14 +11,24 @@
1211
<h2>Properties of Functors</h2>
1312

1413
<!-- TODO: add search feature if the list grows in the future -->
14+
<!-- TODO: remove code duplication with category properties list page -->
1515

1616
<p class="hint">
17-
{pluralize(data.properties.length, {
18-
one: 'Found {count} property',
19-
other: 'Found {count} properties',
20-
})}
17+
Found {data.total} properties ({data.grouped_total} grouped)
2118
</p>
2219

23-
<PropertyList properties={data.properties} type="functor" />
20+
<ul>
21+
{#each data.grouped_properties as { id, relation, dual_property_id }}
22+
<li>
23+
{relation}
24+
<a href={get_property_url(id, 'functor')}>{id}</a>
25+
{#if dual_property_id && id !== dual_property_id}
26+
/ <a href={get_property_url(dual_property_id, 'functor')}>
27+
{dual_property_id}
28+
</a>
29+
{/if}
30+
</li>
31+
{/each}
32+
</ul>
2433

2534
<SuggestionForm />

0 commit comments

Comments
 (0)