muffin-admin 3.13.4 → 3.14.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.
package/dist/types.d.ts CHANGED
@@ -11,6 +11,7 @@ export type AdminAction = {
11
11
  paths: string[];
12
12
  confirm?: string | false;
13
13
  schema?: AdminInput[];
14
+ file?: boolean;
14
15
  };
15
16
  export type AdminField = [string, {
16
17
  source: string;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "muffin-admin",
3
- "version": "3.13.4",
3
+ "version": "3.14.1",
4
4
  "description": "NPM package for https://github.com/klen/muffin-admin",
5
5
  "files": [
6
6
  "src",
@@ -2,6 +2,7 @@ import {
2
2
  Button,
3
3
  FormGroupsProvider,
4
4
  useAugmentedForm,
5
+ useDataProvider,
5
6
  useListContext,
6
7
  useRecordContext,
7
8
  useResourceContext,
@@ -9,7 +10,7 @@ import {
9
10
  } from "react-admin"
10
11
  import { FormProvider } from "react-hook-form"
11
12
 
12
- import { Stack } from "@mui/material"
13
+ import { Link, Stack } from "@mui/material"
13
14
  import { useState } from "react"
14
15
  import { buildRA } from "../buildRA"
15
16
  import { AdminModal, PayloadButtons, useConfirmation } from "../common"
@@ -24,12 +25,28 @@ export type ActionPayloadProps = {
24
25
  onHandle: (payload: any) => void
25
26
  }
26
27
 
27
- export function ActionButton({ paths, confirm, ...props }: AdminAction) {
28
+ export function ActionButton({ paths, confirm, file, ...props }: AdminAction) {
28
29
  const translate = useTranslate()
29
30
  const record = useRecordContext()
30
31
  const confirmation = useConfirmation()
31
32
  const path = paths.find((p) => p.includes("{id}")) || paths[paths.length - 1]
32
33
  const { mutate, isPending } = useAction(path)
34
+ const dataprovider = useDataProvider()
35
+
36
+ if (file) {
37
+ return (
38
+ <Button
39
+ component={Link}
40
+ label={props.label}
41
+ onClick={(e) => {
42
+ e.stopPropagation()
43
+ dataprovider.downloadFile(path)
44
+ }}
45
+ >
46
+ {buildIcon(props.icon)}
47
+ </Button>
48
+ )
49
+ }
33
50
 
34
51
  const confirmMessage =
35
52
  typeof confirm === "string" ? translate(confirm) : "Do you confirm this action?"
@@ -1,4 +1,4 @@
1
- import { APIParams, makeRequest, setupAdmin } from "./utils"
1
+ import { APIParams, makeRequest, requestHeaders, setupAdmin } from "./utils"
2
2
 
3
3
  export function MuffinDataprovider(apiUrl: string) {
4
4
  async function request(url: string, options?: APIParams) {
@@ -87,6 +87,20 @@ export function MuffinDataprovider(apiUrl: string) {
87
87
  })
88
88
  return { data: json }
89
89
  },
90
+
91
+ downloadFile: async (path: string) => {
92
+ path = path.replace(/^\/+/, "")
93
+ const response = await fetch(`${apiUrl}/${path}`, {
94
+ headers: requestHeaders,
95
+ })
96
+ const url = window.URL.createObjectURL(await response.blob())
97
+ const link = document.createElement("a")
98
+ link.href = url
99
+ link.setAttribute("download", path)
100
+ link.click()
101
+ // no need to append link as child to body.
102
+ setTimeout(() => window.URL.revokeObjectURL(url), 0) // this is important too, otherwise we will be unnecessarily spiking memory!
103
+ },
90
104
  }
91
105
  return methods
92
106
  }
package/src/types.ts CHANGED
@@ -12,6 +12,7 @@ export type AdminAction = {
12
12
  paths: string[]
13
13
  confirm?: string | false
14
14
  schema?: AdminInput[]
15
+ file?: boolean
15
16
  }
16
17
 
17
18
  export type AdminField = [string, { source: string }]