-
-
Notifications
You must be signed in to change notification settings - Fork 354
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
734d716
commit 3784767
Showing
15 changed files
with
194 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,128 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { DataModel, DataSource } from './shared'; | ||
import { CRUDContext } from '../shared/context'; | ||
import { match } from 'path-to-regexp'; | ||
import invariant from 'invariant'; | ||
import { DataModel, DataModelId, DataSource } from './shared'; | ||
import { Provider as CRUDProvider } from './Provider'; | ||
import { RouterContext } from '../shared/context'; | ||
import { List } from './List'; | ||
import { Show } from './Show'; | ||
import { Create } from './Create'; | ||
import { Edit } from './Edit'; | ||
|
||
export interface CRUDProps<D extends DataModel> { | ||
/** | ||
* Server-side data source. | ||
*/ | ||
dataSource: DataSource<D>; | ||
children?: React.ReactNode; | ||
/** | ||
* Path to resource list page. | ||
*/ | ||
list: string; | ||
/** | ||
* Path to resource show page. | ||
*/ | ||
show: string; | ||
/** | ||
* Path to resource create page. | ||
*/ | ||
create: string; | ||
/** | ||
* Path to resource edit page. | ||
*/ | ||
edit: string; | ||
/** | ||
* Initial number of rows to show per page. | ||
* @default 100 | ||
*/ | ||
initialPageSize?: number; | ||
/** | ||
* Initial form values. | ||
*/ | ||
initialValues?: Omit<D, 'id'>; | ||
} | ||
|
||
function CRUD<D extends DataModel>(props: CRUDProps<D>) { | ||
const { dataSource, children } = props; | ||
const { dataSource, list, show, create, edit, initialPageSize, initialValues } = props; | ||
|
||
const routerContext = React.useContext(RouterContext); | ||
|
||
const handleRowClick = React.useCallback( | ||
(id: string | number) => { | ||
routerContext?.navigate(`/orders/${String(id)}`); | ||
}, | ||
[routerContext], | ||
); | ||
|
||
const handleCreateClick = React.useCallback(() => { | ||
routerContext?.navigate('/orders/new'); | ||
}, [routerContext]); | ||
|
||
const handleEditClick = React.useCallback( | ||
(id: string | number) => { | ||
routerContext?.navigate(`/orders/${String(id)}/edit`); | ||
}, | ||
[routerContext], | ||
); | ||
|
||
const handleCreate = React.useCallback(() => { | ||
routerContext?.navigate('/orders'); | ||
}, [routerContext]); | ||
|
||
const handleEdit = React.useCallback(() => { | ||
routerContext?.navigate('/orders'); | ||
}, [routerContext]); | ||
|
||
const handleDelete = React.useCallback(() => { | ||
routerContext?.navigate('/orders'); | ||
}, [routerContext]); | ||
|
||
const renderedRoute = React.useMemo(() => { | ||
const pathname = routerContext?.pathname ?? ''; | ||
|
||
if (match(list)(pathname)) { | ||
return ( | ||
<List<D> | ||
initialPageSize={initialPageSize} | ||
onRowClick={handleRowClick} | ||
onCreateClick={handleCreateClick} | ||
onEditClick={handleEditClick} | ||
/> | ||
); | ||
} | ||
const showMatch = match<{ id: DataModelId }>(show)(pathname); | ||
if (showMatch) { | ||
const resourceId = showMatch.params.id; | ||
invariant(resourceId, 'No resource ID present in URL.'); | ||
return <Show<D> id={resourceId} onEditClick={handleEditClick} onDelete={handleDelete} />; | ||
} | ||
if (match(create)(pathname)) { | ||
return <Create<D> initialValues={initialValues} onSubmitSuccess={handleCreate} />; | ||
} | ||
const editMatch = match<{ id: DataModelId }>(edit)(pathname); | ||
if (editMatch) { | ||
const resourceId = editMatch.params.id; | ||
invariant(resourceId, 'No resource ID present in URL.'); | ||
return <Edit<D> id={resourceId} onSubmitSuccess={handleEdit} />; | ||
} | ||
return null; | ||
}, [ | ||
routerContext?.pathname, | ||
list, | ||
show, | ||
create, | ||
edit, | ||
initialPageSize, | ||
handleRowClick, | ||
handleCreateClick, | ||
handleEditClick, | ||
handleDelete, | ||
initialValues, | ||
handleCreate, | ||
handleEdit, | ||
]); | ||
|
||
return <CRUDContext value={{ dataSource } as CRUDProps<DataModel>}>{children}</CRUDContext>; | ||
return <CRUDProvider<D> dataSource={dataSource}>{renderedRoute}</CRUDProvider>; | ||
} | ||
|
||
export { CRUD }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { DataModel, DataSource } from './shared'; | ||
import { CRUDContext } from '../shared/context'; | ||
|
||
export interface ProviderProps<D extends DataModel> { | ||
/** | ||
* Server-side data source. | ||
*/ | ||
dataSource: DataSource<D>; | ||
children?: React.ReactNode; | ||
} | ||
|
||
function Provider<D extends DataModel>(props: ProviderProps<D>) { | ||
const { dataSource, children } = props; | ||
|
||
return <CRUDContext value={{ dataSource } as ProviderProps<DataModel>}>{children}</CRUDContext>; | ||
} | ||
|
||
export { Provider }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.