muffin-admin 3.5.0 → 3.6.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -3,7 +3,7 @@ import sortBy from "lodash/sortBy";
3
3
  import uniq from "lodash/uniq";
4
4
  import { BulkDeleteButton, BulkExportButton, CreateButton, DatagridConfigurable, EditButton, ExportButton, FilterButton, List, Pagination, SelectColumnsButton, TopToolbar, } from "react-admin";
5
5
  import { buildRA } from "./buildRA";
6
- import { BulkActionButton } from "./buttons";
6
+ import { ActionButton, BulkActionButton } from "./buttons";
7
7
  import { useMuffinResourceOpts } from "./hooks";
8
8
  import { buildAdmin, findBuilder, setupAdmin } from "./utils";
9
9
  export function MuffinList() {
@@ -32,13 +32,14 @@ function MuffinListGridButtons() {
32
32
  }
33
33
  setupAdmin(["list-grid-buttons"], MuffinListGridButtons);
34
34
  function MuffinListToolbar() {
35
- const { list: { create }, } = useMuffinResourceOpts();
36
- return (_jsxs(TopToolbar, { children: [_jsx(SelectColumnsButton, {}), _jsx(FilterButton, {}), create && _jsx(CreateButton, {}), _jsx(ExportButton, {})] }));
35
+ const { actions: baseActions = [], list: { create }, } = useMuffinResourceOpts();
36
+ const actions = baseActions.filter((a) => a.view.includes("list"));
37
+ return (_jsxs(TopToolbar, { children: [_jsx(SelectColumnsButton, {}), _jsx(FilterButton, {}), actions.length && actions.map((props) => _jsx(ActionButton, Object.assign({}, props), props.id)), create && _jsx(CreateButton, {}), _jsx(ExportButton, {})] }));
37
38
  }
38
39
  setupAdmin(["list-toolbar"], MuffinListToolbar);
39
40
  function MuffinListBulkActions() {
40
41
  const { actions: baseActions = [], list: { remove }, } = useMuffinResourceOpts();
41
- const actions = baseActions.filter((a) => a.view.includes("list"));
42
+ const actions = baseActions.filter((a) => a.view.includes("bulk"));
42
43
  return (_jsxs(_Fragment, { children: [actions.map((props) => (_jsx(BulkActionButton, Object.assign({}, props), props.id))), _jsx(BulkExportButton, {}), remove && _jsx(BulkDeleteButton, {})] }));
43
44
  }
44
45
  setupAdmin(["list-bulk-actions"], MuffinListBulkActions);
@@ -4,5 +4,12 @@ export type ActionPayloadProps = {
4
4
  onClose: () => void;
5
5
  onHandle: (payload: any) => void;
6
6
  };
7
- export declare function ActionButton({ icon, label, title, action, id, confirm }: AdminAction): import("react/jsx-runtime").JSX.Element;
7
+ export declare function ActionButton({ icon, label, title, action, id, confirm, schema }: AdminAction): import("react/jsx-runtime").JSX.Element;
8
8
  export declare function BulkActionButton({ label, icon, title, action, id, confirm }: AdminAction): import("react/jsx-runtime").JSX.Element;
9
+ export declare function CommonPayload({ active, onClose, onHandle, schema, title }: {
10
+ active: any;
11
+ onClose: any;
12
+ onHandle: any;
13
+ schema: any;
14
+ title: any;
15
+ }): import("react/jsx-runtime").JSX.Element;
@@ -8,35 +8,32 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
8
8
  });
9
9
  };
10
10
  import { jsx as _jsx, Fragment as _Fragment, jsxs as _jsxs } from "react/jsx-runtime";
11
- import { Button, useListContext, useRecordContext, useResourceContext, useTranslate, } from "react-admin";
11
+ import { Button, Form, useListContext, useRecordContext, useResourceContext, useTranslate, } from "react-admin";
12
12
  import { useMemo, useState } from "react";
13
- import { useConfirmation } from "../common";
13
+ import { buildRA } from "../buildRA";
14
+ import { AdminModal, PayloadButtons, useConfirmation } from "../common";
14
15
  import { useAction } from "../hooks/useAction";
15
16
  import { buildIcon, findBuilder } from "../utils";
