Skip to content

Commit

Permalink
feat: add file upload input to upload file to ipfs
Browse files Browse the repository at this point in the history
  • Loading branch information
crackdev01 committed Jun 9, 2023
1 parent f04d8d2 commit 4cf356e
Showing 1 changed file with 24 additions and 10 deletions.
34 changes: 24 additions & 10 deletions src/components/universaldot/DAO/CreateUpdateTaskForm.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { ChangeEvent, useState } from 'react';
import { useState } from 'react';

import * as Yup from 'yup';
import merge from 'lodash/merge';
import { create } from 'ipfs-http-client';
// form
import { useForm, Controller } from 'react-hook-form';
import { yupResolver } from '@hookform/resolvers/yup';
Expand All @@ -15,6 +16,8 @@ import { FormProvider, RHFTextField } from '../../hook-form';
import { Pallets, TaskCallables } from 'src/types';
import dayjs from 'dayjs';
import customParseFormat from 'dayjs/plugin/customParseFormat';

import windowInstance from 'src/window';
dayjs.extend(customParseFormat);

// ----------------------------------------------------------------------
Expand Down Expand Up @@ -63,6 +66,11 @@ type Props = {
actionCb: (palletType: Pallets, actionType: TaskCallables, payload: any) => void;
};

const { IPFS_URL } = windowInstance.env;
const VERSION_PATH = '/api/v0';

const ipfs = create({ url: `${IPFS_URL}${VERSION_PATH}` });

export default function CreateUpdateTaskForm({
taskForm,
taskIdForEdit,
Expand All @@ -73,6 +81,7 @@ export default function CreateUpdateTaskForm({
const [isFileUploading, setIsFileUploading] = useState<boolean>(false);
const [isFileUploaded, setIsFileUploaded] = useState<boolean>(false);
const [filePath, setFilePath] = useState<string>('');
const [file, setFile] = useState<File | undefined>();

const taskFormEdit: FormValuesProps = {
...taskForm,
Expand Down Expand Up @@ -106,18 +115,22 @@ export default function CreateUpdateTaskForm({
control,
} = methods;

const handleFileChange = (event: React.ChangeEvent<HTMLInputElement>) => {
const handleFileChange = async (event: React.ChangeEvent<HTMLInputElement>) => {
if (event.target.files) {
console.log('file: ', event.target.files[0].name);
setFilePath(event.target.files[0].name);
setFile(event.target.files[0]);
setFilePath(URL.createObjectURL(event.target.files[0]));
setIsFileUploaded(true);
setIsFileUploading(true);
const { cid } = await ipfs.add(event.target.files[0]);
setIsFileUploading(false);
alert("cid: " + cid);
}
event.persist();
}

const handleRemoveFile = () => {

setFilePath("");
setIsFileUploaded(false);
setFile(undefined);
}

const onSubmit = (data: FormValuesProps) => {
Expand Down Expand Up @@ -179,15 +192,16 @@ export default function CreateUpdateTaskForm({
<RHFTextField
name="attachments"
label="Attachments"
type="file"
type={!file? "file": "text"}
value={file?.name ?? ""}
onChange={handleFileChange}
InputLabelProps={{
shrink: true
}}
InputProps={{
endAdornment: isFileUploading &&
<Box sx={{ display: 'flex', alignItems: 'center' }}>
<SvgIconStyle src={`/assets/icons/ic_close.svg`} sx={{ width: 15, height: 15, cursor: 'pointer'}} onClick={handleRemoveFile} />
endAdornment: !isFileUploading && isFileUploaded &&
<Box sx={{ display: 'flex', alignItems: 'center' }} onClick={handleRemoveFile}>
<SvgIconStyle src={`/assets/icons/ic_close.svg`} sx={{ width: 15, height: 15, cursor: 'pointer'}} />
</Box>
}}
/>
Expand Down

0 comments on commit 4cf356e

Please sign in to comment.