Skip to content

Commit

Permalink
refactor: add publishGoalType function
Browse files Browse the repository at this point in the history
  • Loading branch information
alaca committed Oct 25, 2023
1 parent 18def4b commit 0789307
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
24 changes: 10 additions & 14 deletions src/DonationForms/resources/app/utilities/useDonationFormPubSub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,15 @@ export default function useDonationFormPubSub() {
publish(PREVIEW_EVENTS.GOAL, data, iframeRef)
}

const publishGoalType = (type: string) => {
publish(PREVIEW_EVENTS.GOAL, {
type,
label: type,
typeIsCount: 'amount' !== type,
typeIsMoney: 'amount' === type
}, iframeRef)
}

const publishColors = (data: RequireAtLeastOne<FormColors>) => {
publish(PREVIEW_EVENTS.COLORS, data, iframeRef)
}
Expand Down Expand Up @@ -112,24 +121,11 @@ export default function useDonationFormPubSub() {
unsubscribe,
unsubscribeAll,
publishGoal,
publishGoalType,
publishColors,
publishSettings,
subscribeToGoal,
subscribeToColors,
subscribeToSettings,
}
}

/**
* Helper function for setting goal props in design mode
*
* @param type
*/
export const setGoalType = (type: string) => {
return {
type,
label: type,
typeIsCount: 'amount' !== type,
typeIsMoney: 'amount' === type
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import {
} from '@wordpress/components';
import debounce from 'lodash.debounce';
import {getFormBuilderWindowData} from '@givewp/form-builder/common/getWindowData';
import useDonationFormPubSub, {setGoalType} from '@givewp/forms/app/utilities/useDonationFormPubSub';
import useDonationFormPubSub from '@givewp/forms/app/utilities/useDonationFormPubSub';

const {goalTypeOptions} = getFormBuilderWindowData();

Expand All @@ -19,7 +19,7 @@ const DonationGoalSettings = () => {
settings: {enableDonationGoal, enableAutoClose, goalAchievedMessage, goalType, goalAmount},
} = useFormState();
const dispatch = useFormStateDispatch();
const {publishGoal} = useDonationFormPubSub();
const {publishGoal, publishGoalType} = useDonationFormPubSub();

const selectedGoalType = goalTypeOptions.find((option) => option.value === goalType);
const selectedGoalDescription = selectedGoalType ? selectedGoalType.description : '';
Expand Down Expand Up @@ -69,7 +69,7 @@ const DonationGoalSettings = () => {
options={goalTypeOptions}
onChange={(goalType: string) => {
dispatch(setFormSettings({goalType}))
publishGoal(setGoalType(goalType));
publishGoalType(goalType);
}}
help={selectedGoalDescription}
/>
Expand Down

0 comments on commit 0789307

Please sign in to comment.