Skip to content

Commit

Permalink
feat: pass uploads to onSubmit
Browse files Browse the repository at this point in the history
  • Loading branch information
jordan-a-young committed Dec 16, 2024
1 parent c2db0c0 commit e894489
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 9 deletions.
2 changes: 1 addition & 1 deletion packages/file-selector/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"@availity/mui-list": "workspace:^",
"@availity/mui-progress": "workspace:^",
"@availity/mui-typography": "workspace:^",
"@availity/upload-core": "7.0.0-alpha.3",
"@availity/upload-core": "7.0.0-alpha.5",
"@tanstack/react-query": "^4.36.1",
"react-dropzone": "^11.7.1",
"react-hook-form": "^7.51.3",
Expand Down
14 changes: 11 additions & 3 deletions packages/file-selector/src/lib/FileSelector.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { ChangeEvent, ReactNode, useState } from 'react';
import { useForm, FormProvider } from 'react-hook-form';
import type { FileRejection } from 'react-dropzone/typings/react-dropzone';
import { useQueryClient } from '@tanstack/react-query';
import Upload, { UploadOptions } from '@availity/upload-core';
import { Button } from '@availity/mui-button';
import { Grid } from '@availity/mui-layout';
Expand Down Expand Up @@ -35,7 +36,7 @@ export type FileSelectorProps = {
onChange?: (event: ChangeEvent<HTMLInputElement>) => void;
// onDeliveryError?: (error: unknown) => void;
// onDeliverySuccess?: () => void;
onSubmit?: (values: Record<string, File[]>) => void;
onSubmit?: (uploads: Upload[], values: Record<string, File[]>) => void;
onSuccess?: UploadOptions['onSuccess'];
onError?: UploadOptions['onError'];
onFilePreUpload?: (() => boolean)[];
Expand Down Expand Up @@ -70,6 +71,8 @@ export const FileSelector = ({
const [totalSize, setTotalSize] = useState(0);
const [fileRejections, setFileRejections] = useState<(FileRejection & { id: number })[]>([]);

const client = useQueryClient();

const methods = useForm({
defaultValues: {
[name]: [] as File[],
Expand Down Expand Up @@ -112,11 +115,16 @@ export const FileSelector = ({
const handleOnSubmit = (values: Record<string, File[]>) => {
if (values[name].length === 0) return;

if (onSubmit) onSubmit(values);
const queries = client.getQueriesData<Upload>(['upload']);
const uploads = [];
for (const [, data] of queries) {
if (data) uploads.push(data);
}

if (onSubmit) onSubmit(uploads, values);
};

const handleRemoveRejection = (id: number) => {
console.log('i:', id);
const rejections = fileRejections.filter((value) => value.id !== id);
setFileRejections(rejections);
};
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -591,7 +591,7 @@ __metadata:
"@availity/mui-list": "workspace:^"
"@availity/mui-progress": "workspace:^"
"@availity/mui-typography": "workspace:^"
"@availity/upload-core": 7.0.0-alpha.3
"@availity/upload-core": 7.0.0-alpha.5
"@mui/material": ^5.15.15
"@tanstack/react-query": ^4.36.1
react: 18.2.0
Expand Down Expand Up @@ -1137,13 +1137,13 @@ __metadata:
languageName: unknown
linkType: soft

"@availity/upload-core@npm:7.0.0-alpha.3":
version: 7.0.0-alpha.3
resolution: "@availity/upload-core@npm:7.0.0-alpha.3"
"@availity/upload-core@npm:7.0.0-alpha.5":
version: 7.0.0-alpha.5
resolution: "@availity/upload-core@npm:7.0.0-alpha.5"
dependencies:
"@availity/resolve-url": 3.0.0
tus-js-client: 4.2.3
checksum: 3b64315218748de52fd6b5a9c0ca14b4265a133f458b84f973f314a50316d5a7ac5f7169bf7a8af4ae08842c6d429b6c9b00a600b31650abb11f1d363bd48265
checksum: 0e26f04ab351101bdf00a9e9237c545a48c96c6e11bdb3541dfc8a7d531b9e28cbb50d545938aec7a6f3df6fa7144e248e226e5767c1c0df1ad9aa2416534a21
languageName: node
linkType: hard

Expand Down

0 comments on commit e894489

Please sign in to comment.