Skip to content

Commit ad89adf

Browse files
MathyouMBl004p
andcommitted
Add sort by date
Co-authored-by: Brontë <33074099+l004p@users.noreply.github.com>
1 parent 452aca3 commit ad89adf

1 file changed

Lines changed: 35 additions & 25 deletions

File tree

src/infrastructure/github/index.ts

Lines changed: 35 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -76,31 +76,41 @@ const fetchData = async (): Promise<Result<any, Error>> => {
7676
};
7777

7878
const convertGithubItems = (items: ProjectV2Item[]) => {
79-
return items.map((item: ProjectV2Item) => {
80-
const assignedUsers = item.fieldValues.nodes
81-
.filter((field) => field.users)
82-
.flatMap((field) => field.users.nodes.map((user) => user.url));
83-
const status = item.fieldValues.nodes
84-
.filter((field) => field.name)
85-
.map((field) => field.name)[0];
86-
const labels = item.fieldValues.nodes
87-
.filter((field) => field.labels)
88-
.flatMap((field) => field.labels.nodes.map((label) => label.name));
79+
return items
80+
.map((item: ProjectV2Item) => {
81+
const assignedUsers = item.fieldValues.nodes
82+
.filter((field) => field.users)
83+
.flatMap((field) => field.users.nodes.map((user) => user.url));
84+
const status = item.fieldValues.nodes
85+
.filter((field) => field.name)
86+
.map((field) => field.name)[0];
87+
const labels = item.fieldValues.nodes
88+
.filter((field) => field.labels)
89+
.flatMap((field) => field.labels.nodes.map((label) => label.name));
8990

90-
// TODO: improve this
91-
let dueDate: Date | undefined;
92-
if (item.fieldValueByName?.date) {
93-
dueDate = new Date(item.fieldValueByName.date);
94-
dueDate.setDate(dueDate.getDate() + 1);
95-
}
91+
// TODO: improve this
92+
let dueDate: Date | undefined;
93+
if (item.fieldValueByName?.date) {
94+
dueDate = new Date(item.fieldValueByName.date);
95+
dueDate.setDate(dueDate.getDate() + 1);
96+
}
9697

97-
return {
98-
title: item.content.title,
99-
url: item.content.url,
100-
assignedUsers,
101-
labels,
102-
dueDate: dueDate,
103-
status: status,
104-
};
105-
});
98+
return {
99+
title: item.content.title,
100+
url: item.content.url,
101+
assignedUsers,
102+
labels,
103+
dueDate: dueDate,
104+
status: status,
105+
};
106+
})
107+
.sort(sortByDate);
108+
};
109+
110+
export const sortByDate = (item1: Item, item2: Item): number => {
111+
if (item1.dueDate === undefined && item2.dueDate === undefined) return 0;
112+
if (item1.dueDate === undefined) return 1;
113+
if (item2.dueDate === undefined) return -1;
114+
if (item1.dueDate === item2.dueDate) return 0;
115+
return item1.dueDate < item2.dueDate ? -1 : 1;
106116
};

0 commit comments

Comments
 (0)