Skip to content

Commit 507a67a

Browse files
committed
Refactor component imports and add new NotesTable component
1 parent 01d1c25 commit 507a67a

File tree

7 files changed

+83
-16
lines changed

7 files changed

+83
-16
lines changed

src/components/atoms/add-new-button/add-new-button.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ export const AddNewButton = ({ openModal }) => {
1111
variant="notes-transparent-border"
1212
onClick={handleClick}
1313
aria-label="Add new"
14-
style={{ marginBottom: '1rem' }}
14+
p={'sm'}
1515
>
1616
Add
1717
</Button>
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { useState } from 'react';
2+
import { AddNewButton, openNoteModal } from '@notes/components';
3+
import { ComboboxItem, Grid, Select, Title } from '@mantine/core';
4+
import { MainLayout } from '@notes/layout';
5+
import { ViewType, viewTypes } from '@notes/types';
6+
import { formOption } from '@notes/utils';
7+
8+
type Props = {
9+
isData: boolean;
10+
title: string;
11+
Table: React.FC<{ isLoading: boolean }>;
12+
Stickers?: React.FC;
13+
Tiles?: React.FC;
14+
};
15+
16+
export function DataDisplay({ isData, title, Table, Stickers, Tiles }: Props) {
17+
const [viewType, setViewType] = useState<ComboboxItem>(formOption<ViewType>(viewTypes.TABLE));
18+
19+
const options = [
20+
formOption<ViewType>(viewTypes.TABLE),
21+
formOption<ViewType>(viewTypes.GRID),
22+
formOption<ViewType>(viewTypes.STICKERS)
23+
];
24+
25+
return (
26+
<MainLayout>
27+
<Grid align="center" justify="space-between" p="xl">
28+
<Grid.Col span={3} order={{ base: 1, sm: 2, lg: 1 }}>
29+
<AddNewButton openModal={openNoteModal} />
30+
</Grid.Col>
31+
<Grid.Col span={3} order={{ base: 1, sm: 2, lg: 1 }}>
32+
<Title order={2}>{title}</Title>
33+
</Grid.Col>
34+
<Grid.Col span={3} order={{ base: 1, sm: 2, lg: 1 }}>
35+
<Select
36+
c={'var(--primary)'}
37+
label="Change display view:"
38+
data={options}
39+
value={viewType.value}
40+
allowDeselect={false}
41+
onChange={(_value, option) => setViewType(option)}
42+
/>
43+
</Grid.Col>
44+
</Grid>
45+
{viewType.value === viewTypes.TABLE && <Table isLoading={!isData} />}
46+
{viewType.value === viewTypes.STICKERS && <Stickers isLoading={!isData} />}
47+
{viewType.value === viewTypes.GRID && <Tiles isLoading={!isData} />}
48+
49+
{!isData && <Title order={3}>Your list is empty! </Title>}
50+
</MainLayout>
51+
);
52+
}

src/types/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from './contexts'
22
export * from './collections'
3-
export * from './table'
3+
export * from './table'
4+
export * from './views'

src/types/views.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export const viewTypes = {
2+
TABLE: 'TABLE',
3+
GRID: 'GRID',
4+
STICKERS: 'STICKERS',
5+
} as const
6+
7+
8+
export type ViewType = typeof viewTypes[keyof typeof viewTypes]

src/utils/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './colors'
22
export * from './remove-id'
33
export * from './routes-def'
4-
export * from './table-controls-config'
4+
export * from './table-controls-config'
5+
export * from './options'

src/utils/options.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export function formOption<T extends string>(value: T, label?: T) {
2+
const newLabel = label || value[0] + value.slice(1)?.toLowerCase() as T
3+
4+
return ({
5+
value,
6+
label: newLabel,
7+
})
8+
}

src/views/pages/notes.tsx

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,19 @@
1-
import { AddNewButton, openNoteModal, NotesTable } from '@notes/components';
2-
import { MainLayout } from '@notes/layout';
3-
4-
import { Title } from '@mantine/core';
1+
import { NotesTable } from '@notes/components';
52
import { useQuery } from '@tanstack/react-query';
63
import { getNotesQueryOptions } from '@notes/rq';
4+
import { DataDisplay } from 'src/components/templates/data-display';
5+
import { Box } from '@mantine/core';
76

87
export const Notes = () => {
98
const { data: notes } = useQuery(getNotesQueryOptions());
109

1110
return (
12-
<MainLayout>
13-
<Title pt="xl" order={2}>
14-
My Private Notes
15-
</Title>
16-
<AddNewButton openModal={openNoteModal} />
17-
<br />
18-
<NotesTable isLoading={!notes?.length} />
19-
{!notes?.length && <Title order={3}>Your note list is empty! Enter a new note! </Title>}
20-
</MainLayout>
11+
<DataDisplay
12+
isData={Boolean(notes?.length)}
13+
title="My Private Notes"
14+
Table={NotesTable}
15+
Stickers={() => <Box style={{ color: 'white' }}>strckers</Box>}
16+
Tiles={() => <Box style={{ color: 'white' }}>Tiles</Box>}
17+
/>
2118
);
2219
};

0 commit comments

Comments
 (0)