16
- export function ActionButton({ icon, label, title, action, id, confirm }) {
17
+ export function ActionButton({ icon, label, title, action, id, confirm, schema }) {
17
18
  const translate = useTranslate();
18
19
  const record = useRecordContext();
19
20
  const resource = useResourceContext();
20
21
  const confirmation = useConfirmation();
21
22
  const { mutate, isPending } = useAction(action);
22
23
  const [payloadActive, setPayloadActive] = useState(false);
23
- const [payloadResolver, setPayloadResolver] = useState();
24
- const PayloadBuilder = findBuilder(["action", "payload", id, resource]);
25
- const buildPayload = useMemo(() => {
26
- if (PayloadBuilder) {
27
- return new Promise((resolve) => setPayloadResolver(() => resolve));
28
- }
29
- return Promise.resolve();
30
- }, [PayloadBuilder]);
24
+ const PayloadBuilder = findBuilder(["action", "payload", id, resource]) || (schema && CommonPayload);
31
25
  const confirmMessage = typeof confirm === "string" ? translate(confirm) : "Do you confirm this action?";
32
- return (_jsxs(_Fragment, { children: [_jsx(Button, { label: label, title: title, onClick: () => __awaiter(this, void 0, void 0, function* () {
26
+ const onHandle = (payload) => __awaiter(this, void 0, void 0, function* () {
27
+ const process = confirm ? yield confirmation.confirm({ message: confirmMessage }) : true;
28
+ if (process)
29
+ yield mutate({ record, payload });
30
+ });
31
+ return (_jsxs(_Fragment, { children: [_jsx(Button, { label: label, title: title, onClick: () => {
33
32
  if (PayloadBuilder)
34
33
  setPayloadActive(true);
35
- const payload = yield buildPayload;
36
- const process = confirm ? yield confirmation.confirm({ message: confirmMessage }) : true;
37
- if (process)
38
- yield mutate({ record, payload });
39
- }), disabled: isPending, children: buildIcon(icon) }), PayloadBuilder && (_jsx(PayloadBuilder, { active: payloadActive, onHandle: payloadResolver, onClose: () => setPayloadActive(false) }))] }));
34
+ else
35
+ onHandle();
36
+ }, disabled: isPending, children: buildIcon(icon) }), PayloadBuilder && (_jsx(PayloadBuilder, { active: payloadActive, onHandle: onHandle, onClose: () => setPayloadActive(false), title: title || label, schema: schema }))] }));
40
37
  }
41
38
  export function BulkActionButton({ label, icon, title, action, id, confirm }) {
42
39
  const translate = useTranslate();
@@ -63,3 +60,11 @@ export function BulkActionButton({ label, icon, title, action, id, confirm }) {
63
60
  yield mutate({ ids: selectedIds, payload });
64
61
  }), disabled: isPending, children: buildIcon(icon) }), PayloadBuilder && (_jsx(PayloadBuilder, { active: payloadActive, onHandle: payloadResolver, onClose: () => setPayloadActive(false) }))] }));
65
62
  }
63
+ export function CommonPayload({ active, onClose, onHandle, schema, title }) {
64
+ const inputs = buildRA(schema);
65
+ const translate = useTranslate();
66
+ return (_jsx(AdminModal, { maxWidth: "sm", open: active, onClose: onClose, title: translate(title, { _: title }), children: _jsxs(Form, { onSubmit: (data) => {
67
+ onClose();
68
+ onHandle(data);
69
+ }, children: [inputs, _jsx(PayloadButtons, { onClose: onClose })] }) }));
70
+ }
@@ -0,0 +1,3 @@
1
+ export declare function PayloadButtons({ onClose }: {
2
+ onClose: any;
3
+ }): import("react/jsx-runtime").JSX.Element;
@@ -0,0 +1,8 @@
1
+ import { jsx as _jsx, jsxs as _jsxs } from "react/jsx-runtime";
2
+ import { Stack } from "@mui/material";
3
+ import { Button } from "react-admin";
4
+ import { useFormContext } from "react-hook-form";
5
+ export function PayloadButtons({ onClose }) {
6
+ const { formState } = useFormContext();
7
+ return (_jsxs(Stack, { direction: "row", alignItems: "center", justifyContent: "flex-end", spacing: 1, children: [_jsx(Button, { type: "submit", label: "ra.action.confirm", variant: "contained", disabled: !formState.isValid, size: "medium" }), _jsx(Button, { label: "ra.action.cancel", variant: "outlined", size: "medium", onClick: onClose })] }));
8
+ }
@@ -1,2 +1,3 @@
1
1
  export * from "./AdminModal";
2
2
  export * from "./ConfirmationProvider";
3
+ export * from "./PayloadButtons";
@@ -1,2 +1,3 @@
1
1
  export * from "./AdminModal";
2
2
  export * from "./ConfirmationProvider";
3
+ export * from "./PayloadButtons";