Skip to content

Commit 5d3de41

Browse files
fix: signature not saving in managesign
1 parent 5cad0a9 commit 5d3de41

File tree

1 file changed

+90
-180
lines changed

1 file changed

+90
-180
lines changed

apps/OpenSign/src/pages/Managesign.js

Lines changed: 90 additions & 180 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@ import React, { useState, useRef, useEffect } from "react";
22
import SignatureCanvas from "react-signature-canvas";
33
import "../styles/managesign.css";
44
import "../styles/signature.css";
5-
import axios from "axios";
65
import { toDataUrl } from "../constant/Utils";
7-
6+
import Parse from "parse";
7+
import { appInfo } from "../constant/appinfo";
88
const ManageSign = () => {
9-
let appName;
9+
const appName = appInfo.appname;
1010
const [penColor, setPenColor] = useState("blue");
1111
const [initialPen, setInitialPen] = useState("blue");
1212
const [image, setImage] = useState();
@@ -25,59 +25,41 @@ const ManageSign = () => {
2525
const imageRef = useRef(null);
2626
const initailsRef = useRef(null);
2727
useEffect(() => {
28-
getLocalStorageValue();
28+
fetchUserSign();
2929
// eslint-disable-next-line
3030
}, []);
31-
//function for get localstorage value first and than call another function.
32-
const getLocalStorageValue = () => {
33-
let User = JSON.parse(
34-
localStorage.getItem(
35-
"Parse/" + localStorage.getItem("parseAppId") + "/currentUser"
36-
)
37-
);
38-
appName =
39-
localStorage.getItem("_appName") && localStorage.getItem("_appName");
31+
// cWT92Z2ycy
32+
const fetchUserSign = async () => {
33+
const User = Parse.User.current();
4034
if (User) {
41-
fetchUserSign(User);
42-
}
43-
};
44-
45-
const fetchUserSign = async (User) => {
46-
const userId = {
47-
__type: "Pointer",
48-
className: "_User",
49-
objectId: User.objectId
50-
};
51-
const strObj = JSON.stringify(userId);
52-
const url =
53-
localStorage.getItem("baseUrl") +
54-
`classes/${appName}_Signature?where={"UserId":${strObj}}&limit=1`;
55-
56-
const headers = {
57-
"Content-Type": "application/json",
58-
"X-Parse-Application-Id": localStorage.getItem("parseAppId")
59-
// "X-Parse-Session-Token": localStorage.getItem("accesstoken"),
60-
};
61-
const res = await axios
62-
.get(url, { headers: headers })
63-
.then((res) => {
64-
if (res.data.results.length > 0) {
65-
setId(res.data.results[0].objectId);
66-
setSignName(res.data.results[0].SignatureName);
67-
setImage(res.data.results[0].ImageURL);
68-
if (res.data.results[0] && res.data.results[0].Initials) {
69-
setInitials(res.data.results[0].Initials);
35+
const userId = {
36+
__type: "Pointer",
37+
className: "_User",
38+
objectId: User.id
39+
};
40+
try {
41+
const signCls = `${appName}_Signature`;
42+
const signQuery = new Parse.Query(signCls);
43+
signQuery.equalTo("UserId", userId);
44+
const signRes = await signQuery.first();
45+
if (signRes) {
46+
const res = signRes.toJSON();
47+
setId(res.objectId);
48+
setSignName(res?.SignatureName);
49+
setImage(res.ImageURL);
50+
if (res && res.Initials) {
51+
setInitials(res.Initials);
7052
setIsInitials(true);
7153
}
54+
} else {
55+
setSignName(User?.get("name") || "");
7256
}
7357
setIsLoader(false);
74-
return res;
75-
})
76-
.catch((error) => {
77-
console.log(error);
78-
alert(`${error.message}`);
79-
});
80-
return res;
58+
} catch (err) {
59+
console.log("Err", err);
60+
alert(`${err.message}`);
61+
}
62+
}
8163
};
8264
const handleSignatureChange = () => {
8365
if (imageRef.current) {
@@ -108,18 +90,6 @@ const ManageSign = () => {
10890
setIsInitials(false);
10991
};
11092

111-
// const handleReset = () => {
112-
// if (canvasRef.current) {
113-
// canvasRef.current.clear();
114-
// }
115-
// if (imageRef.current) {
116-
// imageRef.current.value = "";
117-
// }
118-
// setImage("");
119-
// setSignature("");
120-
// setIsValue(false);
121-
// setSignName("");
122-
// };
12393
const onImageChange = async (event) => {
12494
if (canvasRef.current) {
12595
canvasRef.current.clear();
@@ -146,28 +116,32 @@ const ManageSign = () => {
146116
const replaceSpace = signName.replace(/ /g, "_");
147117
let file;
148118
if (signature) {
149-
file = base64StringtoFile(signature, `${replaceSpace}.png`);
119+
file = base64StringtoFile(signature, `${replaceSpace}_sign.png`);
150120
} else {
151121
if (!isUrl) {
152-
file = base64StringtoFile(image, `${replaceSpace}.png`);
122+
file = base64StringtoFile(image, `${replaceSpace}__sign.png`);
153123
}
154124
}
155125
// console.log("isUrl ", isUrl);
156126
let imgUrl;
157127
if (!isUrl) {
158128
imgUrl = await uploadFile(file);
159129
} else {
160-
imgUrl = { data: { imageUrl: image } };
130+
imgUrl = image;
161131
}
162132
let initialsUrl = "";
163-
if (Initials) {
164-
const initialsImg = base64StringtoFile(Initials, `${replaceSpace}.png`);
133+
const isInitialsUrl = Initials.includes("https");
134+
if (!isInitialsUrl && Initials) {
135+
const initialsImg = base64StringtoFile(
136+
Initials,
137+
`${replaceSpace}_initials.png`
138+
);
165139
initialsUrl = await uploadFile(initialsImg);
166140
}
167-
if (imgUrl.data && imgUrl.data.imageUrl) {
141+
if (imgUrl) {
168142
await saveEntry({
169143
name: signName,
170-
url: imgUrl.data.imageUrl,
144+
url: imgUrl,
171145
initialsUrl: initialsUrl
172146
});
173147
}
@@ -186,110 +160,54 @@ const ManageSign = () => {
186160
}
187161

188162
const uploadFile = async (file) => {
189-
let parseBaseUrl = localStorage.getItem("baseUrl");
190-
parseBaseUrl = parseBaseUrl.slice(0, -4);
191-
const url = parseBaseUrl + `file_upload`;
192-
const formData = new FormData();
193-
formData.append("file", file);
194-
const config = {
195-
headers: {
196-
"content-type": "multipart/form-data",
197-
"X-Parse-Application-Id": localStorage.getItem("parseAppId")
198-
}
199-
};
200-
const response = await axios
201-
.post(url, formData, config)
202-
.then((res) => {
203-
if (res.data.status === "Error") {
204-
alert(res.data.message);
205-
}
206-
return res;
207-
})
208-
.catch((error) => {
209-
console.log(error);
210-
setIsLoader(false);
211-
alert(`${error.message}`);
212-
});
213-
return response;
163+
try {
164+
const parseFile = new Parse.File(file.name, file);
165+
const response = await parseFile.save();
166+
return response?.url();
167+
} catch (err) {
168+
console.log("sign upload err", err);
169+
setIsLoader(false);
170+
alert(`${err.message}`);
171+
}
214172
};
215173

216174
const saveEntry = async (obj) => {
175+
const signCls = `${appName}_Signature`;
176+
const User = Parse.User.current().id;
177+
const userId = { __type: "Pointer", className: "_User", objectId: User };
217178
if (id) {
218-
const User = JSON.parse(
219-
localStorage.getItem(
220-
"Parse/" + localStorage.getItem("parseAppId") + "/currentUser"
221-
)
222-
);
223-
const url =
224-
localStorage.getItem("baseUrl") +
225-
`classes/${localStorage.getItem("_appName")}_Signature/${id}`;
226-
const body = {
227-
Initials: obj.initialsUrl ? obj.initialsUrl.data.imageUrl : "",
228-
ImageURL: obj.url,
229-
SignatureName: obj.name,
230-
UserId: {
231-
__type: "Pointer",
232-
className: "_User",
233-
objectId: User.objectId
234-
}
235-
};
236-
const headers = {
237-
"Content-Type": "application/json",
238-
"X-Parse-Application-Id": localStorage.getItem("parseAppId")
239-
// "X-Parse-Session-Token": localStorage.getItem("accesstoken"),
240-
};
241-
const res = await axios
242-
.put(url, body, { headers: headers })
243-
.then((res) => {
244-
// handleReset();
245-
setIsLoader(false);
246-
setIsSuccess(true);
247-
return res;
248-
})
249-
.catch((error) => {
250-
console.log(error);
251-
setIsLoader(false);
252-
alert(`${error.message}`);
253-
});
254-
return res;
179+
try {
180+
const updateSign = new Parse.Object(signCls);
181+
updateSign.id = id;
182+
updateSign.set("Initials", obj.initialsUrl ? obj.initialsUrl : "");
183+
updateSign.set("ImageURL", obj.url);
184+
updateSign.set("SignatureName", obj.name);
185+
updateSign.set("UserId", userId);
186+
const res = await updateSign.save();
187+
setIsLoader(false);
188+
setIsSuccess(true);
189+
return res;
190+
} catch (err) {
191+
console.log(err);
192+
setIsLoader(false);
193+
alert(`${err.message}`);
194+
}
255195
} else {
256-
const User = JSON.parse(
257-
localStorage.getItem(
258-
"Parse/" + localStorage.getItem("parseAppId") + "/currentUser"
259-
)
260-
);
261-
const url =
262-
localStorage.getItem("baseUrl") +
263-
`classes/${localStorage.getItem("_appName")}_Signature`;
264-
const body = {
265-
Initials: obj.initialsUrl ? obj.initialsUrl.data.imageUrl : "",
266-
ImageURL: obj.url,
267-
SignatureName: obj.name,
268-
UserId: {
269-
__type: "Pointer",
270-
className: "_User",
271-
objectId: User.objectId
272-
}
273-
};
274-
const headers = {
275-
"Content-Type": "application/json",
276-
"X-Parse-Application-Id": localStorage.getItem("parseAppId")
277-
// "X-Parse-Session-Token": localStorage.getItem("accesstoken"),
278-
};
279-
const res = await axios
280-
.post(url, body, { headers: headers })
281-
.then((res) => {
282-
// handleReset();
283-
setIsLoader(false);
284-
setIsSuccess(true);
285-
return res;
286-
})
287-
.catch((error) => {
288-
console.log(error);
289-
setIsLoader(false);
290-
alert(`${error.message}`);
291-
});
292-
return res;
196+
try {
197+
const updateSign = new Parse.Object(signCls);
198+
updateSign.set("Initials", obj.initialsUrl ? obj.initialsUrl : "");
199+
updateSign.set("ImageURL", obj.url);
200+
updateSign.set("SignatureName", obj.name);
201+
updateSign.set("UserId", userId);
202+
const res = await updateSign.save();
203+
setIsLoader(false);
204+
setIsSuccess(true);
205+
return res;
206+
} catch (err) {
207+
console.log(err);
208+
setIsLoader(false);
209+
alert(`${err.message}`);
210+
}
293211
}
294212
};
295213
const handleSignatureBtn = () => {
@@ -337,7 +255,6 @@ const ManageSign = () => {
337255
Signature saved successfully!
338256
</div>
339257
)}
340-
341258
<div
342259
className="mainDiv"
343260
style={{
@@ -461,9 +378,8 @@ const ManageSign = () => {
461378
<div
462379
style={{
463380
position: "relative",
464-
465-
border: "2px solid #888",
466-
marginBottom: 6
381+
border: "2px solid #888"
382+
// marginBottom: 6
467383
}}
468384
className="signatureCanvas"
469385
>
@@ -569,21 +485,15 @@ const ManageSign = () => {
569485
</div>
570486
<div style={{ position: "relative" }}>
571487
<div style={{ margin: "6px 5px 18px" }}>
572-
<span
573-
// onClick={() => handleSignatureBtn()}
574-
className="signature"
575-
>
576-
Initials
577-
</span>
488+
<span className="signature">Initials</span>
578489
</div>
579490
<div>
580491
{isInitials ? (
581492
<div
582493
style={{
583494
position: "relative",
584-
585-
border: "2px solid #888",
586-
marginBottom: 6
495+
border: "2px solid #888"
496+
// marginBottom: 6
587497
}}
588498
className="intialSignature"
589499
>

0 commit comments

Comments
 (0)