2023-02-22 07:10:02 -05:00
|
|
|
import { useCallback, useState } from 'react'
|
|
|
|
import { useTranslation } from 'react-i18next'
|
|
|
|
import { deleteJSON } from '../../../../infrastructure/fetch-json'
|
|
|
|
import { useSubscriptionDashboardContext } from '../../context/subscription-dashboard-context'
|
2023-03-16 08:32:48 -04:00
|
|
|
import { useLocation } from '../../../../shared/hooks/use-location'
|
2023-09-27 05:45:49 -04:00
|
|
|
import { debugConsole } from '@/utils/debugging'
|
2024-09-30 05:49:18 -04:00
|
|
|
import OLModal, {
|
|
|
|
OLModalBody,
|
|
|
|
OLModalFooter,
|
|
|
|
OLModalHeader,
|
|
|
|
OLModalTitle,
|
|
|
|
} from '@/features/ui/components/ol/ol-modal'
|
|
|
|
import OLButton from '@/features/ui/components/ol/ol-button'
|
2023-02-22 07:10:02 -05:00
|
|
|
|
|
|
|
export const LEAVE_GROUP_MODAL_ID = 'leave-group'
|
|
|
|
|
|
|
|
export default function LeaveGroupModal() {
|
|
|
|
const { t } = useTranslation()
|
|
|
|
const { handleCloseModal, modalIdShown, leavingGroupId } =
|
|
|
|
useSubscriptionDashboardContext()
|
|
|
|
const [inflight, setInflight] = useState(false)
|
2023-03-16 08:32:48 -04:00
|
|
|
const location = useLocation()
|
2023-02-22 07:10:02 -05:00
|
|
|
|
|
|
|
const handleConfirmLeaveGroup = useCallback(async () => {
|
|
|
|
if (!leavingGroupId) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
setInflight(true)
|
|
|
|
try {
|
|
|
|
const params = new URLSearchParams()
|
|
|
|
params.set('subscriptionId', leavingGroupId)
|
|
|
|
await deleteJSON(`/subscription/group/user?${params}`)
|
2023-03-16 08:32:48 -04:00
|
|
|
location.reload()
|
2023-02-22 07:10:02 -05:00
|
|
|
} catch (error) {
|
2023-09-27 05:45:49 -04:00
|
|
|
debugConsole.error('something went wrong', error)
|
2023-02-22 07:10:02 -05:00
|
|
|
setInflight(false)
|
|
|
|
}
|
2023-03-16 08:32:48 -04:00
|
|
|
}, [location, leavingGroupId])
|
2023-02-22 07:10:02 -05:00
|
|
|
|
|
|
|
if (modalIdShown !== LEAVE_GROUP_MODAL_ID || !leavingGroupId) {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
|
|
|
|
return (
|
2024-09-30 05:49:18 -04:00
|
|
|
<OLModal
|
2023-02-22 07:10:02 -05:00
|
|
|
id={LEAVE_GROUP_MODAL_ID}
|
|
|
|
show
|
|
|
|
animation
|
|
|
|
onHide={handleCloseModal}
|
|
|
|
backdrop="static"
|
|
|
|
>
|
2024-09-30 05:49:18 -04:00
|
|
|
<OLModalHeader>
|
|
|
|
<OLModalTitle>{t('leave_group')}</OLModalTitle>
|
|
|
|
</OLModalHeader>
|
2023-02-22 07:10:02 -05:00
|
|
|
|
2024-09-30 05:49:18 -04:00
|
|
|
<OLModalBody>
|
2023-02-22 07:10:02 -05:00
|
|
|
<p>{t('sure_you_want_to_leave_group')}</p>
|
2024-09-30 05:49:18 -04:00
|
|
|
</OLModalBody>
|
2023-02-22 07:10:02 -05:00
|
|
|
|
2024-09-30 05:49:18 -04:00
|
|
|
<OLModalFooter>
|
|
|
|
<OLButton
|
|
|
|
variant="secondary"
|
2023-02-22 07:10:02 -05:00
|
|
|
onClick={handleCloseModal}
|
|
|
|
disabled={inflight}
|
|
|
|
>
|
|
|
|
{t('cancel')}
|
2024-09-30 05:49:18 -04:00
|
|
|
</OLButton>
|
|
|
|
<OLButton
|
|
|
|
variant="danger"
|
2023-02-22 07:10:02 -05:00
|
|
|
onClick={handleConfirmLeaveGroup}
|
|
|
|
disabled={inflight}
|
2024-09-30 05:49:18 -04:00
|
|
|
isLoading={inflight}
|
|
|
|
bs3Props={{
|
|
|
|
loading: inflight
|
|
|
|
? t('processing_uppercase') + '…'
|
|
|
|
: t('leave_now'),
|
|
|
|
}}
|
2023-02-22 07:10:02 -05:00
|
|
|
>
|
2024-09-30 05:49:18 -04:00
|
|
|
{t('processing_uppercase')}
|
|
|
|
</OLButton>
|
|
|
|
</OLModalFooter>
|
|
|
|
</OLModal>
|
2023-02-22 07:10:02 -05:00
|
|
|
)
|
|
|
|
}
|