Skip to content

Commit

Permalink
fix: ASC-27509 - file upload (#750)
Browse files Browse the repository at this point in the history
* fix: cannot upload the same file again

* fix: show modal to discard files after upload failed
  • Loading branch information
ptchayap authored and ChayanitBm committed Dec 2, 2024
1 parent 475a96f commit 8e9d6a9
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 10 deletions.
5 changes: 4 additions & 1 deletion src/core/components/Uploaders/Loader.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -170,7 +170,10 @@ const FileLoader: React.FC<FileLoaderProps> = ({
accept={mimeType}
multiple={multiple}
disabled={disabled}
onChange={onLoad}
onChange={(e) => {
e.target.value && onLoad(e);
e.target.value = '';
}}
onClick={onClick}
/>
{children}
Expand Down
22 changes: 19 additions & 3 deletions src/social/components/post/Creator/components/FilesUploaded.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import React, { useEffect } from 'react';
import styled from 'styled-components';

import File from '~/core/components/Uploaders/File';
import useFileUpload from '~/core/hooks/useFileUpload';
import { useShallowCompareEffect } from 'react-use';
import { useConfirmContext } from '~/core/providers/ConfirmProvider';

const StylesFileRows = styled.div<{ uploadLoading?: boolean }>`
display: grid;
Expand Down Expand Up @@ -64,7 +65,7 @@ interface FilesProps {
onChange: (data: { uploaded: Array<Amity.File>; uploading: Array<File> }) => void;
onLoadingChange: (loading: boolean) => void;
uploadLoading: boolean;
onError: (error: string) => void;
onError?: (error: string) => void;
rowDataQaAnchor?: string;
}

Expand All @@ -85,7 +86,22 @@ const Files = ({
onError,
});

const { allFiles } = useFileUploadProps;
const { info } = useConfirmContext();

const { allFiles, rejected, reset } = useFileUploadProps;

useEffect(() => {
if (rejected && rejected.length > 0) {
info({
title: 'Error',
content: 'There was an issue uploading file. Please try again later.',
onOk: () => reset(),
okText: 'Discard',
onCancel: () => reset(),
OkButton: undefined,
});
}
}, [rejected]);

if (allFiles.length === 0) return null;

Expand Down
22 changes: 19 additions & 3 deletions src/social/components/post/Creator/components/ImagesUploaded.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import React from 'react';
import React, { useEffect } from 'react';
import styled, { css } from 'styled-components';

import GalleryGrid from '~/core/components/GalleryGrid';
import Image from '~/core/components/Uploaders/Image';
import useFileUpload, { isAmityFile } from '~/core/hooks/useFileUpload';
import { useConfirmContext } from '~/core/providers/ConfirmProvider';

const StyledGalleryGrid = styled(GalleryGrid)<
{ uploadLoading?: boolean } & React.ComponentProps<typeof GalleryGrid<Amity.File | File>>
Expand Down Expand Up @@ -68,7 +69,7 @@ interface ImagesUploadedProps {
onChange: (data: { uploaded: Array<Amity.File>; uploading: Array<File> }) => void;
onLoadingChange: (loading: boolean) => void;
uploadLoading: boolean;
onError: (error: string) => void;
onError?: (error: string) => void;
}

const ImagesUploaded = ({
Expand All @@ -87,7 +88,22 @@ const ImagesUploaded = ({
onError,
});

const { allFiles } = useFileUploadProps;
const { info } = useConfirmContext();

const { allFiles, rejected, reset } = useFileUploadProps;

useEffect(() => {
if (rejected && rejected.length > 0) {
info({
title: 'Error',
content: 'There was an issue uploading media. Please try again later.',
onOk: () => reset(),
okText: 'Discard',
onCancel: () => reset(),
OkButton: undefined,
});
}
}, [rejected]);

if (allFiles.length === 0) return null;

Expand Down
3 changes: 0 additions & 3 deletions src/social/components/post/Creator/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -361,7 +361,6 @@ const PostCreatorBar = ({
setPostImages(uploaded);
setIncomingImages(uploading);
}}
onError={setError}
/>
<VideosUploaded
files={incomingVideos}
Expand All @@ -372,7 +371,6 @@ const PostCreatorBar = ({
setPostVideos(uploaded);
setIncomingVideos(uploading);
}}
onError={setError}
/>
<FilesUploaded
files={incomingFiles}
Expand All @@ -383,7 +381,6 @@ const PostCreatorBar = ({
setPostFiles(uploaded);
setIncomingFiles(uploading);
}}
onError={setError}
/>
</UploadsContainer>
}
Expand Down

0 comments on commit 8e9d6a9

Please sign in to comment.