Skip to content

Commit

Permalink
Merge pull request #38 from thomasoca/add-image-upload
Browse files Browse the repository at this point in the history
Add image upload
  • Loading branch information
thomasoca committed Dec 3, 2023
2 parents 0bdd7d6 + dbed8af commit 8539744
Show file tree
Hide file tree
Showing 7 changed files with 162 additions and 6 deletions.
4 changes: 2 additions & 2 deletions frontend/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion frontend/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,15 @@ import {
KeyboardArrowRight,
GetApp,
} from "@material-ui/icons";
import ImageUploadControl, {
ImageUploadControlTester,
} from "./ImageUploadControl";

pdfjs.GlobalWorkerOptions.workerSrc = `//cdnjs.cloudflare.com/ajax/libs/pdf.js/${pdfjs.version}/pdf.worker.js`;
const renderers = [
...materialRenderers,
//register custom renderers
//{ tester: ratingControlTester, renderer: RatingControl },
{ tester: ImageUploadControlTester, renderer: ImageUploadControl },
];
const useStyles = makeStyles((theme) => ({
root: {
Expand Down
82 changes: 82 additions & 0 deletions frontend/src/ImageUpload.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
import React, { FC } from "react";
import { Grid, Button } from "@material-ui/core";
import CloudUploadIcon from "@material-ui/icons/CloudUpload";
import DeleteIcon from "@material-ui/icons/Delete";

interface ImageUploadProps {
path: string;
updateValue: (newValue: string) => void;
value?: string;
}

const getBase64 = (file: File) => {
return new Promise<string>((resolve) => {
let baseURL: string = "";
// Make new FileReader
let reader = new FileReader();

// Convert the file to base64 text
reader.readAsDataURL(file);

// on reader load somthing...
reader.onload = () => {
// Make a fileInfo Object
baseURL = reader.result as string;
resolve(baseURL);
};
});
};

const ImageUpload: FC<ImageUploadProps> = ({ path, updateValue, value }) => {
const handleClear = () => {
updateValue("");
};
return (
<div style={{ marginBottom: "20px" }}>
<Grid container spacing={2} alignItems="center">
<Grid item>
<input
accept="image/*"
style={{ display: "none" }}
id={`icon_${path}`}
onChange={(e) => {
let file = e?.target.files?.item(0)!;
getBase64(file)
.then((result) => {
updateValue(result);
})
.catch((err) => {
console.log(err);
});
}}
type="file"
multiple={false} // Change to single image upload
/>
<label htmlFor={`icon_${path}`}>
<Button
variant="contained"
color="primary"
component="span"
startIcon={<CloudUploadIcon />}
>
Upload Photo
</Button>
</label>
</Grid>
&nbsp;
<Grid item>
<Button
onClick={handleClear}
className="materialBtn"
startIcon={<DeleteIcon />}
>
Clear
</Button>
</Grid>
&nbsp;
</Grid>
</div>
);
};

export default ImageUpload;
51 changes: 51 additions & 0 deletions frontend/src/ImageUploadControl.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { withJsonFormsControlProps } from "@jsonforms/react";
import ImageUpload from "./ImageUpload"; // Assuming you have an ImageUpload component
import { rankWith, scopeEndsWith, ControlProps } from "@jsonforms/core";

interface ImageUploadControlTesterProps {
uischema: any;
schema: any;
}

interface NewHandleChangeProps {
handleChange(path: string, newValue: any): void;
path: string;
newValue: any;
}

interface ImageUploadControlProps {
data: any;
path: string;
label: string;
handleChange(path: string, value: any): void;
}

export const ImageUploadControlTester = rankWith(
3, // increase rank as needed
scopeEndsWith("picture")
);

const newHandleChange = ({
handleChange,
path,
newValue,
}: NewHandleChangeProps) => {
handleChange(path, newValue);
};

const ImageUploadControl = ({
data,
handleChange,
path,
label,
}: ControlProps) => (
<ImageUpload
updateValue={(newValue: string) =>
newHandleChange({ handleChange, path, newValue })
}
value={data}
path={path}
/>
);

export default withJsonFormsControlProps(ImageUploadControl);
4 changes: 4 additions & 0 deletions frontend/src/schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@
"twitter": {
"type": "string",
"description": "@jane"
},
"picture": {
"type": "string",
"title": "Upload Profile Picture"
}
}
},
Expand Down
10 changes: 10 additions & 0 deletions frontend/src/uischema.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,16 @@
"scope": "#/properties/personal_info/properties/github"
}
]
},
{
"type": "HorizontalLayout",
"elements": [
{
"type": "Control",
"label": "Profile Picture",
"scope": "#/properties/personal_info/properties/picture"
}
]
}
]
},
Expand Down
12 changes: 9 additions & 3 deletions frontend/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
{
"compilerOptions": {
"target": "es5",
"lib": ["dom", "dom.iterable", "esnext"],
"lib": [
"dom",
"dom.iterable",
"esnext"
],
"allowJs": true,
"skipLibCheck": true,
"esModuleInterop": true,
Expand All @@ -14,7 +18,9 @@
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true,
"jsx": "react-jsx"
"jsx": "preserve"
},
"include": ["src"]
"include": [
"src"
]
}

0 comments on commit 8539744

Please sign in to comment.