diff --git a/src/features/tasks/components/data-table-row-actions.tsx b/src/features/tasks/components/data-table-row-actions.tsx index d0287d8b..c4e971be 100644 --- a/src/features/tasks/components/data-table-row-actions.tsx +++ b/src/features/tasks/components/data-table-row-actions.tsx @@ -15,7 +15,7 @@ import { DropdownMenuSubTrigger, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu' -import { useTasksContext } from '../context/tasks-context' +import { useTasks } from '../context/tasks-context' import { labels } from '../data/data' import { taskSchema } from '../data/schema' @@ -28,7 +28,7 @@ export function DataTableRowActions({ }: DataTableRowActionsProps) { const task = taskSchema.parse(row.original) - const { setOpen, setCurrentRow } = useTasksContext() + const { setOpen, setCurrentRow } = useTasks() return ( diff --git a/src/features/tasks/components/tasks-dialogs.tsx b/src/features/tasks/components/tasks-dialogs.tsx new file mode 100644 index 00000000..9d32c5a1 --- /dev/null +++ b/src/features/tasks/components/tasks-dialogs.tsx @@ -0,0 +1,78 @@ +import { toast } from '@/hooks/use-toast' +import { ConfirmDialog } from '@/components/confirm-dialog' +import { useTasks } from '../context/tasks-context' +import { TasksImportDialog } from './tasks-import-dialog' +import { TasksMutateDrawer } from './tasks-mutate-drawer' + +export function TasksDialogs() { + const { open, setOpen, currentRow, setCurrentRow } = useTasks() + return ( + <> + setOpen('create')} + /> + + setOpen('import')} + /> + + {currentRow && ( + <> + { + setOpen('update') + setTimeout(() => { + setCurrentRow(null) + }, 500) + }} + currentRow={currentRow} + /> + + { + setOpen('delete') + setTimeout(() => { + setCurrentRow(null) + }, 500) + }} + handleConfirm={() => { + setOpen(null) + setTimeout(() => { + setCurrentRow(null) + }, 500) + toast({ + title: 'The following task has been deleted:', + description: ( +
+                    
+                      {JSON.stringify(currentRow, null, 2)}
+                    
+                  
+ ), + }) + }} + className='max-w-md' + title={`Delete this task: ${currentRow.id} ?`} + desc={ + <> + You are about to delete a task with the ID{' '} + {currentRow.id}.
+ This action cannot be undone. + + } + confirmText='Delete' + /> + + )} + + ) +} diff --git a/src/features/tasks/components/tasks-primary-buttons.tsx b/src/features/tasks/components/tasks-primary-buttons.tsx new file mode 100644 index 00000000..1112f8bf --- /dev/null +++ b/src/features/tasks/components/tasks-primary-buttons.tsx @@ -0,0 +1,21 @@ +import { IconDownload, IconPlus } from '@tabler/icons-react' +import { Button } from '@/components/ui/button' +import { useTasks } from '../context/tasks-context' + +export function TasksPrimaryButtons() { + const { setOpen } = useTasks() + return ( +
+ + +
+ ) +} diff --git a/src/features/tasks/context/tasks-context.tsx b/src/features/tasks/context/tasks-context.tsx index 0a4df214..d4136732 100644 --- a/src/features/tasks/context/tasks-context.tsx +++ b/src/features/tasks/context/tasks-context.tsx @@ -1,7 +1,8 @@ -import React from 'react' +import React, { useState } from 'react' +import useDialogState from '@/hooks/use-dialog-state' import { Task } from '../data/schema' -export type TasksDialogType = 'create' | 'update' | 'delete' | 'import' +type TasksDialogType = 'create' | 'update' | 'delete' | 'import' interface TasksContextType { open: TasksDialogType | null @@ -14,21 +15,24 @@ const TasksContext = React.createContext(null) interface Props { children: React.ReactNode - value: TasksContextType } -export default function TasksContextProvider({ children, value }: Props) { - return {children} +export default function TasksProvider({ children }: Props) { + const [open, setOpen] = useDialogState(null) + const [currentRow, setCurrentRow] = useState(null) + return ( + + {children} + + ) } // eslint-disable-next-line react-refresh/only-export-components -export const useTasksContext = () => { +export const useTasks = () => { const tasksContext = React.useContext(TasksContext) if (!tasksContext) { - throw new Error( - 'useTasksContext has to be used within ' - ) + throw new Error('useTasks has to be used within ') } return tasksContext diff --git a/src/features/tasks/index.tsx b/src/features/tasks/index.tsx index ad598489..67529fdb 100644 --- a/src/features/tasks/index.tsx +++ b/src/features/tasks/index.tsx @@ -1,9 +1,3 @@ -import { useState } from 'react' -import { IconDownload, IconPlus } from '@tabler/icons-react' -import useDialogState from '@/hooks/use-dialog-state' -import { toast } from '@/hooks/use-toast' -import { Button } from '@/components/ui/button' -import { ConfirmDialog } from '@/components/confirm-dialog' import { Header } from '@/components/layout/header' import { Main } from '@/components/layout/main' import { ProfileDropdown } from '@/components/profile-dropdown' @@ -11,20 +5,14 @@ import { Search } from '@/components/search' import { ThemeSwitch } from '@/components/theme-switch' import { columns } from './components/columns' import { DataTable } from './components/data-table' -import { TasksImportDialog } from './components/tasks-import-dialog' -import { TasksMutateDrawer } from './components/tasks-mutate-drawer' -import TasksContextProvider, { TasksDialogType } from './context/tasks-context' -import { Task } from './data/schema' +import { TasksDialogs } from './components/tasks-dialogs' +import { TasksPrimaryButtons } from './components/tasks-primary-buttons' +import TasksProvider from './context/tasks-context' import { tasks } from './data/tasks' export default function Tasks() { - // Local states - const [currentRow, setCurrentRow] = useState(null) - const [open, setOpen] = useDialogState(null) - return ( - - {/* ===== Top Heading ===== */} +
@@ -41,89 +29,14 @@ export default function Tasks() { Here's a list of your tasks for this month!

-
- - -
+
- setOpen('create')} - /> - - setOpen('import')} - /> - - {currentRow && ( - <> - { - setOpen('update') - setTimeout(() => { - setCurrentRow(null) - }, 500) - }} - currentRow={currentRow} - /> - - { - setOpen('delete') - setTimeout(() => { - setCurrentRow(null) - }, 500) - }} - handleConfirm={() => { - setOpen(null) - setTimeout(() => { - setCurrentRow(null) - }, 500) - toast({ - title: 'The following task has been deleted:', - description: ( -
-                    
-                      {JSON.stringify(currentRow, null, 2)}
-                    
-                  
- ), - }) - }} - className='max-w-md' - title={`Delete this task: ${currentRow.id} ?`} - desc={ - <> - You are about to delete a task with the ID{' '} - {currentRow.id}.
- This action cannot be undone. - - } - confirmText='Delete' - /> - - )} - + + ) } diff --git a/src/features/users/components/data-table-row-actions.tsx b/src/features/users/components/data-table-row-actions.tsx index 9077be1d..51307bf6 100644 --- a/src/features/users/components/data-table-row-actions.tsx +++ b/src/features/users/components/data-table-row-actions.tsx @@ -10,7 +10,7 @@ import { DropdownMenuShortcut, DropdownMenuTrigger, } from '@/components/ui/dropdown-menu' -import { useUsersContext } from '../context/users-context' +import { useUsers } from '../context/users-context' import { User } from '../data/schema' interface DataTableRowActionsProps { @@ -18,7 +18,7 @@ interface DataTableRowActionsProps { } export function DataTableRowActions({ row }: DataTableRowActionsProps) { - const { setOpen, setCurrentRow } = useUsersContext() + const { setOpen, setCurrentRow } = useUsers() return ( <> diff --git a/src/features/users/components/users-dialogs.tsx b/src/features/users/components/users-dialogs.tsx new file mode 100644 index 00000000..018eaf3e --- /dev/null +++ b/src/features/users/components/users-dialogs.tsx @@ -0,0 +1,51 @@ +import { useUsers } from '../context/users-context' +import { UsersActionDialog } from './users-action-dialog' +import { UsersDeleteDialog } from './users-delete-dialog' +import { UsersInviteDialog } from './users-invite-dialog' + +export function UsersDialogs() { + const { open, setOpen, currentRow, setCurrentRow } = useUsers() + return ( + <> + setOpen('add')} + /> + + setOpen('invite')} + /> + + {currentRow && ( + <> + { + setOpen('edit') + setTimeout(() => { + setCurrentRow(null) + }, 500) + }} + currentRow={currentRow} + /> + + { + setOpen('delete') + setTimeout(() => { + setCurrentRow(null) + }, 500) + }} + currentRow={currentRow} + /> + + )} + + ) +} diff --git a/src/features/users/components/users-primary-buttons.tsx b/src/features/users/components/users-primary-buttons.tsx new file mode 100644 index 00000000..53fe01f0 --- /dev/null +++ b/src/features/users/components/users-primary-buttons.tsx @@ -0,0 +1,21 @@ +import { IconMailPlus, IconUserPlus } from '@tabler/icons-react' +import { Button } from '@/components/ui/button' +import { useUsers } from '../context/users-context' + +export function UsersPrimaryButtons() { + const { setOpen } = useUsers() + return ( +
+ + +
+ ) +} diff --git a/src/features/users/context/users-context.tsx b/src/features/users/context/users-context.tsx index 89edd981..078ade84 100644 --- a/src/features/users/context/users-context.tsx +++ b/src/features/users/context/users-context.tsx @@ -1,7 +1,8 @@ -import React from 'react' +import React, { useState } from 'react' +import useDialogState from '@/hooks/use-dialog-state' import { User } from '../data/schema' -export type UsersDialogType = 'invite' | 'add' | 'edit' | 'delete' +type UsersDialogType = 'invite' | 'add' | 'edit' | 'delete' interface UsersContextType { open: UsersDialogType | null @@ -14,21 +15,25 @@ const UsersContext = React.createContext(null) interface Props { children: React.ReactNode - value: UsersContextType } -export default function UsersContextProvider({ children, value }: Props) { - return {children} +export default function UsersProvider({ children }: Props) { + const [open, setOpen] = useDialogState(null) + const [currentRow, setCurrentRow] = useState(null) + + return ( + + {children} + + ) } // eslint-disable-next-line react-refresh/only-export-components -export const useUsersContext = () => { +export const useUsers = () => { const usersContext = React.useContext(UsersContext) if (!usersContext) { - throw new Error( - 'useUsersContext has to be used within ' - ) + throw new Error('useUsers has to be used within ') } return usersContext diff --git a/src/features/users/index.tsx b/src/features/users/index.tsx index ad90d89b..38376961 100644 --- a/src/features/users/index.tsx +++ b/src/features/users/index.tsx @@ -1,34 +1,22 @@ -import { useState } from 'react' -import { IconMailPlus, IconUserPlus } from '@tabler/icons-react' -import useDialogState from '@/hooks/use-dialog-state' -import { Button } from '@/components/ui/button' import { Header } from '@/components/layout/header' import { Main } from '@/components/layout/main' import { ProfileDropdown } from '@/components/profile-dropdown' import { Search } from '@/components/search' import { ThemeSwitch } from '@/components/theme-switch' -import { UsersActionDialog } from './components/users-action-dialog' import { columns } from './components/users-columns' -import { UsersDeleteDialog } from './components/users-delete-dialog' -import { UsersInviteDialog } from './components/users-invite-dialog' +import { UsersDialogs } from './components/users-dialogs' +import { UsersPrimaryButtons } from './components/users-primary-buttons' import { UsersTable } from './components/users-table' -import UsersContextProvider, { - type UsersDialogType, -} from './context/users-context' -import { User, userListSchema } from './data/schema' +import UsersProvider from './context/users-context' +import { userListSchema } from './data/schema' import { users } from './data/users' export default function Users() { - // Dialog states - const [currentRow, setCurrentRow] = useState(null) - const [open, setOpen] = useDialogState(null) - // Parse user list const userList = userListSchema.parse(users) return ( - - {/* ===== Top Heading ===== */} +
@@ -45,63 +33,14 @@ export default function Users() { Manage your users and their roles here.

-
- - -
+
- setOpen('add')} - /> - - setOpen('invite')} - /> - - {currentRow && ( - <> - { - setOpen('edit') - setTimeout(() => { - setCurrentRow(null) - }, 500) - }} - currentRow={currentRow} - /> - - { - setOpen('delete') - setTimeout(() => { - setCurrentRow(null) - }, 500) - }} - currentRow={currentRow} - /> - - )} - + + ) }