Skip to content

Commit dc4f353

Browse files
committed
Parse script updated
1 parent d97dc8e commit dc4f353

File tree

6 files changed

+153
-132
lines changed

6 files changed

+153
-132
lines changed

UIcode/client/src/Components/FileUpload.js

-90
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
import React, { Fragment, useState } from "react";
2+
import Message from "../Message";
3+
import Progress from "../Progress";
4+
import axios from "axios";
5+
6+
const FileUpload = ({ onChange }) => {
7+
const [file, setFile] = useState("");
8+
const [filename, setFilename] = useState("Choose File");
9+
const [uploadedFile, setUploadedFile] = useState({});
10+
const [message, setMessage] = useState("");
11+
const [uploadPercentage, setUploadPercentage] = useState(0);
12+
13+
const handleFileChange = (e) => {
14+
setFile(e.target.files[0]);
15+
setFilename(e.target.files[0].name);
16+
onChange(e.target.files[0]);
17+
};
18+
19+
const onSubmit = async (e) => {
20+
e.preventDefault();
21+
const formData = new FormData();
22+
formData.append("file", file);
23+
24+
try {
25+
const res = await axios.post("/upload", formData, {
26+
headers: {
27+
"Content-Type": "multipart/form-data",
28+
},
29+
onUploadProgress: (progressEvent) => {
30+
setUploadPercentage(
31+
parseInt(
32+
Math.round((progressEvent.loaded * 100) / progressEvent.total)
33+
)
34+
);
35+
36+
// Clear percentage
37+
setTimeout(() => setUploadPercentage(0), 10000);
38+
},
39+
});
40+
41+
const { fileName, filePath } = res.data;
42+
43+
setUploadedFile({ fileName, filePath });
44+
45+
setMessage("File Uploaded");
46+
} catch (err) {
47+
if (err.response.status === 500) {
48+
setMessage("There was a problem with the server");
49+
} else {
50+
setMessage(err.response.data.msg);
51+
}
52+
}
53+
};
54+
55+
return (
56+
<Fragment>
57+
{message ? <Message msg={message} /> : null}
58+
<form onSubmit={onSubmit}>
59+
<div className="custom-file mb-4">
60+
<input
61+
type="file"
62+
className="custom-file-input"
63+
id="customFile"
64+
onChange={handleFileChange}
65+
/>
66+
<label className="custom-file-label" htmlFor="customFile">
67+
{filename}
68+
</label>
69+
</div>
70+
71+
<Progress percentage={uploadPercentage} />
72+
73+
<input
74+
type="submit"
75+
value="Upload"
76+
className="btn btn-primary btn-block mt-4"
77+
/>
78+
</form>
79+
{uploadedFile ? (
80+
<div className="row mt-5">
81+
<div className="col-md-6 m-auto">
82+
<h3 className="text-center">{uploadedFile.fileName}</h3>
83+
<img style={{ width: "100%" }} src={uploadedFile.filePath} alt="" />
84+
</div>
85+
</div>
86+
) : null}
87+
</Fragment>
88+
);
89+
};
90+
91+
export default FileUpload;

UIcode/client/src/Pages/Contribute/Step3.jsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
import React from "react";
22
import Box from "Styled/Box";
3+
import Row from "Components/Grid/Row";
4+
import FileUpload from "Components/FileUpload/index.jsx";
35

4-
const Step3 = ({ onFileUpload }) => {
6+
const Step3 = ({ onFileChange }) => {
57
return (
68
<Box>
79
<h2>Self Report</h2>
@@ -41,6 +43,9 @@ const Step3 = ({ onFileUpload }) => {
4143
</b>{" "}
4244
Can’t find the file?
4345
</p>
46+
<Row>
47+
<FileUpload onChange={onFileChange}/>
48+
</Row>
4449
</Box>
4550
);
4651
};

UIcode/client/src/Pages/Contribute/index.jsx

+10-1
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import Step2 from "./Step2";
88
import { authContext } from "Providers/AuthProvider";
99
import LoginForm from "Sections/LoginForm";
1010
import Step3 from "./Step3";
11+
import { parseJsonFile, convertLocationData } from "Services/parser";
1112

1213
const Wrapper = styled.div`
1314
padding-top: 50px;
@@ -24,6 +25,14 @@ const Contribute = () => {
2425
const handleTestDateChange = (date) => setTestDate(date);
2526
const handleNextClick = () => setStep((step) => step + 1);
2627
const handleBackClick = () => setStep((step) => step - 1);
28+
const onFileChange = (f) => {
29+
parseJsonFile(f).then(
30+
(json) => {
31+
console.log(convertLocationData(json));
32+
},
33+
() => alert("Invalid file")
34+
);
35+
};
2736

2837
const steps = [
2938
() => <Step1 submitReport={handleNextClick} viewResults={() => {}} />,
@@ -35,7 +44,7 @@ const Contribute = () => {
3544
onTestDateChange={handleTestDateChange}
3645
/>
3746
),
38-
() => <Step3 />,
47+
() => <Step3 onFileChange={onFileChange} />,
3948
];
4049

4150
const CurrentStep = steps[step];

UIcode/client/src/Services/parser.js

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
export const parseJsonFile = async (file) => {
2+
const reader = new FileReader();
3+
4+
return new Promise((resolve, reject) => {
5+
reader.onload = (evt) => {
6+
let parsed;
7+
try {
8+
parsed = JSON.parse(evt.target.result);
9+
} catch {
10+
return reject(new Error("Invalid file"));
11+
}
12+
13+
if (
14+
parsed.hasOwnProperty("timelineObjects") &&
15+
Array.isArray(parsed.timelineObjects)
16+
) {
17+
return resolve(parsed);
18+
} else {
19+
return reject(new Error("Invalid file"));
20+
}
21+
};
22+
reader.readAsText(file);
23+
});
24+
};
25+
26+
export const isPlaceVisitActivity = (location) =>
27+
location.hasOwnProperty("placeVisit");
28+
29+
export const startedAfter = (timestamp) => (location) =>
30+
location.placeVisit.duration.startTimestampMs > timestamp;
31+
32+
export const activityTransformer = (activity) => ({
33+
lat: activity.placeVisit.location.latitudeE7 / 10000000,
34+
lng: activity.placeVisit.location.longitudeE7 / 10000000,
35+
startTS: activity.placeVisit.duration.startTimestampMs,
36+
endTS: activity.placeVisit.duration.endTimestampMs,
37+
});
38+
39+
export const convertLocationData = (json) => {
40+
const startingDate = new Date().getTime() - 2 * 604800 * 1000;
41+
42+
const activityFilter = (activity) =>
43+
isPlaceVisitActivity(activity) && startedAfter(startingDate)(activity);
44+
45+
return json.timelineObjects.filter(activityFilter).map(activityTransformer);
46+
};

client/scripts/last_14d_visits.js

-40
This file was deleted.

0 commit comments

Comments
 (0)