@availity/mui-file-selector 0.1.0

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.
@@ -0,0 +1,75 @@
1
+ import { useQuery } from '@tanstack/react-query';
2
+ import { avFilesDeliveryApi } from '@availity/api-axios';
3
+ import Upload from '@availity/upload-core';
4
+ import { AxiosResponse } from 'axios';
5
+
6
+ export type UploadDeliveryOptions = {
7
+ bucketId: string;
8
+ clientId: string;
9
+ customerId: string;
10
+ deliveryChannel?: string;
11
+ deliverFileOnSubmit?: boolean;
12
+ fileDeliveryMetadata?: Record<string, unknown> | ((upload: Upload) => Record<string, unknown>);
13
+ onSuccess?: (responses: unknown[]) => void;
14
+ onError?: (responses: unknown[]) => void;
15
+ uploads: Upload[];
16
+ };
17
+
18
+ export function useFileDelivery({
19
+ bucketId,
20
+ clientId,
21
+ customerId,
22
+ deliveryChannel,
23
+ deliverFileOnSubmit,
24
+ fileDeliveryMetadata,
25
+ onSuccess,
26
+ onError,
27
+ uploads,
28
+ }: UploadDeliveryOptions) {
29
+ const errors = {};
30
+
31
+ const callFileDelivery = async (uploadList: Upload[]) => {
32
+ const results: Promise<AxiosResponse>[] = [];
33
+
34
+ for (const upload of uploadList) {
35
+ const data = {
36
+ deliveries: [
37
+ {
38
+ deliveryChannel,
39
+ fileURI: upload.references[0],
40
+ metadata: typeof fileDeliveryMetadata === 'function' ? fileDeliveryMetadata(upload) : fileDeliveryMetadata,
41
+ },
42
+ ],
43
+ };
44
+
45
+ results.push(
46
+ avFilesDeliveryApi.uploadFilesDelivery(data, {
47
+ clientId,
48
+ customerId,
49
+ })
50
+ );
51
+ }
52
+
53
+ const responses = await Promise.all(results);
54
+
55
+ return responses;
56
+ };
57
+
58
+ const validate = (errors: Record<string, unknown>) => {
59
+ return Object.keys(errors).length === 0;
60
+ };
61
+
62
+ const isQueryEnabled =
63
+ !!customerId &&
64
+ !!clientId &&
65
+ !!bucketId &&
66
+ !!deliveryChannel &&
67
+ deliverFileOnSubmit &&
68
+ uploads.length > 0 &&
69
+ validate(errors);
70
+
71
+ return useQuery(['file-delivery', customerId, clientId, bucketId], () => callFileDelivery(uploads), {
72
+ enabled: isQueryEnabled,
73
+ retry: false,
74
+ });
75
+ }
@@ -0,0 +1,23 @@
1
+ import { useQuery } from '@tanstack/react-query';
2
+ import Upload, { Options } from '@availity/upload-core';
3
+
4
+ function startUploads(files: File[], options: Options) {
5
+ return files.map((file) => {
6
+ const upload = new Upload(file, options);
7
+
8
+ upload.start();
9
+
10
+ return upload;
11
+ });
12
+ }
13
+
14
+ export function useUploadCore(files: File[], options: Options) {
15
+ const fileNames = files.map((file) => file.name).join(',');
16
+
17
+ const isQueryEnabled = files.length > 0;
18
+
19
+ return useQuery(['upload', fileNames, options], () => startUploads(files, options), {
20
+ enabled: isQueryEnabled,
21
+ retry: false,
22
+ });
23
+ }
@@ -0,0 +1,42 @@
1
+ export function formatBytes(bytes: number, decimals = 2) {
2
+ if (!+bytes) return '0 Bytes';
3
+
4
+ const k = 1000;
5
+ const dm = decimals < 0 ? 0 : decimals;
6
+ const sizes = ['Bytes', 'KB', 'MB', 'GB'];
7
+
8
+ const i = Math.floor(Math.log(bytes) / Math.log(k));
9
+
10
+ return `${parseFloat((bytes / Math.pow(k, i)).toFixed(dm))} ${sizes[i]}`;
11
+ }
12
+
13
+ export const FILE_EXT_ICONS = {
14
+ png: 'file-image',
15
+ jpg: 'file-image',
16
+ jpeg: 'file-image',
17
+ gif: 'file-image',
18
+ ppt: 'file-powerpoint',
19
+ pptx: 'file-powerpoint',
20
+ xls: 'file-excel',
21
+ xlsx: 'file-excel',
22
+ doc: 'file-word',
23
+ docx: 'file-word',
24
+ txt: 'doc-alt',
25
+ text: 'doc-alt',
26
+ zip: 'file-archive',
27
+ '7zip': 'file-archive',
28
+ xml: 'file-code',
29
+ html: 'file-code',
30
+ pdf: 'file-pdf',
31
+ } as const;
32
+
33
+ export type FileExtensionKey = keyof typeof FILE_EXT_ICONS;
34
+
35
+ export const isValidKey = (key: string): key is FileExtensionKey => (key ? key in FILE_EXT_ICONS : false);
36
+
37
+ export const getFileExtIcon = (fileName: string) => {
38
+ const ext = fileName.split('.').pop()?.toLowerCase() || '';
39
+ const icon = isValidKey(ext) ? FILE_EXT_ICONS[ext] : 'doc';
40
+
41
+ return { ext, icon };
42
+ };
package/tsconfig.json ADDED
@@ -0,0 +1,5 @@
1
+ {
2
+ "extends": "../../tsconfig.base.json",
3
+ "include": ["."],
4
+ "exclude": ["dist", "build", "node_modules"]
5
+ }
@@ -0,0 +1,10 @@
1
+ {
2
+ "extends": "./tsconfig.json",
3
+ "compilerOptions": {
4
+ "outDir": "../../dist/out-tsc",
5
+ "module": "commonjs",
6
+ "types": ["jest", "node", "@testing-library/jest-dom"],
7
+ "allowJs": true
8
+ },
9
+ "include": ["**/*.test.js", "**/*.test.ts", "**/*.test.tsx", "**/*.d.ts"]
10
+ }