Skip to content

Commit 9ef2bc9

Browse files
authored
Merge pull request #238 from OpenSignLabs/feat-dropbox-fileupload
feat: add dropbox fileupload
2 parents 12ec1ba + 26d38f4 commit 9ef2bc9

File tree

6 files changed

+178
-54
lines changed

6 files changed

+178
-54
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
import React, { useCallback, useMemo } from "react";
2+
import { useDropScript } from "../../hook/useScript";
3+
4+
const DROPBOX_APP_KEY = process.env.REACT_APP_DROPBOX_API_KEY; // App key
5+
const DROPBOX_SDK_URL = "https://www.dropbox.com/static/api/2/dropins.js";
6+
const DROPBOX_SCRIPT_ID = "dropboxjs";
7+
8+
export default function DropboxChooser({ children, onSuccess, onCancel }) {
9+
useDropScript(DROPBOX_SDK_URL, {
10+
attrs: {
11+
id: DROPBOX_SCRIPT_ID,
12+
"data-app-key": DROPBOX_APP_KEY
13+
}
14+
});
15+
16+
const options = useMemo(
17+
() => ({
18+
// Required. Called when a user selects an item in the Chooser.
19+
success: (files) => {
20+
// console.log("success", files);
21+
onSuccess && onSuccess(files);
22+
},
23+
// Optional. Called when the user closes the dialog without selecting a file
24+
// and does not include any parameters.
25+
cancel: () => {
26+
console.log("cancel");
27+
onCancel && onCancel();
28+
},
29+
30+
// Optional. "preview" (default) is a preview link to the document for sharing,
31+
// "direct" is an expiring link to download the contents of the file. For more
32+
linkType: "direct", // or "preview"
33+
multiselect: false, // 是否支持多选
34+
extensions: [".pdf"],
35+
36+
// Optional. A value of false (default) limits selection to files,
37+
// while true allows the user to select both folders and files.
38+
// You cannot specify `linkType: "direct"` when using `folderselect: true`.
39+
folderselect: false // or true
40+
}),
41+
[onSuccess, onCancel]
42+
);
43+
44+
const handleChoose = useCallback(() => {
45+
if (window.Dropbox) {
46+
window.Dropbox.choose(options);
47+
}
48+
}, [options]);
49+
50+
return (
51+
<div onClick={handleChoose}>
52+
{children || (
53+
<button className="px-2 py-[2px] text-2xl rounded border-[1px] text-blue-400 border-gray-300 w-full">
54+
<i className="fa-brands fa-dropbox"></i>
55+
</button>
56+
)}
57+
</div>
58+
);
59+
}

apps/OpenSign/src/components/fields/FileUpload.js

