hide specific
data for users
into CustomDataGrid
This commit is contained in:
parent
7fc6c9dc7c
commit
bf73138521
@ -1,45 +1,87 @@
|
|||||||
import {DataGrid, type DataGridProps, type GridColumnVisibilityModel} from '@mui/x-data-grid'
|
import {DataGrid, type DataGridProps, type GridColumnVisibilityModel} from '@mui/x-data-grid'
|
||||||
import {Stack, Box, Button} from '@mui/material'
|
import {Stack, Button} from '@mui/material'
|
||||||
import React, {useState, useEffect} from 'react'
|
import React, {useState, useEffect, useMemo} from 'react'
|
||||||
import Cookies from 'js-cookie'
|
import Cookies from 'js-cookie'
|
||||||
|
|
||||||
interface CustomDataGridProps extends DataGridProps {
|
interface CustomDataGridProps extends DataGridProps {
|
||||||
hasCoordinates?: boolean
|
hasCoordinates?: boolean
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const DEV_FIELDS = ['id', 'city_id', 'carrier_id'] as const
|
||||||
|
|
||||||
export const CustomDataGrid = ({hasCoordinates = false, columns = [], ...props}: CustomDataGridProps) => {
|
export const CustomDataGrid = ({hasCoordinates = false, columns = [], ...props}: CustomDataGridProps) => {
|
||||||
|
const isDev = import.meta.env.DEV
|
||||||
|
|
||||||
const initialShowCoordinates = Cookies.get('showCoordinates') === 'true'
|
const initialShowCoordinates = Cookies.get('showCoordinates') === 'true'
|
||||||
|
const initialShowDevData = Cookies.get('showDevData') === 'true'
|
||||||
|
|
||||||
const [showCoordinates, setShowCoordinates] = useState(initialShowCoordinates)
|
const [showCoordinates, setShowCoordinates] = useState(initialShowCoordinates)
|
||||||
const [columnVisibilityModel, setColumnVisibilityModel] = useState<GridColumnVisibilityModel>({
|
const [showDevData, setShowDevData] = useState(initialShowDevData)
|
||||||
latitude: showCoordinates,
|
|
||||||
longitude: showCoordinates,
|
const availableDevFields = useMemo(() => DEV_FIELDS.filter((field) => columns.some((column) => column.field === field)), [columns])
|
||||||
})
|
|
||||||
|
const initialVisibilityModel = useMemo(() => {
|
||||||
|
const model: GridColumnVisibilityModel = {}
|
||||||
|
|
||||||
|
availableDevFields.forEach((field) => {
|
||||||
|
model[field] = initialShowDevData
|
||||||
|
})
|
||||||
|
|
||||||
|
if (hasCoordinates) {
|
||||||
|
model.latitude = initialShowCoordinates
|
||||||
|
model.longitude = initialShowCoordinates
|
||||||
|
}
|
||||||
|
|
||||||
|
return model
|
||||||
|
}, [availableDevFields, hasCoordinates, initialShowCoordinates, initialShowDevData])
|
||||||
|
|
||||||
|
const [columnVisibilityModel, setColumnVisibilityModel] = useState<GridColumnVisibilityModel>(initialVisibilityModel)
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (hasCoordinates) {
|
setColumnVisibilityModel((prevModel) => {
|
||||||
setColumnVisibilityModel({
|
const newModel = {...prevModel}
|
||||||
latitude: showCoordinates,
|
|
||||||
longitude: showCoordinates,
|
availableDevFields.forEach((field) => {
|
||||||
|
newModel[field] = showDevData
|
||||||
})
|
})
|
||||||
|
|
||||||
|
if (hasCoordinates) {
|
||||||
|
newModel.latitude = showCoordinates
|
||||||
|
newModel.longitude = showCoordinates
|
||||||
|
}
|
||||||
|
|
||||||
|
return newModel
|
||||||
|
})
|
||||||
|
|
||||||
|
if (hasCoordinates) {
|
||||||
Cookies.set('showCoordinates', String(showCoordinates))
|
Cookies.set('showCoordinates', String(showCoordinates))
|
||||||
}
|
}
|
||||||
}, [showCoordinates, hasCoordinates])
|
Cookies.set('showDevData', String(showDevData))
|
||||||
|
}, [showCoordinates, showDevData, hasCoordinates, availableDevFields])
|
||||||
|
|
||||||
const toggleCoordinates = () => {
|
const toggleCoordinates = () => {
|
||||||
setShowCoordinates((prev) => !prev)
|
setShowCoordinates((prev) => !prev)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const toggleDevData = () => {
|
||||||
|
setShowDevData((prev) => !prev)
|
||||||
|
}
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Stack spacing={2}>
|
<Stack spacing={2}>
|
||||||
<DataGrid {...props} columns={columns} columnVisibilityModel={hasCoordinates ? columnVisibilityModel : undefined} onColumnVisibilityModelChange={hasCoordinates ? (model) => setColumnVisibilityModel(model) : undefined} />
|
<DataGrid {...props} columns={columns} columnVisibilityModel={columnVisibilityModel} onColumnVisibilityModelChange={setColumnVisibilityModel} />
|
||||||
|
<Stack direction="row" spacing={2} sx={{mb: 2}}>
|
||||||
{hasCoordinates && (
|
{hasCoordinates && (
|
||||||
<Box sx={{mb: 2}}>
|
|
||||||
<Button variant="contained" onClick={toggleCoordinates}>
|
<Button variant="contained" onClick={toggleCoordinates}>
|
||||||
{showCoordinates ? 'Скрыть координаты' : 'Показать координаты'}
|
{showCoordinates ? 'Скрыть координаты' : 'Показать координаты'}
|
||||||
</Button>
|
</Button>
|
||||||
</Box>
|
)}
|
||||||
)}
|
{isDev && availableDevFields.length > 0 && (
|
||||||
|
<Button variant="contained" onClick={toggleDevData}>
|
||||||
|
{showDevData ? 'Скрыть служебные данные' : 'Показать служебные данные'}
|
||||||
|
</Button>
|
||||||
|
)}
|
||||||
|
</Stack>
|
||||||
</Stack>
|
</Stack>
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
@ -41,10 +41,10 @@ export const MediaList = () => {
|
|||||||
field: 'actions',
|
field: 'actions',
|
||||||
headerName: 'Действия',
|
headerName: 'Действия',
|
||||||
align: 'right',
|
align: 'right',
|
||||||
headerAlign: 'center',
|
headerAlign: 'right',
|
||||||
minWidth: 130,
|
|
||||||
sortable: false,
|
|
||||||
display: 'flex',
|
display: 'flex',
|
||||||
|
flex: 1,
|
||||||
|
sortable: false,
|
||||||
renderCell: function render({row}) {
|
renderCell: function render({row}) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
@ -60,10 +60,10 @@ export const SightList = () => {
|
|||||||
field: 'actions',
|
field: 'actions',
|
||||||
headerName: 'Действия',
|
headerName: 'Действия',
|
||||||
align: 'right',
|
align: 'right',
|
||||||
headerAlign: 'center',
|
headerAlign: 'right',
|
||||||
minWidth: 120,
|
|
||||||
sortable: false,
|
|
||||||
display: 'flex',
|
display: 'flex',
|
||||||
|
flex: 1,
|
||||||
|
sortable: false,
|
||||||
renderCell: function render({row}) {
|
renderCell: function render({row}) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
@ -51,10 +51,11 @@ export const VehicleList = () => {
|
|||||||
field: 'actions',
|
field: 'actions',
|
||||||
headerName: 'Действия',
|
headerName: 'Действия',
|
||||||
align: 'right',
|
align: 'right',
|
||||||
headerAlign: 'center',
|
headerAlign: 'right',
|
||||||
minWidth: 120,
|
|
||||||
sortable: false,
|
|
||||||
display: 'flex',
|
display: 'flex',
|
||||||
|
flex: 1,
|
||||||
|
sortable: false,
|
||||||
|
|
||||||
renderCell: function render({row}) {
|
renderCell: function render({row}) {
|
||||||
return (
|
return (
|
||||||
<>
|
<>
|
||||||
|
Loading…
Reference in New Issue
Block a user