-
Notifications
You must be signed in to change notification settings - Fork 191
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: added recurring donation levels to form builder
- Loading branch information
1 parent
584621b
commit baf925d
Showing
12 changed files
with
346 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
74 changes: 74 additions & 0 deletions
74
...Builder/resources/js/form-builder/src/blocks/fields/amount/inspector/useDonationLevels.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
import {useState, useCallback} from '@wordpress/element'; | ||
import type {OptionProps} from '@givewp/form-builder/components/OptionsPanel/types'; | ||
import {formatCurrencyAmount} from '@givewp/form-builder/components/CurrencyControl'; | ||
|
||
function generateLevelId() { | ||
return String(Math.floor(Math.random() * 1000000)); | ||
} | ||
|
||
export default function useDonationLevels( | ||
levels: number[], | ||
defaultLevel: number, | ||
setDefaultLevel: (defaultLevel: number) => void, | ||
setLevels: (levels: number[]) => void, | ||
) { | ||
const [levelOptions, setLevelOptions] = useState<OptionProps[]>( | ||
levels.map((level: number) => ({ | ||
id: generateLevelId(), | ||
label: formatCurrencyAmount(level.toString()), | ||
value: level.toString(), | ||
checked: defaultLevel === level, | ||
})) | ||
); | ||
|
||
const handleLevelAdded = useCallback(() => { | ||
const newLevelValue = levels.length ? String(Math.max(...levels) * 2) : '10'; | ||
const newLevel = { | ||
id: generateLevelId(), | ||
label: formatCurrencyAmount(newLevelValue), | ||
value: newLevelValue, | ||
checked: false, | ||
}; | ||
|
||
// If there are no levels, set the new level as the default. | ||
if (!levels.length) { | ||
newLevel.checked = true; | ||
setDefaultLevel(Number(newLevelValue)); | ||
} | ||
|
||
setLevelOptions([...levelOptions, newLevel]); | ||
setLevels([...levels, Number(newLevelValue)]); | ||
}, [levels, setLevels, setDefaultLevel, setLevelOptions]); | ||
|
||
const handleLevelRemoved = useCallback( | ||
(level: OptionProps, index: number) => { | ||
const newLevels = levels.filter((_, i) => i !== index); | ||
const newLevelOptions = levelOptions.filter((_, i) => i !== index); | ||
|
||
if (level.checked && newLevelOptions.length > 0) { | ||
newLevelOptions[0].checked = true; | ||
setDefaultLevel(Number(newLevelOptions[0].value)); | ||
} | ||
|
||
setLevelOptions(newLevelOptions); | ||
setLevels(newLevels); | ||
}, | ||
[levels, setLevels, setDefaultLevel, setLevelOptions] | ||
); | ||
|
||
const handleLevelsChange = useCallback((options: OptionProps[]) => { | ||
const checkedLevel = options.filter((option) => option.checked); | ||
const newLevels = options.filter((option) => option.value).map((option) => Number(option.value)); | ||
|
||
setLevelOptions(options); | ||
setLevels(newLevels); | ||
setDefaultLevel(Number(checkedLevel[0].value)); | ||
}, [setLevels, setDefaultLevel, setLevelOptions]); | ||
|
||
return { | ||
levelOptions, | ||
handleLevelAdded, | ||
handleLevelRemoved, | ||
handleLevelsChange, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
13 changes: 13 additions & 0 deletions
13
src/FormBuilder/resources/js/form-builder/src/stores/core/index.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import {createReduxStore} from '@wordpress/data'; | ||
import {getWindowData} from '@givewp/form-builder/common'; | ||
|
||
const DEFAULT_STATE = getWindowData(); | ||
|
||
const store = createReduxStore('givewp/core', { | ||
reducer: (state = DEFAULT_STATE) => state, | ||
selectors: { | ||
get: () => () => DEFAULT_STATE, | ||
}, | ||
}); | ||
|
||
export default store; |
Oops, something went wrong.