-
Notifications
You must be signed in to change notification settings - Fork 744
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* Toast Component added with locales * null check added * eslint issues fixed * eslint issues fixed * minor fix * nokogiri version updated * reviewed changes * test added * reviewed changes * gem version updated * package dependencies updated * Update client/app/components/Toast/Toast.scss Co-authored-by: Julia Nguyen <[email protected]> * tests added * issue fixed * eslint issue fixed * eslint issue fixed * eslint issue fixed * eslint issue fixed * changes reverted * Re-run yarn lint * test script fixed * test fixed Co-authored-by: Julia Nguyen <[email protected]> Co-authored-by: Julia Nguyen <[email protected]>
- Loading branch information
1 parent
39f213c
commit e171b11
Showing
29 changed files
with
306 additions
and
26 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
This file was deleted.
Oops, something went wrong.
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
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,28 @@ | ||
@import "~styles/_global.scss"; | ||
|
||
.toast { | ||
display: flex; | ||
justify-content: space-between; | ||
width: fit-content; | ||
margin: 0 auto; | ||
|
||
button { | ||
background: transparent; | ||
color: $white; | ||
opacity: 1; | ||
border: none; | ||
|
||
&:hover { | ||
border: none; | ||
opacity: 0.8; | ||
} | ||
} | ||
} | ||
|
||
.toastElementHidden { | ||
visibility: hidden; | ||
} | ||
|
||
.toastElementVisible { | ||
visibility: visible; | ||
} |
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,96 @@ | ||
// @flow | ||
import React from 'react'; | ||
import { | ||
render, screen, fireEvent, waitFor, | ||
} from '@testing-library/react'; | ||
import { Toast } from 'components/Toast'; | ||
|
||
describe('Toast', () => { | ||
describe('Toast Type: Alert', () => { | ||
it('renders correctly', () => { | ||
render( | ||
<Toast | ||
alert="Invalid username or password." | ||
appendDashboardClass="true" | ||
/>, | ||
); | ||
expect(screen).not.toBeNull(); | ||
}); | ||
|
||
it('closes correctly on button click', () => { | ||
const { getByRole, container } = render( | ||
<Toast | ||
alert="Invalid username or password." | ||
appendDashboardClass="true" | ||
/>, | ||
); | ||
|
||
const toastContent = getByRole('alert'); | ||
const toastBtn = container.querySelector('#btn-close-toast-alert'); | ||
|
||
expect(toastContent).toHaveClass('toastElementVisible'); | ||
fireEvent.click(toastBtn); | ||
expect(toastContent).toHaveClass('toastElementHidden'); | ||
}); | ||
|
||
it('closes automatically after 7 seconds', async () => { | ||
const { getByRole } = render( | ||
<Toast | ||
alert="Invalid username or password." | ||
appendDashboardClass="true" | ||
/>, | ||
); | ||
|
||
const toastContent = getByRole('alert'); | ||
expect(toastContent).toHaveClass('toastElementVisible'); | ||
await waitFor( | ||
() => { | ||
expect(toastContent).toHaveClass('toastElementHidden'); | ||
}, | ||
{ | ||
timeout: 7000, | ||
}, | ||
); | ||
}, 30000); | ||
}); | ||
|
||
describe('Toast Type: Notice', () => { | ||
it('renders correctly', () => { | ||
render(<Toast notice="Login successful." />); | ||
expect(screen).not.toBeNull(); | ||
}); | ||
|
||
it('closes correctly on button click', () => { | ||
const { getByRole, container } = render(<Toast notice="Login successful." />); | ||
|
||
const toastContent = getByRole('region'); | ||
const toastBtn = container.querySelector('#btn-close-toast-notice'); | ||
|
||
expect(toastContent).toHaveClass('toastElementVisible'); | ||
fireEvent.click(toastBtn); | ||
expect(toastContent).toHaveClass('toastElementHidden'); | ||
}); | ||
|
||
it('closes automatically after 7 seconds', async () => { | ||
const { getByRole } = render(<Toast notice="Login successful." />); | ||
|
||
const toastContent = getByRole('region'); | ||
expect(toastContent).toHaveClass('toastElementVisible'); | ||
await waitFor( | ||
() => { | ||
expect(toastContent).toHaveClass('toastElementHidden'); | ||
}, | ||
{ | ||
timeout: 7000, | ||
}, | ||
); | ||
}, 30000); | ||
}); | ||
|
||
describe('Toast Type: Notice', () => { | ||
it('renders correctly', () => { | ||
render(<Toast notice="Signed out successfully." />); | ||
expect(screen).not.toBeNull(); | ||
}); | ||
}); | ||
}); |
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,103 @@ | ||
// @flow | ||
import React, { useState } from 'react'; | ||
import type { Node } from 'react'; | ||
import { I18n } from 'libs/i18n'; | ||
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; | ||
import { faTimes } from '@fortawesome/free-solid-svg-icons'; | ||
import css from './Toast.scss'; | ||
|
||
type Props = { | ||
alert?: string, | ||
notice?: string, | ||
appendDashboardClass?: boolean, | ||
}; | ||
export type State = { | ||
showToast: boolean, | ||
}; | ||
|
||
export const Toast = ({ alert, notice, appendDashboardClass }: Props): Node => { | ||
const [showAlert, setShowAlert] = useState<boolean>( | ||
alert !== null | ||
&& alert !== '' | ||
&& !document.documentElement?.hasAttribute('data-turbolinks-preview'), | ||
); | ||
const [showNotice, setShowNotice] = useState<boolean>( | ||
notice !== null | ||
&& notice !== '' | ||
&& !document.documentElement?.hasAttribute('data-turbolinks-preview'), | ||
); | ||
const hideNotice = () => { | ||
setShowNotice(false); | ||
}; | ||
const hideAlert = () => { | ||
setShowAlert(false); | ||
}; | ||
if (showAlert || showNotice) { | ||
setTimeout(() => { | ||
hideNotice(); | ||
hideAlert(); | ||
}, 7000); | ||
} | ||
return ( | ||
<> | ||
<div | ||
id="toast-notice" | ||
aria-label={showNotice ? I18n.t('alert_auto_hide') : ''} | ||
role="region" | ||
aria-live="polite" | ||
aria-atomic="true" | ||
className={`${ | ||
showNotice ? 'notice toastElementVisible' : 'toastElementHidden' | ||
} ${css.toast} ${ | ||
showNotice && (showAlert || appendDashboardClass) | ||
? 'smallMarginBottom' | ||
: '' | ||
}`} | ||
> | ||
{showNotice && ( | ||
<> | ||
<div> | ||
{notice} | ||
</div> | ||
<button id="btn-close-toast-notice" type="button" onClick={hideNotice} aria-label={I18n.t('close')}> | ||
<span aria-hidden="true"> | ||
<FontAwesomeIcon icon={faTimes} /> | ||
</span> | ||
</button> | ||
</> | ||
)} | ||
</div> | ||
<div | ||
id="toast-alert" | ||
aria-label={showAlert ? I18n.t('alert_auto_hide') : ''} | ||
role="alert" | ||
className={`${ | ||
showAlert ? 'alert toastElementVisible' : 'toastElementHidden' | ||
} ${css.toast} ${ | ||
showAlert && appendDashboardClass ? 'smallMarginBottom' : '' | ||
}`} | ||
> | ||
{showAlert && ( | ||
<> | ||
<div> | ||
{alert} | ||
</div> | ||
<button id="btn-close-toast-alert" type="button" onClick={hideAlert} aria-label={I18n.t('close')}> | ||
<span aria-hidden="true"> | ||
<FontAwesomeIcon icon={faTimes} /> | ||
</span> | ||
</button> | ||
</> | ||
)} | ||
</div> | ||
</> | ||
); | ||
}; | ||
|
||
export default ({ alert, notice, appendDashboardClass }: Props): Node => ( | ||
<Toast | ||
alert={alert} | ||
notice={notice} | ||
appendDashboardClass={appendDashboardClass} | ||
/> | ||
); |
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,25 @@ | ||
/* eslint-disable react/jsx-props-no-spreading */ | ||
import React from 'react'; | ||
import { Toast } from 'components/Toast'; | ||
|
||
export default { | ||
title: 'Components/Toast', | ||
component: Toast, | ||
}; | ||
|
||
const Template = (args) => <Toast {...args} />; | ||
|
||
export const noticeToast = Template.bind({}); | ||
|
||
noticeToast.args = { | ||
notice: 'Login successful.', | ||
appendDashboardClass: 'true', | ||
}; | ||
noticeToast.storyName = 'Toast Type: Notice'; | ||
|
||
export const alertToast = Template.bind({}); | ||
|
||
alertToast.args = { | ||
alert: 'Login failed.', | ||
}; | ||
alertToast.storyName = 'Toast Type: Alert'; |
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
Oops, something went wrong.