Refactor RoomDirectory

Change-Id: Ie3bd606fc91b2673d2a3422f8fd465258d3211b0
This commit is contained in:
dklimpel 2024-02-06 12:01:25 +01:00 committed by Manuel Stahl
parent 4204eb902f
commit 00ecb29d6b
2 changed files with 42 additions and 45 deletions

View file

@ -13,6 +13,7 @@ import {
TextField, TextField,
TopToolbar, TopToolbar,
useCreate, useCreate,
useDataProvider,
useListContext, useListContext,
useNotify, useNotify,
useTranslate, useTranslate,
@ -28,7 +29,7 @@ const RoomDirectoryPagination = () => (
<Pagination rowsPerPageOptions={[100, 500, 1000, 2000]} /> <Pagination rowsPerPageOptions={[100, 500, 1000, 2000]} />
); );
export const RoomDirectoryDeleteButton = props => { export const RoomDirectoryUnpublishButton = props => {
const translate = useTranslate(); const translate = useTranslate();
return ( return (
@ -49,7 +50,7 @@ export const RoomDirectoryDeleteButton = props => {
); );
}; };
export const RoomDirectoryBulkDeleteButton = props => ( export const RoomDirectoryBulkUnpublishButton = props => (
<BulkDeleteButton <BulkDeleteButton
{...props} {...props}
label="resources.room_directory.action.erase" label="resources.room_directory.action.erase"
@ -61,57 +62,59 @@ export const RoomDirectoryBulkDeleteButton = props => (
/> />
); );
export const RoomDirectoryBulkSaveButton = () => { export const RoomDirectoryBulkPublishButton = props => {
const { selectedIds } = useListContext(); const { selectedIds } = useListContext();
const notify = useNotify(); const notify = useNotify();
const refresh = useRefresh(); const refresh = useRefresh();
const unselectAllRooms = useUnselectAll("rooms"); const unselectAllRooms = useUnselectAll("rooms");
const { createMany, isloading } = useMutation(); const dataProvider = useDataProvider();
const { mutate, isLoading } = useMutation(
const handleSend = values => { () =>
createMany( dataProvider.createMany("room_directory", {
["room_directory", "createMany", { ids: selectedIds, data: {} }], ids: selectedIds,
data: {},
}),
{ {
onSuccess: data => { onSuccess: () => {
notify("resources.room_directory.action.send_success"); notify("resources.room_directory.action.send_success");
unselectAllRooms(); unselectAllRooms();
refresh(); refresh();
}, },
onError: error => onError: () =>
notify("resources.room_directory.action.send_failure", { notify("resources.room_directory.action.send_failure", {
type: "error", type: "error",
}), }),
} }
); );
};
return ( return (
<Button <Button
{...props}
label="resources.room_directory.action.create" label="resources.room_directory.action.create"
onClick={handleSend} onClick={mutate}
disabled={isloading} disabled={isLoading}
> >
<RoomDirectoryIcon /> <RoomDirectoryIcon />
</Button> </Button>
); );
}; };
export const RoomDirectorySaveButton = () => { export const RoomDirectoryPublishButton = props => {
const record = useRecordContext(); const record = useRecordContext();
const notify = useNotify(); const notify = useNotify();
const refresh = useRefresh(); const refresh = useRefresh();
const [create, { isloading }] = useCreate(); const [create, { isLoading }] = useCreate();
const handleSend = () => { const handleSend = () => {
create( create(
"room_directory", "room_directory",
{ data: { id: record.id } }, { data: { id: record.id } },
{ {
onSuccess: _data => { onSuccess: () => {
notify("resources.room_directory.action.send_success"); notify("resources.room_directory.action.send_success");
refresh(); refresh();
}, },
onError: _error => onError: () =>
notify("resources.room_directory.action.send_failure", { notify("resources.room_directory.action.send_failure", {
type: "error", type: "error",
}), }),
@ -121,17 +124,16 @@ export const RoomDirectorySaveButton = () => {
return ( return (
<Button <Button
{...props}
label="resources.room_directory.action.create" label="resources.room_directory.action.create"
onClick={handleSend} onClick={handleSend}
disabled={isloading} disabled={isLoading}
> >
<RoomDirectoryIcon /> <RoomDirectoryIcon />
</Button> </Button>
); );
}; };
const RoomDirectoryBulkActionButtons = () => <RoomDirectoryBulkDeleteButton />;
const RoomDirectoryListActions = () => ( const RoomDirectoryListActions = () => (
<TopToolbar> <TopToolbar>
<SelectColumnsButton /> <SelectColumnsButton />
@ -146,8 +148,8 @@ export const RoomDirectoryList = () => (
actions={<RoomDirectoryListActions />} actions={<RoomDirectoryListActions />}
> >
<DatagridConfigurable <DatagridConfigurable
rowClick={(id, resource, record) => "/rooms/" + id + "/show"} rowClick={(id, _resource, _record) => "/rooms/" + id + "/show"}
bulkActionButtons={<RoomDirectoryBulkActionButtons />} bulkActionButtons={<RoomDirectoryBulkUnpublishButton />}
omit={["room_id", "canonical_alias", "topic"]} omit={["room_id", "canonical_alias", "topic"]}
> >
<AvatarField <AvatarField

View file

@ -36,10 +36,10 @@ import VisibilityIcon from "@mui/icons-material/Visibility";
import EventIcon from "@mui/icons-material/Event"; import EventIcon from "@mui/icons-material/Event";
import RoomIcon from "@mui/icons-material/ViewList"; import RoomIcon from "@mui/icons-material/ViewList";
import { import {
RoomDirectoryBulkDeleteButton, RoomDirectoryBulkUnpublishButton,
RoomDirectoryBulkSaveButton, RoomDirectoryBulkPublishButton,
RoomDirectoryDeleteButton, RoomDirectoryUnpublishButton,
RoomDirectorySaveButton, RoomDirectoryPublishButton,
} from "./RoomDirectory"; } from "./RoomDirectory";
const date_format = { const date_format = {
@ -79,14 +79,9 @@ const RoomShowActions = () => {
return ( return (
<TopToolbar> <TopToolbar>
{roomDirectoryStatus === false && ( {roomDirectoryStatus === false && <RoomDirectoryPublishButton />}
<RoomDirectorySaveButton record={record} /> {roomDirectoryStatus === true && <RoomDirectoryUnpublishButton />}
)}
{roomDirectoryStatus === true && (
<RoomDirectoryDeleteButton record={record} />
)}
<DeleteButton <DeleteButton
record={record}
mutationMode="pessimistic" mutationMode="pessimistic"
confirmTitle="resources.rooms.action.erase.title" confirmTitle="resources.rooms.action.erase.title"
confirmContent="resources.rooms.action.erase.content" confirmContent="resources.rooms.action.erase.content"
@ -280,8 +275,8 @@ export const RoomShow = props => {
const RoomBulkActionButtons = () => ( const RoomBulkActionButtons = () => (
<> <>
<RoomDirectoryBulkSaveButton /> <RoomDirectoryBulkPublishButton />
<RoomDirectoryBulkDeleteButton /> <RoomDirectoryBulkUnpublishButton />
<BulkDeleteButton <BulkDeleteButton
confirmTitle="resources.rooms.action.erase.title" confirmTitle="resources.rooms.action.erase.title"
confirmContent="resources.rooms.action.erase.content" confirmContent="resources.rooms.action.erase.content"