Skip to content

Commit 0db2599

Browse files
committed
feat(PoliticalEntity): add order field for listing prioritization
Add order field to PoliticalEntity type and collection to allow manual sorting of entities in listings. Lower numbers appear first, with name used as secondary sort criteria when order is equal.
1 parent a47124d commit 0db2599

File tree

3 files changed

+37
-1
lines changed

3 files changed

+37
-1
lines changed

src/collections/PoliticalEntities/index.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ export const PoliticalEntities: CollectionConfig = {
2424
},
2525
defaultColumns: [
2626
"name",
27+
"order",
2728
"image",
2829
"region",
2930
"position",
@@ -53,6 +54,23 @@ export const PoliticalEntities: CollectionConfig = {
5354
},
5455
},
5556
}),
57+
{
58+
name: "order",
59+
type: "number",
60+
index: true,
61+
min: 0,
62+
admin: {
63+
position: "sidebar",
64+
description: {
65+
en: "Lower numbers appear first in listings.",
66+
fr: "Les nombres les plus bas apparaissent en premier dans les listes.",
67+
},
68+
},
69+
label: {
70+
en: "Order",
71+
fr: "Ordre",
72+
},
73+
},
5674
{
5775
name: "region",
5876
type: "text",

src/components/PoliticalEntityList.tsx

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,21 @@ export const PoliticalEntityList = async ({
6464
)
6565
: {};
6666

67+
const sortedPoliticalEntities = [...politicalEntities].sort((entityA, entityB) => {
68+
const orderA = entityA.order ?? Number.POSITIVE_INFINITY;
69+
const orderB = entityB.order ?? Number.POSITIVE_INFINITY;
70+
71+
if (orderA !== orderB) {
72+
return orderA - orderB;
73+
}
74+
75+
return entityA.name.localeCompare(entityB.name, undefined, {
76+
sensitivity: "base",
77+
});
78+
});
79+
6780
const entitiesWithMedia = await Promise.all(
68-
politicalEntities.map(async (entity) => ({
81+
sortedPoliticalEntities.map(async (entity) => ({
6982
entity,
7083
media: await resolveMedia(entity.image),
7184
}))

src/payload-types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,10 @@ export interface PoliticalEntity {
235235
name: string;
236236
slug: string;
237237
slugLock?: boolean | null;
238+
/**
239+
* Lower numbers appear first in listings.
240+
*/
241+
order?: number | null;
238242
region?: string | null;
239243
/**
240244
* Title of the entity, i.e President, Governor, Prime Minister
@@ -1450,6 +1454,7 @@ export interface PoliticalEntitiesSelect<T extends boolean = true> {
14501454
name?: T;
14511455
slug?: T;
14521456
slugLock?: T;
1457+
order?: T;
14531458
region?: T;
14541459
position?: T;
14551460
image?: T;

0 commit comments

Comments
 (0)