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 all 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
3 changes: 2 additions & 1 deletion .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ module.exports = {
"no-unused-vars": "off",
"@typescript-eslint/no-unused-vars": "error",
"prettier/prettier": "error",
"jest/no-export": "off"
"jest/no-export": "off",
"camelcase": "off",
},
};
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 @@ const theme = createMuiTheme({

service(RoxContainer);

declare let PagarMeCheckout: any;
type CheckoutSuccessData = {
amount: number;
card_hash: string;
customer: {
document_number: string;
name: string;
email: string;
address: {
city: string;
complementary: string;
neighborhood: string;
state: string;
street: string;
street_number: string;
zipcode: string;
};
phone: {
ddd: string;
number: string;
};
};
installments: number | null;
payment_method: string; // 'credit_card'
};

declare let PagarMeCheckout: {
Checkout: {
new (args: {
encryption_key: string;
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 @@ export const DonationForm = (props: any) => {
) {
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 @@ export const DonationForm = (props: any) => {
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 @@ export const DonationForm = (props: any) => {
},
});

// Checkout API require booleans to be passed as strings
checkout.open({
amount: amountInCents,
buttonText: "Contribuir",
Expand All @@ -160,7 +188,7 @@ export const DonationForm = (props: any) => {
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 @@ export const DonationForm = (props: any) => {
raised
unelevated
disabled={loading}
onClick={onCheckout}
onClick={initCheckout}
id={styles.defaultButton}
/>
{loading && <LinearProgress color="primary" />}
Expand Down
Loading
Loading