Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/Add ranking #124

Merged
merged 17 commits into from
Oct 16, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
90 changes: 59 additions & 31 deletions components/DonationForm/index.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Button } from "@rmwc/button";
import axios from "axios";
import { useState, useContext } from "react";
import { useState, useContext, MouseEventHandler } from "react";
import { LinearProgress } from "@material-ui/core";
import { createMuiTheme, ThemeProvider } from "@material-ui/core/styles";

Expand Down Expand Up @@ -71,7 +71,41 @@

service(RoxContainer);

declare let PagarMeCheckout: any;
type CheckoutSuccessData = {
amount: number;
card_hash: string;

Check failure on line 76 in components/DonationForm/index.tsx

View workflow job for this annotation

GitHub Actions / run-all-checks

Identifier 'card_hash' is not in camel case
customer: {
document_number: string;

Check failure on line 78 in components/DonationForm/index.tsx

View workflow job for this annotation

GitHub Actions / run-all-checks

Identifier 'document_number' is not in camel case
name: string;
email: string;
address: {
city: string;
complementary: string;
neighborhood: string;
state: string;
street: string;
street_number: string;

Check failure on line 87 in components/DonationForm/index.tsx

View workflow job for this annotation

GitHub Actions / run-all-checks

Identifier 'street_number' is not in camel case
zipcode: string;
};
phone: {
ddd: string;
number: string;
};
};
installments: number | null;
payment_method: string; // 'credit_card'

Check failure on line 96 in components/DonationForm/index.tsx

View workflow job for this annotation

GitHub Actions / run-all-checks

Identifier 'payment_method' is not in camel case
};

declare let PagarMeCheckout: {
Checkout: {
new (args: {
encryption_key: string;

Check failure on line 102 in components/DonationForm/index.tsx

View workflow job for this annotation

GitHub Actions / run-all-checks

Identifier 'encryption_key' is not in camel case
success: (data: CheckoutSuccessData) => void;
error: () => void;
close: () => void;
}): any;
};
};
const encryptionKey = process.env.PAGARME_ENC_KEY;

export const DonationForm = (props: any) => {
Expand All @@ -85,9 +119,8 @@
) {
pushDonation(ReditusEvent.info, "Donation concluded", value, mode);
push(ReditusEvent.info, "Donation concluded");
if (userExists) {
if (userExists)
push(ReditusEvent.info, "Donation done by a recurring user");
}
donation.userExists.set(userExists);
props.goToStep(2);
}
Expand All @@ -96,44 +129,40 @@
props.goToStep(3);
}

async function onCheckout(e: any) {
e.preventDefault();
const initCheckout: MouseEventHandler<HTMLButtonElement> = (element) => {
element.preventDefault();

const error = donation.validate();
if (error) return;

// At this point, we are guaranteed to have a valid date obj.
const birthday: string = format(
donation.birthday.value as Date,
"yyyy-MM-dd"
);
if (!encryptionKey || !donation.birthday.value) return;

push(ReditusEvent.click, `Open modal to donate: ${donation.value.value}`);

const birthday: string = format(donation.birthday.value, "yyyy-MM-dd");
const amountInCents = donation.value.value * 100;
const donationMode = donation.mode.value;

// Create checkout instance
const checkout = new PagarMeCheckout.Checkout({
encryption_key: encryptionKey,
success: async function (data: any) {
success: async function (data) {
try {
donationMode == "subscriptions"
? (data[
"ssr"
] = RoxContainer.suggestedMonthlyDonationValues.getValue())
: (data[
"ssr"
] = RoxContainer.suggestedSingleDonationValues.getValue());

data["dob"] = birthday;
const ssr: string =
donationMode == "subscriptions"
? RoxContainer.suggestedMonthlyDonationValues.getValue()
: RoxContainer.suggestedSingleDonationValues.getValue();

const postData = {
...data,
ssr,
dob: birthday,
};

donation.email.set(data.customer.email);
setLoading(true);
const response = await axios.post(`/api/${donationMode}`, data);

let userExists = false;
if (response && response.data)
userExists = !!response.data.userExists;
const response = await axios.post(`/api/${donationMode}`, postData);
const userExists = response?.data
? !!response.data.userExists
: false;

setLoading(false);
return successDonation(userExists, amountInCents, donationMode);
Expand All @@ -149,7 +178,6 @@
},
});

// Checkout API require booleans to be passed as strings
checkout.open({
amount: amountInCents,
buttonText: "Contribuir",
Expand All @@ -160,7 +188,7 @@
uiColor: "#00d4ff",
headerText: "Vou contribuir com {price_info}",
});
}
};

const buttonLabel = `Doar R$ ${donation.value.value}${
donation.mode.value == "subscriptions" ? " mensalmente" : ""
Expand All @@ -180,7 +208,7 @@
raised
unelevated
disabled={loading}
onClick={onCheckout}
onClick={initCheckout}
id={styles.defaultButton}
/>
{loading && <LinearProgress color="primary" />}
Expand Down
Loading
Loading