Skip to content

Commit

Permalink
Merge pull request #137 from MegaTheLEGEND/main
Browse files Browse the repository at this point in the history
date and time
  • Loading branch information
s-alad authored Aug 14, 2024
2 parents 419cbbf + d64bf87 commit 100bfb7
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 6 deletions.
2 changes: 1 addition & 1 deletion client/components/instant/instant.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ async function getMusicData() {
</Link>
<div className={s.discourse}>
<Link href={`/profile/${comment.owner.uid}`}>
<div className={s.username}>@{comment.owner.username}</div>
<div className={s.username}>@{comment.owner.username} posted {comment.comment_time}</div>
</Link>
<div className={s.convo}>{comment.text}</div>
</div>
Expand Down
30 changes: 28 additions & 2 deletions client/models/comment.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ class Comment {
comment_id: number;
text: string;
owner: User;
comment_time?: string;

constructor(comment_id: number, text: string, owner: User) {
constructor(comment_id: number, text: string, owner: User, comment_time?: string) {
this.comment_id = comment_id;
this.text = text;
this.owner = owner;
this.comment_time = comment_time;
}

static create(raw: any) {
Expand All @@ -23,10 +25,34 @@ class Comment {
static moment(raw: any) {
let comment_id = raw.id;
let text = raw.content;
let comment_time = this.formatTime(raw.postedAt);

let owner = User.create(raw.user);

return new Comment(comment_id, text, owner);
return new Comment(comment_id, text, owner, comment_time);
}

static formatTime(postedAt: string): string {
if (!postedAt) return "no date available";

let postedDate = new Date(postedAt);
let now = new Date();
let diffInSeconds = Math.floor((now.getTime() - postedDate.getTime()) / 1000);

if (diffInSeconds < 60) {
return `${diffInSeconds} seconds ago`;
} else if (diffInSeconds < 3600) {
let minutes = Math.floor(diffInSeconds / 60);
return `${minutes} minutes ago`;
} else {
return postedDate.toLocaleString(undefined, {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
month: 'short',
day: 'numeric'
});
}
}
}

Expand Down
30 changes: 27 additions & 3 deletions client/models/instance.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class Instance {
let primary = raw.primary.url;
let secondary = raw.secondary.url;

let creationdate = raw.takenAt ? new Date(raw.takenAt).toLocaleString(undefined, { hour: '2-digit', minute: '2-digit', second: '2-digit', month: 'short', day: 'numeric' }) : "no date available";
let creationdate = Instance.formatTime(raw.takenAt);

let raw_realmojis = raw.realMojis;
let realmojis: Realmoji[] = [];
Expand Down Expand Up @@ -90,7 +90,7 @@ class Instance {
let primary = raw.primary.url;
let secondary = raw.secondary.url;

let creationdate = raw.takenAt ? new Date(raw.takenAt).toLocaleString(undefined, { hour: '2-digit', minute: '2-digit', second: '2-digit', month: 'short', day: 'numeric' }) : "no date available";
let creationdate = Instance.formatTime(raw.takenAt);

let raw_realmojis = raw.realMojis;
let realmojis: Realmoji[] = [];
Expand Down Expand Up @@ -120,6 +120,30 @@ class Instance {

return new Instance(user, realmojis, comments, location, creationdate, caption, instanceid, primary, secondary, bts, music);
}

// static method to format time
static formatTime(takenAt: string): string {
if (!takenAt) return "no date available";

let postedDate = new Date(takenAt);
let now = new Date();
let diffInSeconds = Math.floor((now.getTime() - postedDate.getTime()) / 1000);

if (diffInSeconds < 60) {
return `${diffInSeconds} seconds ago`;
} else if (diffInSeconds < 3600) {
let minutes = Math.floor(diffInSeconds / 60);
return `${minutes} minutes ago`;
} else {
return postedDate.toLocaleString(undefined, {
hour: '2-digit',
minute: '2-digit',
second: '2-digit',
month: 'short',
day: 'numeric'
});
}
}
}

export default Instance;
export default Instance;

0 comments on commit 100bfb7

Please sign in to comment.