-
-
Notifications
You must be signed in to change notification settings - Fork 369
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[docs] Add CRUD to themed example #4785
Open
bharatkashyap
wants to merge
4
commits into
mui:master
Choose a base branch
from
bharatkashyap:examples/crud-themed
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+372
−8
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
84 changes: 84 additions & 0 deletions
84
examples/core/auth-nextjs-themed/app/(dashboard)/employees/[[...segments]]/page.tsx
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,84 @@ | ||
'use client'; | ||
import * as React from 'react'; | ||
import { usePathname, useRouter } from 'next/navigation'; | ||
import { CrudProvider, List, Create, Edit, Show } from '@toolpad/core/Crud'; | ||
import { employeesDataSource, Employee, employeesCache } from '../../../mocks/employees'; | ||
import CustomDataGrid from '../../../components/CustomDataGrid'; | ||
|
||
function matchPath(pattern: string, pathname: string): string | null { | ||
const regex = new RegExp(`^${pattern.replace(/:[^/]+/g, '([^/]+)')}$`); | ||
const match = pathname.match(regex); | ||
return match ? match[1] : null; | ||
} | ||
|
||
export default function EmployeesCrudPage() { | ||
const pathname = usePathname(); | ||
const router = useRouter(); | ||
|
||
const rootPath = '/employees'; | ||
const listPath = rootPath; | ||
const showPath = `${rootPath}/:employeeId`; | ||
const createPath = `${rootPath}/new`; | ||
const editPath = `${rootPath}/:employeeId/edit`; | ||
|
||
const showEmployeeId = matchPath(showPath, pathname); | ||
const editEmployeeId = matchPath(editPath, pathname); | ||
|
||
const handleRowClick = React.useCallback( | ||
(employeeId: string | number) => { | ||
console.log('Clicked on row with ID', employeeId); | ||
router.push(`${rootPath}/${String(employeeId)}`); | ||
}, | ||
[router], | ||
); | ||
|
||
const handleCreateClick = React.useCallback(() => { | ||
router.push(createPath); | ||
}, [createPath, router]); | ||
|
||
const handleEditClick = React.useCallback( | ||
(employeeId: string | number) => { | ||
router.push(`${rootPath}/${String(employeeId)}/edit`); | ||
}, | ||
[router], | ||
); | ||
|
||
const handleCreate = React.useCallback(() => { | ||
router.push(listPath); | ||
}, [listPath, router]); | ||
|
||
const handleEdit = React.useCallback(() => { | ||
router.push(listPath); | ||
}, [listPath, router]); | ||
|
||
const handleDelete = React.useCallback(() => { | ||
router.push(listPath); | ||
}, [listPath, router]); | ||
|
||
return ( | ||
<CrudProvider<Employee> dataSource={employeesDataSource} dataSourceCache={employeesCache}> | ||
{pathname === listPath ? ( | ||
<List<Employee> | ||
initialPageSize={25} | ||
onRowClick={handleRowClick} | ||
onCreateClick={handleCreateClick} | ||
onEditClick={handleEditClick} | ||
slots={{ | ||
dataGrid: CustomDataGrid, | ||
}} | ||
/> | ||
) : null} | ||
{pathname === createPath ? ( | ||
<Create<Employee> | ||
initialValues={{ title: 'New Employee' }} | ||
onSubmitSuccess={handleCreate} | ||
resetOnSubmit={false} | ||
/> | ||
) : null} | ||
{pathname !== createPath && showEmployeeId ? ( | ||
<Show<Employee> id={showEmployeeId} onEditClick={handleEditClick} onDelete={handleDelete} /> | ||
) : null} | ||
{editEmployeeId ? <Edit<Employee> id={editEmployeeId} onSubmitSuccess={handleEdit} /> : null} | ||
</CrudProvider> | ||
); | ||
} |
21 changes: 20 additions & 1 deletion
21
examples/core/auth-nextjs-themed/app/(dashboard)/layout.tsx
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
211 changes: 211 additions & 0 deletions
211
examples/core/auth-nextjs-themed/app/mocks/employees.ts
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,211 @@ | ||
'use client'; | ||
import { DataModel, DataSource, DataSourceCache } from '@toolpad/core/Crud'; | ||
import { z } from 'zod'; | ||
|
||
type EmployeeRole = 'Market' | 'Finance' | 'Development'; | ||
|
||
export interface Employee extends DataModel { | ||
id: number; | ||
name: string; | ||
age: number; | ||
joinDate: string; | ||
role: EmployeeRole; | ||
} | ||
|
||
const INITIAL_EMPLOYEES_STORE: Employee[] = [ | ||
{ | ||
id: 1, | ||
name: 'Edward Perry', | ||
age: 25, | ||
joinDate: new Date().toISOString(), | ||
role: 'Finance', | ||
}, | ||
{ | ||
id: 2, | ||
name: 'Josephine Drake', | ||
age: 36, | ||
joinDate: new Date().toISOString(), | ||
role: 'Market', | ||
}, | ||
{ | ||
id: 3, | ||
name: 'Cody Phillips', | ||
age: 19, | ||
joinDate: new Date().toISOString(), | ||
role: 'Development', | ||
}, | ||
]; | ||
|
||
const getEmployeesStore = (): Employee[] => { | ||
const value = localStorage.getItem('employees-store'); | ||
return value ? JSON.parse(value) : INITIAL_EMPLOYEES_STORE; | ||
}; | ||
|
||
const setEmployeesStore = (value: Employee[]) => { | ||
return localStorage.setItem('employees-store', JSON.stringify(value)); | ||
}; | ||
|
||
export const employeesDataSource: DataSource<Employee> = { | ||
fields: [ | ||
{ field: 'id', headerName: 'ID' }, | ||
{ field: 'name', headerName: 'Name', width: 140 }, | ||
{ field: 'age', headerName: 'Age', type: 'number' }, | ||
{ | ||
field: 'joinDate', | ||
headerName: 'Join date', | ||
type: 'date', | ||
valueGetter: (value: string) => value && new Date(value), | ||
width: 140, | ||
}, | ||
{ | ||
field: 'role', | ||
headerName: 'Department', | ||
type: 'singleSelect', | ||
valueOptions: ['Market', 'Finance', 'Development'], | ||
width: 160, | ||
}, | ||
], | ||
getMany: async ({ paginationModel, filterModel, sortModel }) => { | ||
// Simulate loading delay | ||
await new Promise((resolve) => { | ||
setTimeout(resolve, 750); | ||
}); | ||
|
||
const employeesStore = getEmployeesStore(); | ||
|
||
let filteredEmployees = [...employeesStore]; | ||
|
||
// Apply filters (example only) | ||
if (filterModel?.items?.length) { | ||
filterModel.items.forEach(({ field, value, operator }) => { | ||
if (!field || value == null) { | ||
return; | ||
} | ||
|
||
filteredEmployees = filteredEmployees.filter((employee) => { | ||
const employeeValue = employee[field]; | ||
|
||
switch (operator) { | ||
case 'contains': | ||
return String(employeeValue).toLowerCase().includes(String(value).toLowerCase()); | ||
case 'equals': | ||
return employeeValue === value; | ||
case 'startsWith': | ||
return String(employeeValue).toLowerCase().startsWith(String(value).toLowerCase()); | ||
case 'endsWith': | ||
return String(employeeValue).toLowerCase().endsWith(String(value).toLowerCase()); | ||
case '>': | ||
return (employeeValue as number) > value; | ||
case '<': | ||
return (employeeValue as number) < value; | ||
default: | ||
return true; | ||
} | ||
}); | ||
}); | ||
} | ||
|
||
// Apply sorting | ||
if (sortModel?.length) { | ||
filteredEmployees.sort((a, b) => { | ||
for (const { field, sort } of sortModel) { | ||
if ((a[field] as number) < (b[field] as number)) { | ||
return sort === 'asc' ? -1 : 1; | ||
} | ||
if ((a[field] as number) > (b[field] as number)) { | ||
return sort === 'asc' ? 1 : -1; | ||
} | ||
} | ||
return 0; | ||
}); | ||
} | ||
|
||
// Apply pagination | ||
const start = paginationModel.page * paginationModel.pageSize; | ||
const end = start + paginationModel.pageSize; | ||
const paginatedEmployees = filteredEmployees.slice(start, end); | ||
|
||
return { | ||
items: paginatedEmployees, | ||
itemCount: filteredEmployees.length, | ||
}; | ||
}, | ||
getOne: async (employeeId) => { | ||
// Simulate loading delay | ||
await new Promise((resolve) => { | ||
setTimeout(resolve, 750); | ||
}); | ||
|
||
const employeesStore = getEmployeesStore(); | ||
|
||
const employeeToShow = employeesStore.find((employee) => employee.id === Number(employeeId)); | ||
|
||
if (!employeeToShow) { | ||
throw new Error('Employee not found'); | ||
} | ||
return employeeToShow; | ||
}, | ||
createOne: async (data) => { | ||
// Simulate loading delay | ||
await new Promise((resolve) => { | ||
setTimeout(resolve, 750); | ||
}); | ||
|
||
const employeesStore = getEmployeesStore(); | ||
|
||
const newEmployee = { id: employeesStore.length + 1, ...data } as Employee; | ||
|
||
setEmployeesStore([...employeesStore, newEmployee]); | ||
|
||
return newEmployee; | ||
}, | ||
updateOne: async (employeeId, data) => { | ||
// Simulate loading delay | ||
await new Promise((resolve) => { | ||
setTimeout(resolve, 750); | ||
}); | ||
|
||
const employeesStore = getEmployeesStore(); | ||
|
||
let updatedEmployee: Employee | null = null; | ||
|
||
setEmployeesStore( | ||
employeesStore.map((employee) => { | ||
if (employee.id === Number(employeeId)) { | ||
updatedEmployee = { ...employee, ...data }; | ||
return updatedEmployee; | ||
} | ||
return employee; | ||
}), | ||
); | ||
|
||
if (!updatedEmployee) { | ||
throw new Error('Employee not found'); | ||
} | ||
return updatedEmployee; | ||
}, | ||
deleteOne: async (employeeId) => { | ||
// Simulate loading delay | ||
await new Promise((resolve) => { | ||
setTimeout(resolve, 750); | ||
}); | ||
|
||
const employeesStore = getEmployeesStore(); | ||
|
||
setEmployeesStore(employeesStore.filter((employee) => employee.id !== Number(employeeId))); | ||
}, | ||
validate: z.object({ | ||
name: z.string({ required_error: 'Name is required' }).nonempty('Name is required'), | ||
age: z.number({ required_error: 'Age is required' }).min(18, 'Age must be at least 18'), | ||
joinDate: z | ||
.string({ required_error: 'Join date is required' }) | ||
.nonempty('Join date is required'), | ||
role: z.enum(['Market', 'Finance', 'Development'], { | ||
errorMap: () => ({ | ||
message: 'Role must be "Market", "Finance" or "Development"', | ||
}), | ||
}), | ||
})['~standard'].validate, | ||
}; | ||
|
||
export const employeesCache = new DataSourceCache(); |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'll expose the
dataGrid
slot to theCrud
component so we can just use that component here.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feel free to just add a TODO comment here for this for now as i guess we will only be able to adjust here after the next release once we merge #4786