Lines changed: 94 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@ import React, { useState, useEffect } from "react";
22
import { SaveFileSize } from "../../constant/saveFileSize";
33
import Parse from "parse";
44
import sanitizeFileName from "../../primitives/sanitizeFileName";
5-
5+
import DropboxChooser from "./DropboxChoose";
66
const FileUpload = (props) => {
77
const [parseBaseUrl] = useState(localStorage.getItem("baseUrl"));
88
const [parseAppId] = useState(localStorage.getItem("parseAppId"));
99
const [_fileupload, setFileUpload] = useState("");
1010
const [fileload, setfileload] = useState(false);
11-
12-
const [localValue, setLocalValue] = useState("");
1311
const [Message] = useState(false);
1412
const [percentage, setpercentage] = useState(0);
1513

@@ -25,7 +23,6 @@ const FileUpload = (props) => {
2523
const onChange = (e) => {
2624
try {
2725
let files = e.target.files;
28-
setLocalValue(e.target.files);
2926
if (typeof files[0] !== "undefined") {
3027
if (props.schema.filetypes && props.schema.filetypes.length > 0) {
3128
var fileName = files[0].name;
@@ -106,6 +103,56 @@ const FileUpload = (props) => {
106103
console.error("Error uploading file:", error);
107104
}
108105
};
106+
107+
const dropboxSuccess = async (files) => {
108+
// console.log("file ", files);
109+
setfileload(true);
110+
const file = files[0];
111+
const url = file.link;
112+
const size = file.bytes;
113+
const mb = Math.round(file.bytes / Math.pow(1024, 2));
114+
115+
if (mb > 10) {
116+
setTimeout(() => {
117+
alert(
118+
`The selected file size is too large. Please select a file less than 10 MB`
119+
);
120+
}, 500);
121+
return;
122+
} else {
123+
const name = sanitizeFileName(file.name);
124+
125+
const parseFile = new Parse.File(name, { uri: url });
126+
127+
try {
128+
const response = await parseFile.save({
129+
progress: (progressValue, loaded, total, { type }) => {
130+
if (type === "upload" && progressValue !== null) {
131+
const percentCompleted = Math.round((loaded * 100) / total);
132+
// console.log("percentCompleted ", percentCompleted);
133+
setpercentage(percentCompleted);
134+
}
135+
}
136+
});
137+
// console.log("response.url() ", response.url());
138+
setFileUpload(response.url());
139+
props.onChange(response.url());
140+
setfileload(false);
141+
142+
if (response.url()) {
143+
SaveFileSize(size, response.url());
144+
return response.url();
145+
}
146+
} catch (error) {
147+
setfileload(false);
148+
setpercentage(0);
149+
console.error("Error uploading file:", error);
150+
}
151+
}
152+
};
153+
const dropboxCancel = async () => {
154+
console.log("cancel clicked ");
155+
};
109156
let fileView =
110157
props.formData &&
111158
props.schema.uploadtype === "s3viajw" ? null : props.formData &&
@@ -207,55 +254,51 @@ const FileUpload = (props) => {
207254
</div>
208255

209256
<>
210-
{localValue ? (
211-
<input
212-
type="file"
213-
id="hashfile"
214-
style={{
215-
border: "1px solid #ccc",
216-
color: "gray",
217-
backgroundColor: "white",
218-
padding: "5px 10px",
219-
borderRadius: "4px",
220-
fontSize: "13px",
221-
width: "100%",
222-
fontWeight: "bold"
223-
}}
224-
accept="application/pdf,application/vnd.ms-excel"
225-
onChange={onChange}
226-
/>
227-
) : props.formData ? (
228-
<div
229-
style={{
230-
border: "1px solid #ccc",
231-
color: "gray",
232-
backgroundColor: "white",
233-
padding: "5px 10px",
234-
borderRadius: "4px",
235-
fontSize: "13px",
236-
width: "100%",
237-
fontWeight: "bold"
238-
}}
239-
>
240-
file selected : {props.formData.split("/")[3]}
257+
{props.formData ? (
258+
<div className="flex gap-2 justify-center items-center">
259+
<div className="flex justify-between items-center px-2 py-[3px] w-full font-bold rounded border-[1px] border-[#ccc] text-gray-500 bg-white text-[13px]">
260+
<div className="break-all">
261+
file selected : {props.formData.split("/")[3]}
262+
</div>
263+
<div
264+
onClick={() => {
265+
console.log("clicked");
266+
setFileUpload([]);
267+
props.onChange(undefined);
268+
}}
269+
className="cursor-pointer px-[10px] text-[20px] font-bold bg-white text-red-500"
270+
>
271+
<i className="fa-solid fa-xmark"></i>
272+
</div>
273+
</div>
274+
<DropboxChooser
275+
onSuccess={dropboxSuccess}
276+
onCancel={dropboxCancel}
277+
/>
241278
</div>
242279
) : (
243-
<input
244-
type="file"
245-
id="hashfile"
246-
style={{
247-
border: "1px solid #ccc",
248-
color: "gray",
249-
backgroundColor: "white",
250-
padding: "5px 10px",
251-
borderRadius: "4px",
252-
fontSize: "13px",
253-
width: "100%",
254-
fontWeight: "bold"
255-
}}
256-
accept="application/pdf,application/vnd.ms-excel"
257-
onChange={onChange}
258-
/>
280+
<div className="flex gap-2 justify-center items-center">
281+
<input
282+
type="file"
283+
id="hashfile"
284+
style={{
285+
border: "1px solid #ccc",
286+
color: "gray",
287+
backgroundColor: "white",
288+
padding: "5px 10px",
289+
borderRadius: "4px",
290+
fontSize: "13px",
291+
width: "100%",
292+
fontWeight: "bold"
293+
}}
294+
accept="application/pdf,application/vnd.ms-excel"
295+
onChange={onChange}
296+
/>
297+
<DropboxChooser
298+
onSuccess={dropboxSuccess}
299+
onCancel={dropboxCancel}
300+
/>
301+
</div>
259302
)}
260303
</>
261304
</React.Fragment>

apps/OpenSign/src/hook/useScript.js

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,20 @@ export const useScript = (url, onload) => {
1515
};
1616
}, [url, onload]);
1717
};
18+
19+
/**`useScript` hook is generated scripte for google sign in button */
20+
export const useDropScript = (url, onload) => {
21+
useEffect(() => {
22+
const script = document.createElement("script");
23+
//add url parameter to the script src, for load and it will remove after load in return
24+
script.src = url;
25+
script.async = true;
26+
script.defer = true;
27+
script.id = "dropboxjs";
28+
script.setAttribute("data-app-key", "8k0thg9r1t7asqg");
29+
document.head.appendChild(script);
30+
return () => {
31+
document.head.removeChild(script);
32+
};
33+
}, [url, onload]);
34+
};

apps/OpenSign/src/json/FormJson.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ export const formJson = (id) => {
1414
type: "string",
1515
title: "Select Document",
1616
filetypes: [],
17-
maxfilesizeKB: "5000",
17+
maxfilesizeKB: "10000",
1818
uploadtype: "regular",
1919
helpbody: "",
2020
helplink: ""

apps/OpenSign/src/json/plansArr.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@
5252
"subtitle": "Customization available Priority support.",
5353
"btnText": "Contact us",
5454
"url": "https://www.opensignlabs.com/contact-us",
55-
"target": "_self",
55+
"target": "_blank",
5656
"benefits": [
5757
"All features",
5858
"Custom domain",

apps/OpenSign/src/routes/PlanSubscriptions.js

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,13 @@ const PlanSubscriptions = () => {
136136
<p>{item.subtitle}</p>
137137
</div>
138138
</div>
139+
139140
<NavLink
140-
to={item.url + details}
141+
to={
142+
item.btnText === "Subscribe"
143+
? item.url + details
144+
: item.url
145+
}
141146
className="bg-[#002862] w-full text-white py-2 rounded uppercase hover:no-underline hover:text-white"
142147
target={item.target}
143148
>

0 commit comments

Comments
 (0)