Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/Upload_media #74

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added public/images/pdf.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/play.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added public/images/sheets.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
81 changes: 71 additions & 10 deletions src/modules/details/components/ImageTag.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,85 @@ import { s3GetPresignedUrl} from 'apis/utils/mediaUpload/awsmedia';
import Loader from 'modules/Auth/components/Loader';
import { useEffect, useState } from 'react';

export const ImageS3Tag = ({ path }) => {
const [srcImg, setSrcImg] = useState<string | undefined>();
export const MediaS3Tag = ({ path }: { path: string }) => {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the type definition affects readability in this case

  1. Define individual parameters with individual types
export const MediaS3Tag = (path: string) => {
  1. Define an interface for parameter object
interface MediaS3TagParams {
  path: string;
}

export const MediaS3Tag = ({path}: MediaS3TagParams) => {

const [srcFile, setSrcFile] = useState<string | undefined>();
const [isLoading, setIsLoading] = useState<Boolean>(false);
const [fileType, setFileType] = useState<string | undefined>();

useEffect(() => {
setIsLoading(true);
s3GetPresignedUrl(path)
.then((img) => {
.then((assetUrl) => {
setIsLoading(false);
setSrcImg(img);
setSrcFile(assetUrl);
const baseUrl = assetUrl.split('?')[0];
const extension = baseUrl.split('.').pop()?.toLowerCase();

if (extension === 'pdf') {
setFileType('pdf');
} else if (['jpg', 'jpeg', 'png', 'gif'].includes(extension || '')) {
setFileType('image');
} else if (['xls', 'xlsx'].includes(extension)) {
setFileType('excel');
} else if (['mp4', 'webm', 'ogg'].includes(extension)) {
setFileType('video');
} else {
setFileType('unsupported');
}
})
.catch((e) => {
console.log('unable to fetch', e);
setIsLoading(false);
});
}, []);
return isLoading ? (
<CircularProgress />
) : (
<img src={srcImg} alt='Image' width={'200px'} height={'200px'}></img>
);
}, [path]);

if (isLoading) {
return <CircularProgress />;
}

if (fileType === 'pdf') {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should'nt be dependent on file extension to determine file type.

return (
<img
src="/images/pdf.png"
alt="PDF Thumbnail"
width="50px"
height="50px"
onClick={() => window.open(srcFile, '_blank')}
style={{ cursor: 'pointer' }}
/>
);
} else if (fileType === 'image') {
return (
<img
src={srcFile}
alt="File"
width="200px"
height="200px"
/>
);
} else if (fileType === 'excel') {
return (
<img
src="/images/sheets.png"
alt="Excel Thumbnail"
width="50px"
height="50px"
onClick={() => window.open(srcFile, '_blank')}
style={{ cursor: 'pointer' }}
/>
);
} else if (fileType === 'video') {
const videoType = srcFile?.split('.').pop()?.toLowerCase() === 'mp4' ? 'video/mp4' : 'video/webm';
return (
<video
width="400"
controls
style={{ cursor: 'pointer' }}>
<source src={srcFile} type={videoType} />
Your browser does not support the video tag.
</video>
);
} else {
return <p>Unsupported file type</p>;
}
};
4 changes: 2 additions & 2 deletions src/modules/details/components/Timeline.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { Box, Chip, Paper, Typography } from '@mui/material';
import { DateFormate, getLastDaysFrom } from 'apis/utils/date.utils';
import { ticketStatusColours } from '../constants';
import { ITicketActivity } from '../type';
import { ImageS3Tag } from './ImageTag';
import { MediaS3Tag } from './ImageTag';

export const TimelineComponent = ({ activities }: any) => {
return (
Expand Down Expand Up @@ -76,7 +76,7 @@ const TimeLineDescription = ({ activity }: { activity: ITicketActivity }) => {
</p>
)}
{activity?.asset_url?.map((item) => (
<ImageS3Tag path={item as string} />
<MediaS3Tag path={item as string} />
))}
</div>
</Paper>
Expand Down
8 changes: 4 additions & 4 deletions src/modules/details/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import EditIcon from '@mui/icons-material/Edit';
import { ticketStatusColours } from './constants';


import { ImageS3Tag } from './components/ImageTag';
import { MediaS3Tag } from './components/ImageTag';
import { UserContext } from 'App';
import { ROLES } from 'routes/roleConstants';

Expand Down Expand Up @@ -133,14 +133,14 @@ function Details() {
</TableCell>
</TableRow>
<TableRow>
<TableCell>Image</TableCell>
<TableCell>Attachment</TableCell>
<TableCell>
<Box
display='flex'
sx={{ width: '300px', overflowX: 'scroll' }}
sx={{ width: '300px' }}
>
{ticket?.asset_url?.map((item) => (
<ImageS3Tag path={item} />
<MediaS3Tag path={item} />
))}
</Box>
</TableCell>
Expand Down
17 changes: 13 additions & 4 deletions src/modules/shared/UploadBucket.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import { Box } from '@mui/system';
import { IconButton, Typography } from '@mui/material';
import CloseIcon from '@mui/icons-material/Close';
import { useState } from 'react';
import { ALLOWED_TYPES } from './constants';
import { ALLOWED_TYPES, MAX_FILE_SIZE } from './constants';

export const UploadBucket = ({
isLoading,
Expand Down Expand Up @@ -32,11 +32,20 @@ export const UploadBucket = ({

if (invalidFile) {
setErrorMessage(`Invalid file type: ${invalidFile.name}`);
} else {
setErrorMessage(null);
handleChange(selectedFiles);
return;
}

const oversizedFile = selectedFiles.find((file) => file.size > MAX_FILE_SIZE);

if (oversizedFile) {
setErrorMessage(`File size exceeds limit: ${oversizedFile.name}`);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

File exceeds size limit seems more clear?

return;
}

setErrorMessage(null);
handleChange(selectedFiles);
};

return (
<Box>
<input
Expand Down
6 changes: 5 additions & 1 deletion src/modules/shared/constants.ts
Original file line number Diff line number Diff line change
@@ -1 +1,5 @@
export const ALLOWED_TYPES = ['image/png', 'image/jpeg', 'image/jpg'];
export const ALLOWED_TYPES = ['image/png', 'image/jpeg', 'image/jpg', 'application/pdf','video/mp4', 'vedio/webm', 'vedio/ogg', 'application/vnd.ms-excel',
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'];

export const MAX_FILE_SIZE = 5 * 1024 * 1024;