All files / src/components ServerNotices.tsx

97.43% Statements 38/39
100% Branches 2/2
92.85% Functions 13/14
97.14% Lines 34/35

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132                                                3x 11x   11x                 11x                                       3x 6x 6x 6x 6x   6x 6x   6x 1x     5x 2x         1x 1x     1x             5x                   3x 6x 6x 6x 6x 6x 6x 6x   6x   2x         1x 1x 1x     1x         6x                  
import { useState } from "react";
 
import IconCancel from "@mui/icons-material/Cancel";
import MessageIcon from "@mui/icons-material/Message";
import { Dialog, DialogContent, DialogContentText, DialogTitle } from "@mui/material";
import {
  Button,
  RaRecord,
  SaveButton,
  SimpleForm,
  TextInput,
  Toolbar,
  ToolbarProps,
  required,
  useCreate,
  useDataProvider,
  useListContext,
  useNotify,
  useRecordContext,
  useTranslate,
  useUnselectAll,
} from "react-admin";
import { useMutation } from "@tanstack/react-query";
 
const ServerNoticeDialog = ({ open, onClose, onSubmit }) => {
  const translate = useTranslate();
 
  const ServerNoticeToolbar = (props: ToolbarProps & { pristine?: boolean }) => (
    <Toolbar {...props}>
      <SaveButton label="resources.servernotices.action.send" disabled={props.pristine} />
      <Button label="ra.action.cancel" onClick={onClose}>
        <IconCancel />
      </Button>
    </Toolbar>
  );
 
  return (
    <Dialog open={open} onClose={onClose}>
      <DialogTitle>{translate("resources.servernotices.action.send")}</DialogTitle>
      <DialogContent>
        <DialogContentText>{translate("resources.servernotices.helper.send")}</DialogContentText>
        <SimpleForm toolbar={<ServerNoticeToolbar />} onSubmit={onSubmit}>
          <TextInput
            source="body"
            label="resources.servernotices.fields.body"
            multiline
            rows="4"
            resettable
            validate={required()}
          />
        </SimpleForm>
      </DialogContent>
    </Dialog>
  );
};
 
export const ServerNoticeButton = () => {
  const record = useRecordContext();
  const [open, setOpen] = useState(false);
  const notify = useNotify();
  const [create, { isLoading }] = useCreate();
 
  const handleDialogOpen = () => setOpen(true);
  const handleDialogClose = () => setOpen(false);
 
  if (!record) {
    return;
  }
 
  const handleSend = (values: Partial<RaRecord>) => {
    create(
      "servernotices",
      { data: { id: record.id, ...values } },
      {
        onSuccess: () => {
          notify("resources.servernotices.action.send_success");
          handleDialogClose();
        },
        onError: () =>
          notify("resources.servernotices.action.send_failure", {
            type: "error",
          }),
      }
    );
  };
 
  return (
    <>
      <Button label="resources.servernotices.send" onClick={handleDialogOpen} disabled={isLoading}>
        <MessageIcon />
      </Button>
      <ServerNoticeDialog open={open} onClose={handleDialogClose} onSubmit={handleSend} />
    </>
  );
};
 
export const ServerNoticeBulkButton = () => {
  const { selectedIds } = useListContext();
  const [open, setOpen] = useState(false);
  const openDialog = () => setOpen(true);
  const closeDialog = () => setOpen(false);
  const notify = useNotify();
  const unselectAllUsers = useUnselectAll("users");
  const dataProvider = useDataProvider();
 
  const { mutate: sendNotices, isPending } = useMutation({
    mutationFn: (data) =>
      dataProvider.createMany("servernotices", {
        ids: selectedIds,
        data: data,
      }),
    onSuccess: () => {
      notify("resources.servernotices.action.send_success");
      unselectAllUsers();
      closeDialog();
    },
    onError: () =>
      notify("resources.servernotices.action.send_failure", {
        type: "error",
      }),
  });
 
  return (
    <>
      <Button label="resources.servernotices.send" onClick={openDialog} disabled={isPending}>
        <MessageIcon />
      </Button>
      <ServerNoticeDialog open={open} onClose={closeDialog} onSubmit={sendNotices} />
    </>
  );
};