import {FormProvider, SubmitHandler, useForm} from 'react-hook-form';
import {__, sprintf} from '@wordpress/i18n';
import styles from './CampaignFormModal.module.scss';
import FormModal from '../FormModal';
import CampaignsApi from '../api';
import {CampaignFormInputs, CampaignModalProps, GoalTypeOption as GoalTypeOptionType} from './types';
import {useRef, useState} from 'react';
import {Currency, Upload} from '../Inputs';
import {
AmountFromSubscriptionsIcon,
AmountIcon,
DonationsIcon,
DonorsFromSubscriptionsIcon,
DonorsIcon,
SubscriptionsIcon,
} from './GoalTypeIcons';
import {getGiveCampaignsListTableWindowData} from '../CampaignsListTable';
import {amountFormatter} from '@givewp/campaigns/utils';
import TextareaControl from '../CampaignDetailsPage/Components/TextareaControl';
import {CampaignGoalInputAttributes, isValidGoalType} from '../../constants/goalInputAttributes';
const {currency, isRecurringEnabled} = getGiveCampaignsListTableWindowData();
const currencyFormatter = amountFormatter(currency);
/**
* @since 4.0.0
*/
const getGoalTypeIcon = (type: string) => {
switch (type) {
case 'amount':
return ;
case 'donations':
return ;
case 'donors':
return ;
case 'amountFromSubscriptions':
return ;
case 'subscriptions':
return ;
case 'donorsFromSubscriptions':
return ;
}
};
/**
* Goal Type Option component
*
* @since 4.0.0
*/
const GoalTypeOption = ({type, label, description, selected, register}: GoalTypeOptionType) => {
const divRef = useRef(null);
const labelRef = useRef(null);
const handleDivClick = () => {
labelRef.current.click();
};
return (
);
};
/**
* Campaign Form Modal component
*
* @since 4.13.0 remove unused date inputs
* @since 4.0.0
*/
export default function CampaignFormModal({isOpen, handleClose, apiSettings}: CampaignModalProps) {
const API = new CampaignsApi(apiSettings);
const [step, setStep] = useState(1);
const methods = useForm({
defaultValues: {
title: '',
shortDescription: '',
image: '',
goalType: 'amount',
goal: null,
},
});
const {
register,
handleSubmit,
formState: {errors, isDirty, isSubmitting},
setValue,
watch,
trigger,
} = methods;
const image = watch('image');
const selectedGoalType = watch('goalType');
const goal = watch('goal');
const getFormModalTitle = () => {
switch (step) {
case 1:
return __('Tell us about your fundraising cause', 'give');
case 2:
return __('Set up your campaign goal', 'give');
}
return null;
};
const goalInputAttribute =
selectedGoalType && isValidGoalType(selectedGoalType)
? new CampaignGoalInputAttributes(selectedGoalType, currency)
: new CampaignGoalInputAttributes('amount', currency);
const requiredAsterisk = *;
const validateTitle = async () => {
return await trigger('title');
};
const onSubmit: SubmitHandler = async (inputs, event) => {
event.preventDefault();
if (step !== 2) {
return;
}
try {
const response = await API.fetchWithArgs('', inputs, 'POST');
handleClose(response);
} catch (error) {
console.error('Error submitting campaign campaign', error);
}
};
return (
{step === 1 && (
<>
{__('Let your donors know the story behind your campaign.', 'give')}
{__('Upload an image to represent and inspire your campaign.', 'give')}
{
setValue('image', coverImageUrl);
}}
reset={() => setValue('image', '')}
/>
>
)}
{step === 2 && (
<>
{__('Set the goal your fundraising efforts will work toward.', 'give')}
{isRecurringEnabled && (
<>
>
)}
{errors.goalType && (
{errors.goalType.message}
)}
{selectedGoalType && (
{goalInputAttribute.getDescription()}
{selectedGoalType === 'amount' || selectedGoalType === 'amountFromSubscriptions' ? (
) : (
)}
{errors.goal && (
)}
)}
>
)}
);
}