feat: role system
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
import { DataGrid, GridColDef, GridRenderCellParams } from "@mui/x-data-grid";
|
||||
import { ruRU } from "@mui/x-data-grid/locales";
|
||||
import {
|
||||
authStore,
|
||||
cityStore,
|
||||
languageStore,
|
||||
sightsStore,
|
||||
@@ -15,7 +16,6 @@ import { Box, CircularProgress } from "@mui/material";
|
||||
|
||||
export const SightListPage = observer(() => {
|
||||
const { sights, getSights, deleteListSight } = sightsStore;
|
||||
const { cities, getCities } = cityStore;
|
||||
const navigate = useNavigate();
|
||||
const [isDeleteModalOpen, setIsDeleteModalOpen] = useState(false);
|
||||
const [isBulkDeleteModalOpen, setIsBulkDeleteModalOpen] = useState(false);
|
||||
@@ -27,13 +27,20 @@ export const SightListPage = observer(() => {
|
||||
pageSize: 50,
|
||||
});
|
||||
const { language } = languageStore;
|
||||
const canReadCities = authStore.canRead("cities");
|
||||
|
||||
useEffect(() => {
|
||||
const fetchSights = async () => {
|
||||
setIsLoading(true);
|
||||
await getCities(language);
|
||||
if (!authStore.me) {
|
||||
await authStore.getMeAction().catch(() => undefined);
|
||||
}
|
||||
if (authStore.canRead("cities")) {
|
||||
await cityStore.getCities(language);
|
||||
} else {
|
||||
await authStore.fetchMeCities().catch(() => undefined);
|
||||
}
|
||||
await getSights();
|
||||
|
||||
setIsLoading(false);
|
||||
};
|
||||
fetchSights();
|
||||
@@ -61,54 +68,59 @@ export const SightListPage = observer(() => {
|
||||
headerName: "Город",
|
||||
flex: 1,
|
||||
renderCell: (params: GridRenderCellParams) => {
|
||||
const lang = language as "ru" | "en" | "zh";
|
||||
const cityName = canReadCities
|
||||
? cityStore.cities[lang]?.data.find((c) => c.id === params.value)?.name
|
||||
: authStore.meCities[lang]?.find((c) => c.city_id === params.value)?.name;
|
||||
return (
|
||||
<div className="w-full h-full flex items-center">
|
||||
{params.value ? (
|
||||
cities[language].data.find((el) => el.id == params.value)?.name
|
||||
) : (
|
||||
<Minus size={20} className="text-red-500" />
|
||||
)}
|
||||
{cityName ?? <Minus size={20} className="text-red-500" />}
|
||||
</div>
|
||||
);
|
||||
},
|
||||
},
|
||||
{
|
||||
...(authStore.canWrite("sights") ? [{
|
||||
field: "actions",
|
||||
headerName: "Действия",
|
||||
align: "center",
|
||||
headerAlign: "center",
|
||||
align: "center" as const,
|
||||
headerAlign: "center" as const,
|
||||
sortable: false,
|
||||
|
||||
renderCell: (params: GridRenderCellParams) => {
|
||||
return (
|
||||
<div className="flex h-full gap-7 justify-center items-center">
|
||||
<button onClick={() => navigate(`/sight/${params.row.id}/edit`)}>
|
||||
<Pencil size={20} className="text-blue-500" />
|
||||
</button>
|
||||
{/* <button onClick={() => navigate(`/sight/${params.row.id}`)}>
|
||||
<Eye size={20} className="text-green-500" />
|
||||
</button> */}
|
||||
<button
|
||||
onClick={() => {
|
||||
setIsDeleteModalOpen(true);
|
||||
setRowId(params.row.id);
|
||||
}}
|
||||
>
|
||||
<Trash2 size={20} className="text-red-500" />
|
||||
</button>
|
||||
</div>
|
||||
);
|
||||
},
|
||||
},
|
||||
renderCell: (params: GridRenderCellParams) => (
|
||||
<div className="flex h-full gap-7 justify-center items-center">
|
||||
<button onClick={() => navigate(`/sight/${params.row.id}/edit`)}>
|
||||
<Pencil size={20} className="text-blue-500" />
|
||||
</button>
|
||||
<button
|
||||
onClick={() => {
|
||||
setIsDeleteModalOpen(true);
|
||||
setRowId(params.row.id);
|
||||
}}
|
||||
>
|
||||
<Trash2 size={20} className="text-red-500" />
|
||||
</button>
|
||||
</div>
|
||||
),
|
||||
}] : []),
|
||||
];
|
||||
|
||||
const filteredSights = useMemo(() => {
|
||||
const { selectedCityId } = selectedCityStore;
|
||||
if (!selectedCityId) {
|
||||
return sights;
|
||||
}
|
||||
return sights.filter((sight: any) => sight.city_id === selectedCityId);
|
||||
}, [sights, selectedCityStore.selectedCityId]);
|
||||
const allowedCityIds = canReadCities
|
||||
? null
|
||||
: authStore.meCities["ru"].map((c) => c.city_id);
|
||||
|
||||
return sights.filter((sight: any) => {
|
||||
if (allowedCityIds && !allowedCityIds.includes(sight.city_id)) {
|
||||
return false;
|
||||
}
|
||||
if (selectedCityId && sight.city_id !== selectedCityId) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
});
|
||||
}, [sights, selectedCityStore.selectedCityId, canReadCities, authStore.meCities]);
|
||||
|
||||
const canWriteSights = authStore.canWrite("sights");
|
||||
|
||||
const rows = filteredSights.map((sight) => ({
|
||||
id: sight.id,
|
||||
@@ -123,13 +135,15 @@ export const SightListPage = observer(() => {
|
||||
<div className="w-full">
|
||||
<div className="flex justify-between items-center mb-10">
|
||||
<h1 className="text-2xl">Достопримечательности</h1>
|
||||
<CreateButton
|
||||
label="Создать достопримечательность"
|
||||
path="/sight/create"
|
||||
/>
|
||||
{canWriteSights && (
|
||||
<CreateButton
|
||||
label="Создать достопримечательность"
|
||||
path="/sight/create"
|
||||
/>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{ids.length > 0 && (
|
||||
{canWriteSights && ids.length > 0 && (
|
||||
<div className="flex justify-end mb-5 duration-300">
|
||||
<button
|
||||
className="px-4 py-2 bg-red-500 text-white rounded flex gap-2 items-center"
|
||||
@@ -144,25 +158,33 @@ export const SightListPage = observer(() => {
|
||||
<DataGrid
|
||||
rows={rows}
|
||||
columns={columns}
|
||||
checkboxSelection
|
||||
checkboxSelection={canWriteSights}
|
||||
disableRowSelectionExcludeModel
|
||||
loading={isLoading}
|
||||
paginationModel={paginationModel}
|
||||
onPaginationModelChange={setPaginationModel}
|
||||
pageSizeOptions={[50]}
|
||||
localeText={ruRU.components.MuiDataGrid.defaultProps.localeText}
|
||||
onRowSelectionModelChange={(newSelection: any) => {
|
||||
if (Array.isArray(newSelection)) {
|
||||
const selectedIds = newSelection.map((id: string | number) => Number(id));
|
||||
setIds(selectedIds);
|
||||
} else if (newSelection && typeof newSelection === 'object' && 'ids' in newSelection) {
|
||||
const idsSet = newSelection.ids as Set<string | number>;
|
||||
const selectedIds = Array.from(idsSet).map((id: string | number) => Number(id));
|
||||
setIds(selectedIds);
|
||||
} else {
|
||||
setIds([]);
|
||||
}
|
||||
}}
|
||||
onRowSelectionModelChange={
|
||||
canWriteSights
|
||||
? (newSelection: any) => {
|
||||
if (Array.isArray(newSelection)) {
|
||||
const selectedIds = newSelection.map(Number);
|
||||
setIds(selectedIds);
|
||||
} else if (
|
||||
newSelection &&
|
||||
typeof newSelection === "object" &&
|
||||
"ids" in newSelection
|
||||
) {
|
||||
const idsSet = newSelection.ids as Set<string | number>;
|
||||
const selectedIds = Array.from(idsSet).map(Number);
|
||||
setIds(selectedIds);
|
||||
} else {
|
||||
setIds([]);
|
||||
}
|
||||
}
|
||||
: undefined
|
||||
}
|
||||
slots={{
|
||||
noRowsOverlay: () => (
|
||||
<Box sx={{ mt: 5, textAlign: "center", color: "text.secondary" }}>
|
||||
|
||||
Reference in New Issue
Block a user