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 payment field to forms #294

Merged
merged 3 commits into from
Sep 18, 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: 3 additions & 0 deletions public/locales/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -198,14 +198,17 @@
"view": "View Only"
},
"formQuestionEdit": {
"amt": "Amount",
"ansReq": "Require an answer for this question",
"checkBox": "Checkbox",
"confirmMsg": "Are you sure you wish to permanently delete this question?",
"date": "Date",
"decNum": "Decimal",
"desc": "Description",
"fieldReq": "Field type is required.",
"fund": "Fund",
"multiChoice": "Multiple Choice",
"payment": "Payment",
"phoneNum": "Phone Number",
"plcOp": "Placeholder (optional)",
"prov": "Provider",
Expand Down
27 changes: 21 additions & 6 deletions src/forms/components/FormQuestionEdit.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Checkbox, FormControl, FormControlLabel, InputLabel, MenuItem, Select,
import React, { useState } from "react";
import { ChoicesEdit } from ".";
import { useMountedState, QuestionInterface, ApiHelper, InputBox, UniqueIdHelper, ErrorMessages, Locale } from "@churchapps/apphelper";
import { PaymentEdit } from "./PaymentEdit";

interface Props {
questionId: string,
Expand Down Expand Up @@ -43,6 +44,23 @@ export function FormQuestionEdit(props: Props) {
setQuestion(q);
}

const getChoices = (fieldType: string) => {
let result = <></>;
switch (fieldType) {
case "Multiple Choice":
case "Checkbox":
result = <ChoicesEdit question={question} updatedFunction={setQuestion} />;
break;
case "Payment":
result = <PaymentEdit question={question} updatedFunction={setQuestion} />;
break;
default:
result = <TextField fullWidth label={Locale.label("forms.formQuestionEdit.plcOp")} id="placeholder" type="text" name="placeholder" value={question.placeholder || ""} onChange={handleChange} />;
break;
}
return result;
}

const validate = () => {
const result = [];
if (!question.title) result.push(Locale.label("forms.formQuestionEdit.questionReq"));
Expand Down Expand Up @@ -82,18 +100,15 @@ export function FormQuestionEdit(props: Props) {
<MenuItem value="Text Area">{Locale.label("forms.formQuestionEdit.textArea")}</MenuItem>
<MenuItem value="Multiple Choice">{Locale.label("forms.formQuestionEdit.multiChoice")}</MenuItem>
<MenuItem value="Checkbox">{Locale.label("forms.formQuestionEdit.checkBox")}</MenuItem>
<MenuItem value="Payment">{Locale.label("forms.formQuestionEdit.payment")}</MenuItem>
</Select>
</FormControl>

<TextField fullWidth label={Locale.label("common.title")} id="title" type="text" name="title" value={question.title || ""} onChange={handleChange} />
<TextField fullWidth label={Locale.label("forms.formQuestionEdit.desc")} id="description" type="text" name="description" value={question.description || ""} onChange={handleChange} />

{
(question.fieldType === "Multiple Choice" || question.fieldType === "Checkbox")
? <ChoicesEdit question={question} updatedFunction={setQuestion} />
: <TextField fullWidth label={Locale.label("forms.formQuestionEdit.plcOp")} id="placeholder" type="text" name="placeholder" value={question.placeholder || ""} onChange={handleChange} />
}
<FormControlLabel control={<Checkbox />} label={Locale.label("forms.formQuestionEdit.ansReq")} name="required" checked={!!question.required} onChange={handleCheckChange} />
{getChoices(question.fieldType)}
{question.fieldType !== "Payment" && <FormControlLabel control={<Checkbox />} label={Locale.label("forms.formQuestionEdit.ansReq")} name="required" checked={!!question.required} onChange={handleCheckChange} />}
</InputBox>
);
}
55 changes: 55 additions & 0 deletions src/forms/components/PaymentEdit.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from "react";
import { FormControl, InputAdornment, InputLabel, MenuItem, Select, SelectChangeEvent, TextField } from "@mui/material";
import { ApiHelper, ArrayHelper, FundInterface, Locale, QuestionInterface } from "@churchapps/apphelper";

interface Props { question: QuestionInterface, updatedFunction: (question: QuestionInterface) => void }

export const PaymentEdit: React.FC<Props> = (props) => {
const [funds, setFunds] = React.useState([]);
const [fundId, setFundId] = React.useState(props.question.choices?.find((c: any) => c.text === "FundId")?.value || "");
const [amount, setAmount] = React.useState(props.question.choices?.find((c: any) => c.text === "Amount")?.value || 0);

const handleChange = (e: React.ChangeEvent<HTMLInputElement | HTMLTextAreaElement> | SelectChangeEvent<string>) => {
e.preventDefault();
switch(e.target.name) {
case "fundId": setFundId(e.target.value); break;
case "amount": setAmount(Number(e.target.value)); break;
}
let q = { ...props.question };
if (e.target.name === "fundId") {
let fundIndex = q.choices.findIndex((c: any) => c.text === "FundId");
q.choices[fundIndex].value = e.target.value;
}
else if (e.target.name === "amount") {
let amountIndex = q.choices.findIndex((c: any) => c.text === "Amount");
q.choices[amountIndex].value = e.target.value.toString();
}
props.updatedFunction(q);
}

React.useEffect(() => {
ApiHelper.get("/funds", "GivingApi").then((data: any) => {
const result = ArrayHelper.getAll(data, "taxDeductible", false);
setFunds(result);
if (fundId === "" && result.length > 0) {
setFundId(result[0].id);
let q = { ...props.question };
if (!q.choices) {
q.choices = [{ value: result[0].id, text: "FundId" }];
q.choices.push({ value: "0", text: "Amount" });
}
props.updatedFunction(q)
}
});
}, []); // eslint-disable-line react-hooks/exhaustive-deps

return <>
<FormControl fullWidth>
<InputLabel id="fund">{Locale.label("forms.formQuestionEdit.fund")}</InputLabel>
<Select name="fundId" labelId="fund" label={Locale.label("forms.formQuestionEdit.fund")} value={fundId} onChange={handleChange}>
{funds.map((fund: FundInterface, index: number) => <MenuItem key={index} value={fund.id}>{fund.name}</MenuItem>)}
</Select>
</FormControl>
<TextField fullWidth name="amount" label={Locale.label("forms.formQuestionEdit.amt")} type="number" value={amount} onChange={handleChange} InputProps={{ startAdornment: <InputAdornment position="start">$</InputAdornment> }} />
</>;
}
Loading