Skip to content

Commit

Permalink
Show user's last activity
Browse files Browse the repository at this point in the history
  • Loading branch information
stnguyen90 committed Jul 6, 2023
1 parent 4ca1c98 commit 7bff1a3
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 0 deletions.
10 changes: 10 additions & 0 deletions src/lib/helpers/date.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
export const toLocaleDate = (datetime: string) => {
const date = new Date(datetime);

if (isNaN(date.getTime())) {
return 'n/a';
}

const options: Intl.DateTimeFormatOptions = {
year: 'numeric',
month: 'short',
Expand All @@ -11,6 +16,11 @@ export const toLocaleDate = (datetime: string) => {

export const toLocaleDateTime = (datetime: string | number) => {
const date = new Date(datetime);

if (isNaN(date.getTime())) {
return 'n/a';
}

const options: Intl.DateTimeFormatOptions = {
year: 'numeric',
month: 'short',
Expand Down
4 changes: 4 additions & 0 deletions src/routes/console/project-[project]/auth/+page.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@
<TableCellHead onlyDesktop width={130}>Status</TableCellHead>
<TableCellHead onlyDesktop width={100}>ID</TableCellHead>
<TableCellHead onlyDesktop>Joined</TableCellHead>
<TableCellHead onlyDesktop>Last Activity</TableCellHead>
</TableHeader>
<TableBody>
{#each data.users.users as user}
Expand Down Expand Up @@ -105,6 +106,9 @@
<TableCellText onlyDesktop title="Joined">
{toLocaleDateTime(user.registration)}
</TableCellText>
<TableCellText onlyDesktop title="Last Activity">
{toLocaleDateTime(user.accessedAt)}
</TableCellText>
</TableRowLink>
{/each}
</TableBody>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<script lang="ts">
import { CardGrid, Box, Heading, AvatarInitials } from '$lib/components';
import { Button } from '$lib/elements/forms';
import { toLocaleDateTime } from '$lib/helpers/date';
import DeleteUser from './deleteUser.svelte';
import { user } from './store';
Expand Down Expand Up @@ -42,6 +43,7 @@
? [$user.email, $user.phone].join(',')
: $user.email || $user.phone}
</p>
<p>Last activity: {toLocaleDateTime($user.accessedAt)}</p>
</Box>
</svelte:fragment>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@
<p class="title">{$user.phone}</p>
{/if}
<p>Joined: {toLocaleDateTime($user.registration)}</p>
<p>Last activity: {toLocaleDateTime($user.accessedAt)}</p>
</div>
{#if !$user.status}
<Pill danger>blocked</Pill>
Expand Down
8 changes: 8 additions & 0 deletions tests/unit/helpers/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ describe('local date', () => {
expect(toLocaleDate(value)).toBe(expected);
});
});

it('invalid date', () => {
expect(toLocaleDate('')).toBe('n/a');
});
});

describe('local date time', () => {
Expand All @@ -27,6 +31,10 @@ describe('local date time', () => {
expect(toLocaleDateTime(value)).toBe(expected);
});
});

it('invalid date', () => {
expect(toLocaleDateTime('')).toBe('n/a');
});
});

describe('is same day', () => {
Expand Down

0 comments on commit 7bff1a3

Please sign in to comment.