Skip to content

Commit 1c9a3b9

Browse files
authored
Merge pull request #15 from NetLogo/feature-logging-download-data
Added Additional Fields to Download Form
2 parents 8163023 + d3a2fd3 commit 1c9a3b9

File tree

3 files changed

+47
-11
lines changed

3 files changed

+47
-11
lines changed

src/components/download/donation-section.tsx

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,16 +13,23 @@ export interface DonationData {
1313
}
1414

1515
interface DonationSectionProps {
16-
donationData: DonationData;
16+
donationArray: DonationData[];
1717
}
1818

19-
const DonationSection = ({ donationData }: DonationSectionProps) => {
19+
const DonationSection = ({ donationArray }: DonationSectionProps) => {
20+
const [donationData, setDonationData] = useState<DonationData | null>(null);
21+
22+
useEffect(() => {
23+
const randomIndex = Math.floor(Math.random() * donationArray.length);
24+
setDonationData(donationArray[randomIndex]);
25+
}, []);
26+
2027
return (
2128
<ContentImageLayout
22-
imageId={donationData.image.id}>
23-
<h1 className="donate-title">{donationData.title}</h1>
29+
imageId={donationData?.image.id}>
30+
<h1 className="donate-title">{donationData?.title}</h1>
2431
<ReactMarkdown className="donate-text">
25-
{donationData.text}
32+
{donationData?.text}
2633
</ReactMarkdown>
2734
<div className="donate-button-container">
2835
<Button
@@ -36,7 +43,7 @@ const DonationSection = ({ donationData }: DonationSectionProps) => {
3643
width: "203px",
3744
borderRadius: "17px",
3845
}}
39-
onClick={() => handleLinkClick(donationData.url)}
46+
onClick={() => donationData ? handleLinkClick(donationData.url) : null}
4047
/>
4148
<p>
4249
Donations are processed through Northwestern University, but 100%

src/components/download/download-form.tsx

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ export interface FormData {
1717
email: string;
1818
subscribe: boolean;
1919
comments: string;
20+
ip: string;
21+
country: string;
22+
time_stamp: string;
2023
}
2124

2225
interface DownloadFormProps {
@@ -37,6 +40,20 @@ const DetectOS = () => {
3740
return os;
3841
}
3942

43+
const getFormattedTimestamp = () => {
44+
const now = new Date();
45+
const pad = (n:number) => String(n).padStart(2, '0');
46+
47+
const year = now.getFullYear();
48+
const month = pad(now.getMonth() + 1); // Months are 0-indexed
49+
const day = pad(now.getDate());
50+
const hour = pad(now.getHours());
51+
const minute = pad(now.getMinutes());
52+
const second = pad(now.getSeconds());
53+
54+
return `${year}-${month}-${day} ${hour}:${minute}:${second}`;
55+
}
56+
4057
const DownloadForm = ({ versions, downloadedSetter }: DownloadFormProps) => {
4158
// State for all form fields with typed interface
4259
const [formData, setFormData] = useState<FormData>({
@@ -47,6 +64,9 @@ const DownloadForm = ({ versions, downloadedSetter }: DownloadFormProps) => {
4764
email: "",
4865
subscribe: false,
4966
comments: "",
67+
ip: "",
68+
country: "",
69+
time_stamp: "",
5070
});
5171

5272
const netLogoVersions = useMemo(() => {
@@ -85,11 +105,20 @@ const DownloadForm = ({ versions, downloadedSetter }: DownloadFormProps) => {
85105
});
86106
};
87107

88-
const handleFormSubmission = (e: FormEvent<HTMLFormElement>) => {
108+
const handleFormSubmission = async (e: FormEvent<HTMLFormElement>) => {
89109
e.preventDefault();
90110

91111
// Send form data to backend
92112
const api = new NetLogoAPI();
113+
114+
await fetch('https://ipapi.co/json/')
115+
.then(res => res.json())
116+
.then(data => {
117+
formData.ip = data.ip;
118+
formData.country = data.country_name;
119+
formData.time_stamp = getFormattedTimestamp();
120+
});
121+
93122
const result = api.sendDownloadForm(formData);
94123

95124
//Go to download Link
@@ -101,6 +130,9 @@ const DownloadForm = ({ versions, downloadedSetter }: DownloadFormProps) => {
101130
(link) => link.platform === formData.platform
102131
)?.download_url;
103132

133+
134+
135+
104136
if (!downloadUrl) {
105137
alert("Download link not found");
106138
return;

src/pages/donate.astro

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,10 @@ import DonationSection from "../components/download/donation-section.tsx";
55
66
const api = new NetLogoApi();
77
8-
98
const donationData = await api.getDonationData()
10-
const randomIndex = Math.floor(Math.random() * donationData.length);
11-
129
1310
---
1411

1512
<Layout show_footer_buttons={false}>
16-
<DonationSection donationData = {donationData[randomIndex]} client:load/>
13+
<DonationSection donationArray = {donationData} client:load/>
1714
</Layout>

0 commit comments

Comments
 (0)