Skip to content

Commit

Permalink
refactor: change dialog to md components
Browse files Browse the repository at this point in the history
  • Loading branch information
Quiddlee committed Dec 29, 2023
1 parent 4a489a2 commit 540f498
Show file tree
Hide file tree
Showing 8 changed files with 35 additions and 97 deletions.
12 changes: 5 additions & 7 deletions src/components/SettingsPageComp/ClearStorageComp.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ import useLanguage from '@/shared/Context/hooks';
import FilledTonalButton from '@/shared/ui/FilledTonalButton';

import ConfirmModal from './ConfirmModal';
import ConfirmOverlay from './ConfirmOverlay';

const ClearStorageComp = () => {
const [isModalShown, setIsModalShown] = useState(false);
Expand All @@ -21,12 +20,11 @@ const ClearStorageComp = () => {
{translation.settingsPage.clear.btn}
</FilledTonalButton>
</div>
<ConfirmOverlay isShown={isModalShown} setIsShown={setIsModalShown}>
<ConfirmModal
setIsShown={setIsModalShown}
locales={{ ...translation.settingsPage.clear.modal, ...translation.settingsPage.clear.undo }}
/>
</ConfirmOverlay>
<ConfirmModal
open={isModalShown}
setIsShown={setIsModalShown}
locales={{ ...translation.settingsPage.clear.modal, ...translation.settingsPage.clear.undo }}
/>
</>
);
};
Expand Down
4 changes: 3 additions & 1 deletion src/components/SettingsPageComp/ClearUndo.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { useEffect, useState } from 'react';

import { SNACKBAR_AUTO_HIDE_DURATION } from '@shared/constants/const';

