Skip to content

Commit

Permalink
feat: moved all shared code to Convert component (#25)
Browse files Browse the repository at this point in the history
  • Loading branch information
CarlaPaiva authored Jan 13, 2024
1 parent 3cae604 commit 58fefb0
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 34 deletions.
File renamed without changes.
57 changes: 57 additions & 0 deletions src/components/converter/index.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import { memo, useCallback, useState } from "react"
import DraggerUpload from "../dragger-upload"
import { Button, UploadFile } from "antd"
import convertFiles from "../../services/converter"
import { Format } from "../../model/format"
import './converter.css'

/** Properties of Converter */
type ConverterProps = {
/** Define allowed types to be uploaded */
allowedUploadingFormats: Format[]
/** Formats available to be converted at */
optionsToConvertTo: Format[]
}

function Converter(props: ConverterProps): JSX.Element {
const [fileList, setFileList] = useState<UploadFile[]>([])

const onUploadDone = useCallback((files: UploadFile[]) => {
setFileList(files)
}, [])

const downloadBlob = useCallback((blob: Blob) => {
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a")
document.body.appendChild(a)
a.style.display = "none"
a.href = url;
a.target = "_blank"
a.download = 'test.gif';
a.click();
window.URL.revokeObjectURL(url);
}, [])

const convert = useCallback(async () => {
const blobs = await convertFiles(fileList)

for (const blob of blobs) {
downloadBlob(blob)
}
}, [downloadBlob, fileList])

return (
<div>
<DraggerUpload
onUploadDone={onUploadDone} />
<Button
size="large"
type="primary"
className="button-convert"
onClick={convert}>Convert</Button>
</div>
)
}


export default memo(Converter)
54 changes: 20 additions & 34 deletions src/components/video-converter/index.tsx
Original file line number Diff line number Diff line change
@@ -1,42 +1,28 @@
import { useCallback, useState } from "react"
import DraggerUpload from "../dragger-upload"
import { Button, UploadFile } from "antd"
import './video-converter.css'
import convertFiles from "../../services/converter"
import { Format } from "../../model/format"
import Converter from "../converter"

function VideoConverter(): JSX.Element {
const [fileList, setFileList] = useState<UploadFile[]>([])
const uploadingFormat: Format[] = [
{
name: "MP4",
extension: ".mp4",
mimeType: "video/mp4"
}
]

const onUploadDone = useCallback((files: UploadFile[]) => {
setFileList(files)
}, [])
const optionsToConvertTo: Format[] = [
{
name: "GIF",
extension: ".gif",
mimeType: "image/gif"
}
]

const convert = useCallback(async () => {
const blobs = await convertFiles(fileList)
console.log(blobs)
for (const blob of blobs) {
const url = window.URL.createObjectURL(blob);
const a = document.createElement("a")
document.body.appendChild(a)
a.style.display = "none"
a.href = url;
a.target = "_blank"
a.download = 'test.gif';
a.click();
window.URL.revokeObjectURL(url);
}
}, [fileList])
function VideoConverter(): JSX.Element {

return (
<div>
<DraggerUpload
onUploadDone={onUploadDone} />
<Button
size="large"
type="primary"
className="button-convert"
onClick={convert}>Convert</Button>
</div>
<Converter
allowedUploadingFormats={uploadingFormat}
optionsToConvertTo={optionsToConvertTo} />
)
}

Expand Down
9 changes: 9 additions & 0 deletions src/model/format.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/** Defines a format */
export type Format = {
/** Name o the format */
name: string
/** Extension with a dot */
extension: string
/** Mime type of this format, that allow it to be downloaded */
mimeType: string
}

0 comments on commit 58fefb0

Please sign in to comment.