Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add button to cancel changes, add eventListener to handle closing tab… #826

Merged
merged 4 commits into from
Sep 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion src/components/Form/Buttons/BasicButton.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export const BasicButton: React.FC<IBasicButtonProps> = ({
className={clsx(getRootClass(), className)}
type="submit"
variant="contained"
fullWidth
disabled={disabled}
onClick={onClick}
>
Expand Down
24 changes: 24 additions & 0 deletions src/components/Form/Buttons/CancelButton.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import React from 'react';
import { Button } from '@material-ui/core';
import { useStyles } from './styles/CancelButton.style';

type CancelButtonPropsType = {
label: string;
onClick?: React.MouseEventHandler<HTMLButtonElement>;
};
export const CancelButton: React.FC<CancelButtonPropsType> = ({
label,
onClick,
}) => {
const classes = useStyles();

return (
<Button
className={classes.cancelButton}
variant="contained"
onClick={onClick}
>
{label}
</Button>
);
};
27 changes: 27 additions & 0 deletions src/components/Form/Buttons/__tests__/CancelButton.test.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { render, screen } from '@testing-library/react';
import React from 'react';
import userEvent, { TargetElement } from '@testing-library/user-event';
import { CancelButton } from '../CancelButton';

describe('Test for CancelButton component', () => {
it('Should render a button', () => {
const { container } = render(<CancelButton label="Hello world" />);
expect(container.firstChild).toHaveClass('CancelButton-cancelButton-1');
});

it('Should render a label properly', () => {
render(<CancelButton label="Hello world" />);
const label = screen.getByText('Hello world');

expect(label).toBeInTheDocument();
});

it('Should handle onClick', () => {
const handleClick = jest.fn();
const { container } = render(
<CancelButton label="Hello World" onClick={handleClick} />,
);
userEvent.click(container.firstChild as TargetElement);
expect(handleClick).toBeCalled();
});
});
1 change: 1 addition & 0 deletions src/components/Form/Buttons/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export { BasicButton } from './BasicButton';
export { CancelButton } from './CancelButton';
10 changes: 9 additions & 1 deletion src/components/Form/Buttons/styles/BasicButton.style.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ export const useStyles = makeStyles(
basicSignButton: {
padding: theme.spacing(3.2, 8.8),
borderRadius: theme.spacing(10),
flex: '0 1 300px',
skyboyer marked this conversation as resolved.
Show resolved Hide resolved
marginRight: 0,
alignSelf: 'flex-end',
backgroundColor: '#FF5C00',
'& .MuiButton-label': {
color: theme.palette.common.white,
Expand All @@ -24,6 +24,14 @@ export const useStyles = makeStyles(
'&:hover': {
backgroundColor: '#ffaa00',
},
[theme.breakpoints.down('sm')]: {
flex: '0 1 auto',
padding: theme.spacing(2, 3.1),
'& .MuiButton-label': {
fontWeight: 600,
skyboyer marked this conversation as resolved.
Show resolved Hide resolved
fontSize: '15px',
},
},
},
}),
{
Expand Down
27 changes: 27 additions & 0 deletions src/components/Form/Buttons/styles/CancelButton.style.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { makeStyles, Theme } from '@material-ui/core';

export const useStyles = makeStyles(
(theme: Theme) => ({
cancelButton: {
flex: '0 1 300px',
backgroundColor: theme.palette.info.light,
'& .MuiButton-label': {
color: theme.palette.common.white,
fontWeight: 700,
fontSize: '18px',
fontFamily: 'Raleway',
skyboyer marked this conversation as resolved.
Show resolved Hide resolved
},
'&:hover': {
backgroundColor: theme.palette.info.main,
},
[theme.breakpoints.down('sm')]: {
flex: '0 1 auto',
'& .MuiButton-label': {
fontWeight: 600,
fontSize: '15px',
},
},
},
}),
{ name: 'CancelButton' },
);
1 change: 1 addition & 0 deletions src/locales/uk/parts/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ export const common = {
enterTagName: 'Введіть назву тега',
showMore: 'Показати ще',
acceptChanges: 'Підтвердити зміни',
cancelChanges: 'Скасувати зміни',
selectedMaterials: 'Обрані матеріали',
publishedMaterials: 'Опубліковані матеріали',
unPublishedMaterials: 'Неопубліковані матеріали',
Expand Down
30 changes: 25 additions & 5 deletions src/views/Profile/PersonalInfo/PersonalInfo.tsx
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import React, { useEffect, useMemo, useState } from 'react';
import {
Avatar,
Grid,
TextField,
InputLabel,
Box,
Grid,
IconButton,
InputLabel,
TextField,
Typography,
} from '@material-ui/core';
import { BasicButton } from 'components/Form';
import { BasicButton, CancelButton } from 'components/Form';
import { PhotoCamera } from '@material-ui/icons';
import { uploadImageToImgur } from 'old/lib/utilities/Imgur/uploadImageToImgur';
import { getStringFromFile } from 'old/lib/utilities/Imgur/getStringFromFile';
Expand Down Expand Up @@ -44,7 +44,7 @@ export const PersonalInfo: React.FC<IEditAuthorProps> = ({ author }) => {
twitter: false,
linkedin: false,
});
const [toggleButton, setToggleButton] = useState(true);
const [toggleButton, setToggleButton] = useState(false);

const [newAuthorValues, setNewAuthorValues] = useState<INewAuthorValues>({
avatar: author?.avatar ?? '',
Expand Down Expand Up @@ -200,6 +200,19 @@ export const PersonalInfo: React.FC<IEditAuthorProps> = ({ author }) => {
[errorMessages],
);

useEffect(() => {
const handleBeforeUnload = (e: BeforeUnloadEvent) => {
e.preventDefault();
e.returnValue =
'Зміни не збережені. Ви впевнені, що хочете залишити сторінку?';
skyboyer marked this conversation as resolved.
Show resolved Hide resolved
};

window.addEventListener('beforeunload', handleBeforeUnload);
skyboyer marked this conversation as resolved.
Show resolved Hide resolved
return () => {
window.removeEventListener('beforeunload', handleBeforeUnload);
};
}, []);

return (
<form>
<Grid container spacing={6} className={classes.PersonalInfo}>
Expand Down Expand Up @@ -363,7 +376,14 @@ export const PersonalInfo: React.FC<IEditAuthorProps> = ({ author }) => {
/>
</Grid>
</Grid>

<Box className={classes.ButtonBox}>
{author && (
<CancelButton
label={i18n.t(langTokens.common.cancelChanges)}
onClick={() => window.close()}
/>
)}
<BasicButton
disabled={isSaveDisabled}
type="sign"
Expand Down
10 changes: 7 additions & 3 deletions src/views/Profile/PersonalInfo/styles/PersonalInfo.styles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,21 @@ export const useStyles = makeStyles(
},
BasicButton: {
'& ': {
marginTop: theme.spacing(5),
width: 300,
borderRadius: theme.spacing(0),
},
},
ButtonBox: {
'& ': {
width: 'fullwidth',
display: 'flex',
gap: '15px',
marginTop: theme.spacing(5),
justifyContent: 'right',
},
[theme.breakpoints.down('xs')]: {
'& ': {
justifyContent: 'center',
},
},
},
WrapperBox: {
'& ': {
Expand Down
Loading