Skip to content

Commit 0c9d17b

Browse files
author
Elise Bach
committed
Added functions to utils
1 parent f0b0e0a commit 0c9d17b

File tree

6 files changed

+65
-61
lines changed

6 files changed

+65
-61
lines changed

app/thread/[threadId]/page.tsx

Lines changed: 12 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,50 +1,10 @@
11
import Navigation from "@/components/navigation";
22
import styles from "./page.module.css";
3-
import Actor from "@/components/actor";
4-
import { ActorPartsFragment } from "@team-plain/typescript-sdk";
5-
import Avatar from "@/components/avatar";
6-
7-
function getFullname(actor) {
8-
switch (actor.__typename) {
9-
case "CustomerActor": {
10-
return actor.customer.fullName;
11-
}
12-
case "UserActor": {
13-
return actor.user.fullName;
14-
}
15-
case "MachineUserActor": {
16-
return actor.user.fullName;
17-
}
18-
}
19-
}
20-
21-
function getPriority(priority: 0 | 1 | 2 | 3) {
22-
switch (priority) {
23-
case 0: {
24-
return "Urgent";
25-
}
26-
case 1: {
27-
return "High";
28-
}
29-
case 2: {
30-
return "Normal";
31-
}
32-
case 3: {
33-
return "Low";
34-
}
35-
}
36-
}
37-
38-
export default async function ThreadPage({
39-
params,
40-
}: {
41-
params: { threadId: string };
42-
}) {
43-
const apiKey = process.env.PLAIN_API_KEY;
44-
if (!apiKey) {
45-
throw new Error("Please set the `PLAIN_API_KEY` environment variable");
46-
}
3+
import { getActorFullName } from "@/utils/getActorFullName";
4+
import { getFormattedDate } from "@/utils/getFormattedDate";
5+
import { getPriority } from "@/utils/getPriority";
476

7+
export default async function ThreadPage() {
488
const data = await fetch("https://core-api.uk.plain.com/graphql/v1", {
499
method: "POST",
5010
body: JSON.stringify({
@@ -130,11 +90,11 @@ export default async function ThreadPage({
13090
"Plain-Workspace-Id": "w_01J28VHKDK5PV3DJSZAA01XGAN",
13191
Authorization: `Bearer ${process.env.PLAIN_API_KEY}`,
13292
},
133-
})
134-
.then((res) => res.json())
93+
}).then((res) => res.json());
13594

13695
const thread = data.data.thread;
13796
const timelineEntries = thread.timelineEntries;
97+
13898
return (
13999
<>
140100
<Navigation hasBackButton title={thread.title} />
@@ -156,14 +116,14 @@ export default async function ThreadPage({
156116
<div className={styles.message} key={entry.id}>
157117
<div className={styles.entryHeader}>
158118
<div className={styles.avatar}>
159-
{getFullname(entry.actor)[0].toUpperCase()}
119+
{getActorFullName(entry.actor)[0].toUpperCase()}
160120
</div>
161121
<div>
162122
<div className={styles.actor}>
163-
{getFullname(entry.actor)}
123+
{getActorFullName(entry.actor)}
164124
</div>
165125
<div className={styles.timestamp}>
166-
{entry.timestamp.iso8601}
126+
{getFormattedDate(entry.timestamp.iso8601)}
167127
</div>
168128
</div>
169129
</div>
@@ -197,17 +157,17 @@ export default async function ThreadPage({
197157
<div className={styles.threadInfoGrid}>
198158
<div className={styles.threadInfoProp}>Opened by:</div>
199159
<div className={styles.threadInfoDesc}>
200-
{getFullname(thread.createdBy)}
160+
{getActorFullName(thread.createdBy)}
201161
</div>
202162

203163
<div className={styles.threadInfoProp}>Opened:</div>
204164
<div className={styles.threadInfoDesc}>
205-
{thread.createdAt.iso8601}
165+
{getFormattedDate(thread.createdAt.iso8601)}
206166
</div>
207167

208168
<div className={styles.threadInfoProp}>Last activity:</div>
209169
<div className={styles.threadInfoDesc}>
210-
{thread.updatedAt.iso8601}
170+
{getFormattedDate(thread.updatedAt.iso8601)}
211171
</div>
212172

213173
<div className={styles.threadInfoProp}>Status:</div>

components/threadRow.tsx

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,19 @@
11
import { plainClient } from "@/utils/plainClient";
2-
import { ThreadPartsFragment } from "@team-plain/typescript-sdk";
3-
import styles from './threadRow.module.css';
2+
import type { ThreadPartsFragment } from "@team-plain/typescript-sdk";
3+
import styles from "./threadRow.module.css";
44

55
export async function ThreadRow({ thread }: { thread: ThreadPartsFragment }) {
6-
const customer = await plainClient.getCustomerById({ customerId: thread.customer.id });
6+
const customer = await plainClient.getCustomerById({
7+
customerId: thread.customer.id,
8+
});
79

8-
return (
9-
<a className={styles.row} href={`/thread/${thread.id}`}>
10-
<div>{customer.data?.fullName}</div><div><h3>{thread.title}</h3><div>{thread.previewText}</div></div>
11-
</a>
12-
)
13-
}
10+
return (
11+
<a className={styles.row} href={`/thread/${thread.id}`}>
12+
<div>{customer.data?.fullName}</div>
13+
<div>
14+
<h3>{thread.title}</h3>
15+
<div>{thread.previewText}</div>
16+
</div>
17+
</a>
18+
);
19+
}

utils/createdBy.ts

Whitespace-only changes.

utils/getActorFullName.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
export function getActorFullName(actor) {
2+
switch (actor.__typename) {
3+
case "CustomerActor": {
4+
return actor.customer.fullName;
5+
}
6+
case "UserActor": {
7+
return actor.user.fullName;
8+
}
9+
case "MachineUserActor": {
10+
return actor.user.fullName;
11+
}
12+
}
13+
}

utils/getFormattedDate.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export function getFormattedDate(date: string) {
2+
return new Date(date).toLocaleDateString(undefined, {
3+
year: "numeric",
4+
month: "long",
5+
day: "numeric",
6+
hour: "numeric",
7+
minute: "numeric",
8+
});
9+
}

utils/getPriority.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
export function getPriority(priority: 0 | 1 | 2 | 3) {
2+
switch (priority) {
3+
case 0: {
4+
return "Urgent";
5+
}
6+
case 1: {
7+
return "High";
8+
}
9+
case 2: {
10+
return "Normal";
11+
}
12+
case 3: {
13+
return "Low";
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)