Skip to content

Commit 3645717

Browse files
committed
Adds hours spent field to payment form
1 parent 068aa6f commit 3645717

File tree

5 files changed

+46
-6
lines changed

5 files changed

+46
-6
lines changed

src/components/common/PaymentForm.tsx

Lines changed: 39 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,25 @@ interface Props {
1717

1818
export const PaymentForm: FunctionComponent<Props> = ({ campaign, inlineForm = false, isPayment = false }) => {
1919
const maxAmount = campaign ? campaign.required_amount : Number.MAX_SAFE_INTEGER;
20+
const hours_spent = 2;
2021
const amount = 100 > maxAmount ? maxAmount : 100;
21-
const initialState = { amount, email: '', phone: '', name: '' };
22+
const initialState = { amount, hours_spent, email: '', phone: '', name: '' };
2223
const [form, setFormValue] = useState(initialState);
2324
const [isSubmitting, setIsSubmitting] = useState(false);
2425
const [isModalOpen, toggleModal] = useState(false);
2526
const finalAmount = getFinalAmount(form.amount);
2627
function onChange(e: ChangeEvent<HTMLInputElement>) {
2728
e.persist();
2829
const value = e.target.value;
30+
if (e.target.name === 'hours_spent') {
31+
const hours_spent = Number(value);
32+
const cost = hours_spent > 3 ? hours_spent * 30 : hours_spent * 50;
33+
return setFormValue(form => ({
34+
...form,
35+
[e.target.name]: value,
36+
'amount': cost < Number(maxAmount) ? cost : maxAmount,
37+
}));
38+
}
2939
if (e.target.name === 'amount') {
3040
return setFormValue(form => ({
3141
...form,
@@ -35,8 +45,8 @@ export const PaymentForm: FunctionComponent<Props> = ({ campaign, inlineForm = f
3545
setFormValue(form => ({ ...form, [e.target.name]: value }));
3646
}
3747

38-
function validateForm(form: { name: string; email: string; phone: string; amount: number }) {
39-
if (!form.amount || !form.email || !form.phone || !form.name) {
48+
function validateForm(form: { name: string; email: string; phone: string; amount: number, hours_spent: number }) {
49+
if (!form.amount || !form.email || !form.phone || !form.name || !form.hours_spent) {
4050
return alert(`Please enter all required fields`);
4151
}
4252

@@ -55,6 +65,10 @@ export const PaymentForm: FunctionComponent<Props> = ({ campaign, inlineForm = f
5565
return `Enter a valid phone number`;
5666
}
5767

68+
if (Number(form.hours_spent) < 1) {
69+
return alert(`Hours spent should be greater than or equal to 1`);
70+
}
71+
5872
if (Number(form.amount) < 1) {
5973
return alert(`Amount should be greater than or equal to 1`);
6074
}
@@ -75,6 +89,7 @@ export const PaymentForm: FunctionComponent<Props> = ({ campaign, inlineForm = f
7589
setIsSubmitting(true);
7690
await openRzp({
7791
...form,
92+
hours_spent: Number(form.hours_spent),
7893
amount: Number(form.amount),
7994
campaign: campaign ? campaign.slug : undefined,
8095
isPayment
@@ -90,8 +105,27 @@ export const PaymentForm: FunctionComponent<Props> = ({ campaign, inlineForm = f
90105

91106
return (
92107
<div className={clsx(!inlineForm && 'px-4', inlineForm && 'md:px-0')}>
93-
<p className="text-lg mb-4 text-gray-700 leading-relaxed">We truly appreciate your generosity</p>
108+
<p className="text-lg mb-4 text-gray-700 leading-relaxed">{ isPayment ? 'Please make payment for hackerspace here' : 'We truly appreciate your generosity' }</p>
94109
<form name="payment" onSubmit={onSubmit}>
110+
<div className="mb-4 mt-4">
111+
<label htmlFor="amount" className="text-sm text-gray-800 mb-1 block">
112+
Hours spent <sup className="text-red-500">*</sup>
113+
</label>
114+
<div>
115+
<input
116+
className="bg-white focus:outline-0 border border-gray-300 rounded py-2 px-2 block w-full appearance-none leading-normal"
117+
type="number"
118+
name="hours_spent"
119+
id="hours_spent"
120+
value={form.hours_spent}
121+
onChange={onChange}
122+
required
123+
autoFocus={!inlineForm}
124+
min="1"
125+
disabled={isSubmitting}
126+
/>
127+
</div>
128+
</div>
95129
<div className="mb-4 mt-4">
96130
<label htmlFor="amount" className="text-sm text-gray-800 mb-1 block">
97131
Amount <sup className="text-red-500">*</sup>
@@ -107,7 +141,7 @@ export const PaymentForm: FunctionComponent<Props> = ({ campaign, inlineForm = f
107141
required
108142
autoFocus={!inlineForm}
109143
min="1"
110-
disabled={isSubmitting}
144+
disabled={isSubmitting || isPayment}
111145
/>
112146
</div>
113147
</div>

src/pages/api/rzp-status.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,9 @@ async function updateStateInAirtable({ razorpay_order_id, razorpay_payment_id, s
5858
.firstPage();
5959

6060
if (!result) {
61+
if (isPayment) {
62+
return updatePaymentBase({ razorpay_order_id, ...data });
63+
}
6164
return updateDonationBase({ razorpay_order_id, ...data });
6265
}
6366

src/pages/api/rzp.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const razorpay = new Razorpay(rzpCredentials);
1818

1919
export default async function handler(req: NextApiRequest, res: NextApiResponse) {
2020
try {
21-
const { email, name, amount, phone, paymentMethod = 'Razorpay', campaign, isPayment } = req.body;
21+
const { email, name, amount, hours_spent, phone, paymentMethod = 'Razorpay', campaign, isPayment } = req.body;
2222
const id = cuid();
2323
const data: { id: string; status: PaymentStatus } = await razorpay.orders.create({
2424
amount: getFinalAmount(Number(amount)) * 100, // in paise
@@ -56,6 +56,7 @@ export default async function handler(req: NextApiRequest, res: NextApiResponse)
5656
name,
5757
email,
5858
phone,
59+
hours_spent: Number(hours_spent),
5960
paid_amount: Number(amount),
6061
payment_method: paymentMethod,
6162
status: PaymentStatus.created,

src/services/airtable.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ export interface Donation extends RZPPayment {
3838

3939
export interface Payment extends RZPPayment {
4040
paid_amount: number;
41+
hours_spent: number;
4142
}
4243

4344
export interface Funding extends Donation {

src/services/rzp.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import { getFinalAmount } from '../utils';
55
interface RzpData {
66
name: string;
77
email: string;
8+
hours_spent: number;
89
amount: number;
910
phone: string;
1011
campaign?: string;

0 commit comments

Comments
 (0)