Skip to content

Commit

Permalink
make the browser ask if you really want to leave without saving (closes
Browse files Browse the repository at this point in the history
  • Loading branch information
almeidx committed Aug 17, 2021
1 parent 73b429a commit 21b9454
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
2 changes: 1 addition & 1 deletion packages/bot/src/components/form/Selector.tsx
Expand Up @@ -34,7 +34,7 @@ const resolveItem = (item: Channel | Role | null, type: SelectorProps['type']) =
type === 'channel'
? { id: item?.id, name: item?.name }
: // eslint-disable-next-line @typescript-eslint/consistent-type-assertions
({ color: (item as Role).color, id: item?.id, name: item?.name } as Role);
({ color: (item as Role | null)?.color, id: item?.id, name: item?.name } as Role);

// eslint-disable-next-line @typescript-eslint/no-namespace, @typescript-eslint/no-unused-vars
declare namespace JSX {
Expand Down
14 changes: 13 additions & 1 deletion packages/bot/src/pages/guilds/[id].tsx
Expand Up @@ -59,7 +59,7 @@ export default function Guild({
guildId,
}: InferGetServerSidePropsType<typeof getServerSideProps>) {
const [menuOpen, setMenuOpen] = useState<boolean>(true);
const { errors, section, updateData, updateGuildId, updateSection, warnings } = useContext(GuildContext);
const { changes, errors, section, updateData, updateGuildId, updateSection, warnings } = useContext(GuildContext);
const { authenticated } = useContext(UserContext);
const router = useRouter();

Expand All @@ -69,6 +69,18 @@ export default function Guild({
const sortedChannels = useMemo(() => [...(channels ?? [])].sort((a, b) => a.name.localeCompare(b.name)), [channels]);
const sortedRoles = useMemo(() => [...(guild?.roles ?? [])].sort((a, b) => b.position - a.position), [guild]);

useEffect(() => {
if (!Object.keys(changes).length) return;

const handleUnload = (event: BeforeUnloadEvent) => {
event.returnValue = 'Changes that you made may not be saved.';
return event.returnValue;
};

window.addEventListener('beforeunload', handleUnload);
return () => window.removeEventListener('beforeunload', handleUnload);
}, [changes]);

useEffect(() => {
const pageQuery = String(router.query.p);

Expand Down

2 comments on commit 21b9454

@vercel
Copy link

@vercel vercel bot commented on 21b9454 Aug 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

pepe-main – ./packages/main

pepe-main-git-main-almeidx.vercel.app
pepe-is.life
pepe-main-almeidx.vercel.app

@vercel
Copy link

@vercel vercel bot commented on 21b9454 Aug 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

pepe-bot – ./packages/bot

pepemanager.com
pepe-bot-git-main-almeidx.vercel.app
pepe-bot-almeidx.vercel.app

Please sign in to comment.