groups: prevent accidental group deletion

Fixes https://github.com/urbit/landscape/issues/68
This commit is contained in:
Tyler Brown Cifu Shuster 2020-10-29 20:18:24 -07:00
parent 61442c4c2e
commit 7017b3d9cb
3 changed files with 16 additions and 6 deletions

View File

@ -327,11 +327,18 @@ export function numToUd(num) {
)(num.toString()) )(num.toString())
} }
export function usePreventWindowUnload(shouldPreventDefault) { export function usePreventWindowUnload(shouldPreventDefault, message = "You have unsaved changes. Are you sure you want to exit?") {
React.useEffect(() => { React.useEffect(() => {
if (!shouldPreventDefault) return; if (!shouldPreventDefault) return;
const handleBeforeUnload = event => event.preventDefault(); const handleBeforeUnload = event => {
event.preventDefault();
return message;
}
window.addEventListener("beforeunload", handleBeforeUnload); window.addEventListener("beforeunload", handleBeforeUnload);
return () => window.removeEventListener("beforeunload", handleBeforeUnload); window.onbeforeunload = handleBeforeUnload;
return () => {
window.removeEventListener("beforeunload", handleBeforeUnload);
window.onbeforeunload = undefined;
}
}, [shouldPreventDefault]); }, [shouldPreventDefault]);
} }

View File

@ -30,7 +30,7 @@ const PromptIfDirty = () => {
return ( return (
<Prompt <Prompt
when={formik.dirty} when={formik.dirty}
message="Are you sure you want to leave? You have with unsaved changes." message="Are you sure you want to leave? You have unsaved changes."
/> />
); );
}; };

View File

@ -83,8 +83,11 @@ export function GroupSettings(props: GroupSettingsProps) {
}; };
const onDelete = async () => { const onDelete = async () => {
await props.api.contacts.delete(association["group-path"]); const name = association['group-path'].split('/').pop();
history.push("/"); if (prompt(`To confirm deleting this group, type ${name}`) === name) {
await props.api.contacts.delete(association["group-path"]);
history.push("/");
}
}; };
const disabled = const disabled =