-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: added confirm dialog to delete or save draft while closing the … (
#115) * feat: added confirm dialog to delete or save draft while closing the composer
- Loading branch information
1 parent
a55d8c2
commit af4f7e1
Showing
4 changed files
with
199 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/* | ||
* SPDX-FileCopyrightText: 2021 Zextras <https://www.zextras.com> | ||
* | ||
* SPDX-License-Identifier: AGPL-3.0-only | ||
*/ | ||
|
||
import React, { useEffect, useState, useMemo } from 'react'; | ||
import { Prompt, useHistory } from 'react-router-dom'; | ||
import { Modal, Padding, Text } from '@zextras/carbonio-design-system'; | ||
import { useTranslation } from 'react-i18next'; | ||
import ModalFooter from '../../../../sidebar/commons/modal-footer'; | ||
|
||
export const RouteLeavingGuard = ({ when, onDeleteDraft }) => { | ||
const history = useHistory(); | ||
const lastLocationInitial = useMemo(() => history.location.pathname, [history]); | ||
const [modalVisible, setModalVisible] = useState(false); | ||
const [lastLocation, setLastLocation] = useState(lastLocationInitial); | ||
const [confirmedNavigation, setConfirmedNavigation] = useState(false); | ||
const [t] = useTranslation(); | ||
|
||
const onDelete = () => { | ||
setModalVisible(false); | ||
onDeleteDraft(); | ||
setConfirmedNavigation(true); | ||
}; | ||
|
||
const onClose = () => { | ||
setModalVisible(false); | ||
}; | ||
|
||
const onDraft = () => { | ||
setModalVisible(false); | ||
setConfirmedNavigation(true); | ||
}; | ||
|
||
const handleBlockedNavigation = (nextLocation) => { | ||
if ( | ||
!confirmedNavigation && | ||
nextLocation.pathname !== (lastLocation?.pathname || lastLocationInitial) | ||
) { | ||
setModalVisible(true); | ||
setLastLocation(nextLocation); | ||
return false; | ||
} | ||
return true; | ||
}; | ||
useEffect(() => { | ||
if (confirmedNavigation && lastLocation) { | ||
// Navigate to the previous blocked location with your navigate function | ||
history.push(lastLocation.pathname); | ||
} | ||
}, [confirmedNavigation, history, lastLocation]); | ||
|
||
return ( | ||
<> | ||
<Prompt when={when} message={handleBlockedNavigation} /> | ||
{/* Your own alert/dialog/modal component */} | ||
<Modal | ||
open={modalVisible} | ||
title={t('label.before_you_leave', 'Before you leave')} | ||
showCloseIcon | ||
onClose={onClose} | ||
customFooter={ | ||
<ModalFooter | ||
typeCancel={'outlined'} | ||
colorCancel={'primary'} | ||
onConfirm={onDraft} | ||
label={t('label.keep_draft', 'Keep Draft')} | ||
secondaryBtnType={'outlined'} | ||
secondaryAction={onDelete} | ||
secondaryLabel={t('label.delete_draft', 'Delete Draft')} | ||
secondaryColor="primary" | ||
showDivider={false} | ||
paddingTop="0" | ||
t={t} | ||
/> | ||
} | ||
> | ||
<Padding vertical="medium"> | ||
<Text> | ||
{t('modal.delete_draft.message1', 'Do you want to keep this draft or delete it?')} | ||
</Text> | ||
</Padding> | ||
</Modal> | ||
</> | ||
); | ||
}; | ||
export default RouteLeavingGuard; |
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