type UndoPropsType = {
closeToast: () => void;
title: string;
Expand All @@ -17,7 +19,7 @@ const ClearUndo = (props: UndoPropsType) => {
useEffect(() => {
const timeoutId = setTimeout(() => {
dataClearance();
}, 2000);
}, SNACKBAR_AUTO_HIDE_DURATION);

setDeleteDataTimeout(timeoutId);

Expand Down
33 changes: 23 additions & 10 deletions src/components/SettingsPageComp/ConfirmModal.tsx
Original file line number Diff line number Diff line change
@@ -1,15 +1,18 @@
import { Dispatch, FC, SetStateAction } from 'react';
import { Dispatch, FC, SetStateAction, useRef } from 'react';

import { MdDialog } from '@material/web/all';
import { toast } from 'react-toastify';

import FilledTonalButton from '@/shared/ui/FilledTonalButton';
import Icon from '@/shared/ui/Icon';
import TextButton from '@/shared/ui/TextButton';
import Dialog from '@shared/ui/Dialog';

import ClearUndo from './ClearUndo';

type PropsType = {
setIsShown: Dispatch<SetStateAction<boolean>>;
open: boolean;
locales: {
title: string;
subtitle: string;
Expand All @@ -20,25 +23,35 @@ type PropsType = {
};
};

const ConfirmModal: FC<PropsType> = ({ setIsShown, locales }) => {
const ConfirmModal: FC<PropsType> = ({ open, setIsShown, locales }) => {
const { title, subtitle, cancel, confirm, undoTitle, cancelBtn } = locales;
const dialogRef = useRef<MdDialog>(null);

return (
<div className="w-[312px] rounded-4xl bg-surface-container-high p-6 text-center font-[500] text-on-surface">
<Icon>delete</Icon>
<h3 className="mt-4 text-2xl">{title}</h3>
<p className="mt-4 text-start text-sm text-on-surface-variant">{subtitle}</p>
<div className="mt-6 flex justify-end gap-2">
<TextButton onClick={() => setIsShown((prev) => !prev)}>{cancel}</TextButton>
<Dialog open={open} ref={dialogRef} className="max-w-[320px]" closed={() => setIsShown(false)}>
<div slot="headline">{title}</div>
<Icon slot="icon">delete_outline</Icon>
<form id="form" slot="content" method="dialog">
{subtitle}
</form>
<div slot="actions">
<TextButton
onClick={() => {
if (dialogRef.current) dialogRef.current.close();
}}
>
{cancel}
</TextButton>
<FilledTonalButton
onClick={() => {
toast(<ClearUndo closeToast={() => {}} title={undoTitle} btn={cancelBtn} />);
setIsShown((prev) => !prev);
if (dialogRef.current) dialogRef.current.close();
}}
>
{confirm}
</FilledTonalButton>
</div>
</div>
</Dialog>
);
};

Expand Down
31 changes: 0 additions & 31 deletions src/components/SettingsPageComp/ConfirmOverlay.tsx

This file was deleted.

12 changes: 0 additions & 12 deletions src/components/loginReg/FormInput.tsx

This file was deleted.

3 changes: 2 additions & 1 deletion src/layouts/MainLayout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import Header from '@components/Header/Header';
import Nav from '@components/Nav/Nav';
import Controls from '@components/RequestEditor/ui/Controls';
import ViewProvider from '@components/ViewList/context/ViewProvider';
import { SNACKBAR_AUTO_HIDE_DURATION } from '@shared/constants/const';
import useScrollbar from '@shared/lib/hooks/useScrollbar';

const SnackBarTransition = cssTransition({
Expand Down Expand Up @@ -39,7 +40,7 @@ const MainLayout = () => {
<ToastContainer
closeOnClick={false}
closeButton={false}
autoClose={4000}
autoClose={SNACKBAR_AUTO_HIDE_DURATION}
hideProgressBar
pauseOnHover={false}
draggable={false}
Expand Down
1 change: 1 addition & 0 deletions src/shared/constants/const.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,4 @@ export const QUERY_PARAMS_INIT = {

export const INTERPOLATION_START = 1;
export const INTERPOLATION_END = 0.8;
export const SNACKBAR_AUTO_HIDE_DURATION = 4000;
36 changes: 1 addition & 35 deletions src/test/SettingsPage/SettingsPage.test.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { act, fireEvent, render, screen, waitForElementToBeRemoved } from '@testing-library/react';
import { act, fireEvent, render, screen } from '@testing-library/react';
import { describe, expect, it } from 'vitest';

import App from '@/app/App';
Expand Down Expand Up @@ -37,40 +37,6 @@ describe('Testing for settings page', () => {
expect(await screen.findByText('Dark mode')).toBeInTheDocument();
expect(await screen.findByText('Clear storage')).toBeInTheDocument();
});
it('Should opened modal and close it with proper buttons', async () => {
render(<App />);
const clearDataBtn = screen.getByText('Clear data');
expect(screen.queryByTestId('overlay')).toBeNull();
await act(async () => {
fireEvent.click(clearDataBtn);
});
expect(await screen.findByTestId('overlay')).toBeInTheDocument();
const closeBtn = await screen.findByText('Cancel');
await act(async () => {
fireEvent.click(closeBtn);
});
waitForElementToBeRemoved(() => {
expect(screen.queryByTestId('overlay')).toBeNull();
}).catch(() => {});
});
it('Should open confirm modal and close it after clicking corresponding buttons', async () => {
render(<App />);
const clearDataBtn = screen.getByText('Clear data');
await act(async () => {
fireEvent.click(clearDataBtn);
});
const clearBtn = await screen.findByText('Clear');
await act(async () => {
fireEvent.click(clearBtn);
});
const undoBtn = await screen.findByText('Undo');
await act(async () => {
fireEvent.click(undoBtn);
});
waitForElementToBeRemoved(() => {
expect(undoBtn).toBeNull();
}).catch(() => {});
});
it('Should change color theme after clicking on theme switcher', async () => {
render(<App />);
expect(document.body.getAttribute('data-user-theme')).toBeNull();
Expand Down

0 comments on commit 540f498

Please sign in to comment.