import ModalDialog from '@/components/ui/ModalDialog.tsx' import TermBox from '@/components/ui/TermBox.tsx' import {cn} from '@/lib/utils.ts' import {useGlobalContext} from '@/term-merger/context.tsx' import {ActionType} from '@/term-merger/reducer.ts' import {type Group, TermId} from '@/types' import {__, sprintf} from '@wordpress/i18n' import {useEffect, useState} from 'react' import {adminAjax} from '@/lib/ajax' import useGetTermsQuery from '@/term-merger/useGetTermsQuery' export default function MergeDialog() { const { dispatch, state: { groups, taxonomy, targetGroup, terms, }, } = useGlobalContext() const query = useGetTermsQuery() // States const [headingTerm, setHeadingTerm] = useState(0), [isMerging, setIsMerging] = useState(false), [theGroup, setTheGroup] = useState(undefined) // Callback const proceedMerge = () => { if (!theGroup) { return } setIsMerging(true) adminAjax.mergeTerms( headingTerm, theGroup.terms, taxonomy, ).then(() => { // Target terms are vanished. // In the group, there is only one term left, the heading term. // // To do now: // - Fetch whole terms list again. // - Alert message. // - Remove target terms from the group. // - Set merging flag to false // - Close the dialog. query.refetch().then(() => { dispatch({ type: ActionType.SET_GROUP, payload: { id: theGroup.id, terms: [headingTerm], }, }) dispatch({ type: ActionType.SET_TARGET_GROUP, payload: 0, }) setIsMerging(false) }) alert(__('Terms have been merged.\nThank you for using SWPMU Term Merger!', 'swpmu-term-merger')) }).catch((data) => { const errorMessage = data as string if (errorMessage) { alert(errorMessage) } }) } // Effects useEffect(() => { setTheGroup(groups.get(targetGroup)) }, [targetGroup]) return !!theGroup && ( 0}>

{sprintf(__('Merging Group \'%s\'', 'swpmu-term-merger'), theGroup.title)}

Choose one heading term - all other terms will be merged into this term.

    {theGroup.terms.map((termId) => { const term = terms.get(termId) return !!term && (
  • { setHeadingTerm(parseInt(e.target.value)) }} type="radio" value={term.term_id} />
  • ) })}

{__('Press \'Proceed\' button to merge.', 'swpmu-term-merger')} {' '} {__('Please! Backup your database!', 'swpmu-term-merger')}

{/* Bottom toolbox */}
) }