Skip to content

Commit

Permalink
feat(profiles): Add ProfileDataTable component and update tests
Browse files Browse the repository at this point in the history
Added a new 'ProfileDataTable' component to display user profiles.
This component flattens the returned profile set for easy processing
and rendering. Updates have also been made to the profiles and
dashboard test files to accommodate this addition. Implemented
breadcrumb navigation for the profiles page for improved user
experience.

Refs: #1
  • Loading branch information
maikbasel committed Jan 21, 2024
1 parent 50081dd commit 3c23f95
Show file tree
Hide file tree
Showing 4 changed files with 240 additions and 6 deletions.
22 changes: 20 additions & 2 deletions src/app/profiles/page.tsx
Original file line number Diff line number Diff line change
@@ -1,11 +1,29 @@
'use client';

import React from 'react';
import { useProfileContext } from '@/sections/dashboard/context/profile-context';
import { ProfileDataTable } from '@/sections/profiles/components/profile-data-table';
import { ProfileSet, profileSetSchema } from '@/modules/profiles/domain';
import BreadCrumb from '@/components/breadcrumb';

export default function Configuration() {
export default function Profiles() {
const { data, error, isLoading } = useProfileContext();

if (isLoading) {
return <div>Loading...</div>; // FIXME: Make more visually appealing
}

if (error) {
throw new Error(error.message); // FIXME: Handle error
}

const parsed: ProfileSet = profileSetSchema.parse(data);

const breadcrumbItems = [{ title: 'Profiles', link: '/profile' }];
return (
<div className='flex h-full flex-col space-y-4 p-4 pt-6'>
<span className='mt-10 px-8 text-3xl font-bold'>Profiles Page</span>
<BreadCrumb items={breadcrumbItems} />
<ProfileDataTable data={parsed} />
</div>
);
}
6 changes: 2 additions & 4 deletions src/sections/dashboard/components/header/profile-nav.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ import React from 'react';
import { render, screen, waitFor } from '@testing-library/react';
import userEvent from '@testing-library/user-event';
import { randomFillSync } from 'crypto';
import {
ProfileNav,
ProfileSet,
} from '@/sections/dashboard/components/header/profile-nav';
import { ProfileNav } from '@/sections/dashboard/components/header/profile-nav';
import { clearMocks, mockIPC } from '@tauri-apps/api/mocks';
import { ProfileProvider } from '@/sections/dashboard/context/profile-context';
import { SWRConfig } from 'swr';
import { ProfileSet } from '@/modules/profiles/domain';

describe('<ProfileNav />', () => {
const profileSet: ProfileSet = {
Expand Down
33 changes: 33 additions & 0 deletions src/sections/profiles/components/profile-data-table.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import React from 'react';
import { render, screen } from '@testing-library/react';
import { ProfileSet } from '@/modules/profiles/domain';
import { ProfileDataTable } from '@/sections/profiles/components/profile-data-table';

describe('<ProfileDataTable />', () => {
it('should render profile data table with profile set data', () => {
const profileSet: ProfileSet = {
profiles: {
prof1: {
credentials: {
access_key_id: 'key1',
secret_access_key: 'secret1',
},
config: {
region: 'eu-west-1',
output_format: 'json',
},
},
},
errors: {},
};

render(<ProfileDataTable data={profileSet} />);

const row = screen.getByText(/prof1/i).closest('tr');

expect(row).toHaveTextContent('key1');
expect(row).toHaveTextContent('secret1');
expect(row).toHaveTextContent('eu-west-1');
expect(row).toHaveTextContent('json');
});
});
185 changes: 185 additions & 0 deletions src/sections/profiles/components/profile-data-table.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
'use client';

import React from 'react';
import { ColumnDef } from '@tanstack/table-core';
import { ProfileSet } from '@/modules/profiles/domain';
import { DataTable } from '@/components/ui/data-table';
import {
DropdownMenu,
DropdownMenuContent,
DropdownMenuItem,
DropdownMenuLabel,
DropdownMenuSeparator,
DropdownMenuTrigger,
} from '@/components/ui/dropdown-menu';
import { Button } from '@/components/ui/button';
import { FileType, Globe2Icon, LucideIcon, MoreHorizontal } from 'lucide-react';
import { DataTableColumnHeader } from '@/components/ui/data-table-column-header';
import { Checkbox } from '@/components/ui/checkbox';
import {
FilterableColumn,
SearchInputFilter,
} from '@/components/ui/data-table-toolbar';

type Profile = {
name: string;
access_key_id?: string;
secret_access_key?: string;
region?: string;
output_format?: string;
};

const profileColumns: ColumnDef<Profile>[] = [
{
id: 'select',
header: ({ table }) => (
<Checkbox
checked={
table.getIsAllPageRowsSelected() ||
(table.getIsSomePageRowsSelected() && 'indeterminate')
}
onCheckedChange={(value) => table.toggleAllPageRowsSelected(!!value)}
aria-label='Select all'
className='translate-y-[2px]'
/>
),
cell: ({ row }) => (
<Checkbox
checked={row.getIsSelected()}
onCheckedChange={(value) => row.toggleSelected(!!value)}
aria-label='Select row'
className='translate-y-[2px]'
/>
),
enableSorting: false,
enableHiding: false,
},
{
accessorKey: 'name',
header: ({ column }) => (
<DataTableColumnHeader column={column} title='Name' />
),
},
{
accessorKey: 'access_key_id',
header: ({ column }) => (
<DataTableColumnHeader column={column} title='Access Key ID' />
),
},
{
accessorKey: 'secret_access_key',
header: ({ column }) => (
<DataTableColumnHeader column={column} title='Secret Access Key' />
),
},
{
accessorKey: 'region',
header: ({ column }) => (
<DataTableColumnHeader column={column} title='Region' />
),
filterFn: (row, id, value) => {
return value.includes(row.getValue(id));
},
},
{
accessorKey: 'output_format',
header: ({ column }) => (
<DataTableColumnHeader column={column} title='Output Format' />
),
filterFn: (row, id, value) => {
return value.includes(row.getValue(id));
},
},
{
id: 'actions',
cell: ({ row }) => {
const profile = row.original;

return (
<DropdownMenu>
<DropdownMenuTrigger asChild>
<Button variant='ghost' className='h-8 w-8 p-0'>
<span className='sr-only'>Open menu</span>
<MoreHorizontal className='h-4 w-4' />
</Button>
</DropdownMenuTrigger>
<DropdownMenuContent align='end'>
<DropdownMenuLabel>Actions</DropdownMenuLabel>
<DropdownMenuSeparator />
<DropdownMenuItem>Update {profile.name} profile</DropdownMenuItem>
<DropdownMenuItem>Delete {profile.name} profile</DropdownMenuItem>
</DropdownMenuContent>
</DropdownMenu>
);
},
},
];

function flattenProfileSet(profileSet: ProfileSet): Profile[] {
const flattenedArr = [];

for (const [key, value] of Object.entries(profileSet.profiles)) {
const flattenedObj = {
name: key,
...value.credentials,
...value.config,
};
flattenedArr.push(flattenedObj);
}

return flattenedArr;
}

type ProfileDataTableProps = {
data: ProfileSet;
};

export function ProfileDataTable({ data }: Readonly<ProfileDataTableProps>) {
const profiles: Profile[] = flattenProfileSet(data);

const getFilterOptions = (property: keyof Profile, icon: LucideIcon) =>
profiles
.filter((profile) => profile[property] !== undefined)
.filter(
(profile, index, array) =>
array.findIndex((entry) => entry[property] === profile[property]) ===
index
)
.map((profile) => {
return {
label: profile[property] as string,
value: profile[property] as string,
icon: icon,
};
});

const regionFilterOptions = getFilterOptions('region', Globe2Icon);
const outputFormatFilterOptions = getFilterOptions('output_format', FileType);

const filterableColumns: FilterableColumn[] = [
{
title: 'Region',
columnName: 'region',
options: regionFilterOptions,
},
{
title: 'Output Format',
columnName: 'output_format',
options: outputFormatFilterOptions,
},
];

const searchInputFilter: SearchInputFilter = {
columnName: 'name',
placeholder: 'Filter profiles',
};

return (
<DataTable
columns={profileColumns}
data={profiles}
searchInputFilter={searchInputFilter}
filterableColumns={filterableColumns}
/>
);
}

0 comments on commit 3c23f95

Please sign in to comment.