// ** React Imports
import { useEffect, useState, useCallback, ChangeEvent } from 'react'
// ** MUI Imports
import Box from '@mui/material/Box'
import Card from '@mui/material/Card'
import Typography from '@mui/material/Typography'
import CardHeader from '@mui/material/CardHeader'
import { DataGrid, GridColDef, GridRenderCellParams, GridSortModel } from '@mui/x-data-grid'
// ** ThirdParty Components
import axios from 'axios'
// ** Custom Components
import CustomChip from 'src/@core/components/mui/chip'
import CustomAvatar from 'src/@core/components/mui/avatar'
import ServerSideToolbar from 'src/views/table/data-grid/ServerSideToolbar'
// ** Types Imports
import { ThemeColor } from 'src/@core/layouts/types'
import { DataGridRowType } from 'src/@fake-db/types'
// ** Utils Import
import { getInitials } from 'src/@core/utils/get-initials'
interface StatusObj {
[key: number]: {
title: string
color: ThemeColor
}
}
type SortType = 'asc' | 'desc' | undefined | null
// ** renders client column
const renderClient = (params: GridRenderCellParams) => {
const { row } = params
const stateNum = Math.floor(Math.random() * 6)
const states = ['success', 'error', 'warning', 'info', 'primary', 'secondary']
const color = states[stateNum]
if (row.avatar.length) {
return
} else {
return (
{getInitials(row.full_name ? row.full_name : 'John Doe')}
)
}
}
const statusObj: StatusObj = {
1: { title: 'current', color: 'primary' },
2: { title: 'professional', color: 'success' },
3: { title: 'rejected', color: 'error' },
4: { title: 'resigned', color: 'warning' },
5: { title: 'applied', color: 'info' }
}
const columns: GridColDef[] = [
{
flex: 0.25,
minWidth: 290,
field: 'full_name',
headerName: 'Name',
renderCell: (params: GridRenderCellParams) => {
const { row } = params
return (
{renderClient(params)}
{row.full_name}
{row.email}
)
}
},
{
flex: 0.175,
type: 'date',
minWidth: 120,
headerName: 'Date',
field: 'start_date',
valueGetter: params => new Date(params.value),
renderCell: (params: GridRenderCellParams) => (
{params.row.start_date}
)
},
{
flex: 0.175,
minWidth: 110,
field: 'salary',
headerName: 'Salary',
renderCell: (params: GridRenderCellParams) => (
{params.row.salary}
)
},
{
flex: 0.125,
field: 'age',
minWidth: 80,
headerName: 'Age',
renderCell: (params: GridRenderCellParams) => (
{params.row.age}
)
},
{
flex: 0.175,
minWidth: 140,
field: 'status',
headerName: 'Status',
renderCell: (params: GridRenderCellParams) => {
const status = statusObj[params.row.status]
return (
)
}
}
]
const TableServerSide = () => {
// ** States
const [total, setTotal] = useState(0)
const [sort, setSort] = useState('asc')
const [rows, setRows] = useState([])
const [searchValue, setSearchValue] = useState('')
const [sortColumn, setSortColumn] = useState('full_name')
const [paginationModel, setPaginationModel] = useState({ page: 0, pageSize: 7 })
function loadServerRows(currentPage: number, data: DataGridRowType[]) {
return data.slice(currentPage * paginationModel.pageSize, (currentPage + 1) * paginationModel.pageSize)
}
const fetchTableData = useCallback(
async (sort: SortType, q: string, column: string) => {
await axios
.get('/api/table/data', {
params: {
q,
sort,
column
}
})
.then(res => {
setTotal(res.data.total)
setRows(loadServerRows(paginationModel.page, res.data.data))
})
},
// eslint-disable-next-line react-hooks/exhaustive-deps
[paginationModel]
)
useEffect(() => {
fetchTableData(sort, searchValue, sortColumn)
}, [fetchTableData, searchValue, sort, sortColumn])
const handleSortModel = (newModel: GridSortModel) => {
if (newModel.length) {
setSort(newModel[0].sort)
setSortColumn(newModel[0].field)
fetchTableData(newModel[0].sort, searchValue, newModel[0].field)
} else {
setSort('asc')
setSortColumn('full_name')
}
}
const handleSearch = (value: string) => {
setSearchValue(value)
fetchTableData(sort, value, sortColumn)
}
return (
handleSearch(''),
onChange: (event: ChangeEvent) => handleSearch(event.target.value)
}
}}
/>
)
}
export default TableServerSide