-
Notifications
You must be signed in to change notification settings - Fork 0
Copy to clipboard notification #76
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
base: main
Are you sure you want to change the base?
Changes from all commits
478d869
d184dd5
838dbd2
b199250
440f8f2
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,5 @@ | ||
| import React, { useRef, useState } from 'react'; | ||
| import { NotificationManager } from 'react-notifications'; | ||
| import { FontAwesomeIcon } from '@fortawesome/react-fontawesome'; | ||
| import { faShareAlt, faCopy, faPencilAlt, faClock, faTrash } from '@fortawesome/free-solid-svg-icons'; | ||
| import { ReactComponent as Logo } from '../../static/logo.svg'; | ||
|
|
@@ -18,6 +19,7 @@ const ListHeader = ({ list }) => { | |
| }); | ||
| } else if (navigator.clipboard) { | ||
| navigator.clipboard.writeText(url); | ||
| NotificationManager.success('Get sharing your list with others!', 'Copied to Clipboard!', 3000); | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "success" can also be changed to error etc for different notification types
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should add a test for this, we'll need to mock out jest.mock('react-notifications');We can then simulate clicking on the button and check that the text is copied to clipboard and that the notification is shown describe('without the web share API available', () => {
const writeText = jest.fn(); // create a mock function for writeText
beforeEach(() => {
// set the navigator items up for our test
global.navigator.share = null;
global.navigator.clipboard = { writeText };
// render and click on the share icon
const { getByTitle } = render(<ListHeader />);
const shareIcon = getByTitle('Share this list');
fireEvent.click(shareIcon);
});
it('copies the item to the clipboard', () => {
expect(writeText).toHaveBeenCalledWith('https://thelist.app');
});
it('triggers a notification', () => {
expect(NotificationManager.success).toHaveBeenCalledWith('Get sharing your list with others!', 'Copied to Clipboard!', 3000);
});
});
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Great, thanks will get a test added :)
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Not sure where I'm supposed to place "jest.mock('react-notifications');" and in what context. I understand when you make a mock function and assign it to a constant, but how does the test know to utilise this line of code?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There's some great documentation about it here, but the short answer is that it replaces every method in
|
||
| } | ||
| }; | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -31,3 +31,8 @@ ul, li { | |
| list-style: none; | ||
| padding-inline-start: 0px; | ||
| } | ||
|
|
||
| .notification-success { | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For some reason placing this css code in the component file didn't have any affect on the built in styling
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this is great, but we should have a go at creating our own version of these, it'll be quite fun 😄 |
||
| background-color: var(--check_yellow); | ||
| border-radius: 5px; | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,64 @@ | ||
| declare module 'react-notifications' { | ||
| import { ReactNode } from 'react'; | ||
| import { EventEmitter } from 'events'; | ||
|
|
||
| enum NotificationType { | ||
| INFO = 'info', | ||
| SUCCESS = 'success', | ||
| WARNING = 'warning', | ||
| ERROR = 'error' | ||
| } | ||
|
|
||
| enum EventType { | ||
| CHANGE = 'change', | ||
| INFO = 'info', | ||
| SUCCESS = 'success', | ||
| WARNING = 'warning', | ||
| ERROR = 'error' | ||
| } | ||
|
|
||
| interface NotificationProps { | ||
| type: NotificationType, | ||
| title?: ReactNode, | ||
| message: ReactNode, | ||
| timeOut?: number, | ||
| onClick: () => any, | ||
| onRequestHide: () => any | ||
| } | ||
|
|
||
| interface NotificationsProps { | ||
| notifications: Notification[]; | ||
| onRequestHide?: (notification: Notification) => any; | ||
| enterTimeout?: number; | ||
| leaveTimeout?: number; | ||
| } | ||
|
|
||
| interface NotificationContainerProps { | ||
| enterTimeout?: number; | ||
| leaveTimeout?: number; | ||
| } | ||
|
|
||
| interface INotificationManagerCreate { | ||
| type: EventType, | ||
| title?: NotificationProps['title'] | ||
| message?: NotificationProps['message'] | ||
| timeout?: number, | ||
| onClick?: () => any, | ||
| priority?: boolean | ||
| } | ||
|
|
||
| class Notification extends React.Component<NotificationProps, {}> {} | ||
|
|
||
| class Notifications extends React.Component<NotificationsProps, {}> {} | ||
|
|
||
| class NotificationContainer extends React.Component<NotificationContainerProps, {}> {} | ||
|
|
||
| class NotificationManager extends EventEmitter { | ||
| static create(INotificationManagerCreate) : void | ||
| static info(message?: INotificationManagerCreate['message'], title?: INotificationManagerCreate['title'], timeOut?: INotificationManagerCreate['timeout'], onClick?: INotificationManagerCreate['onClick'], priority?: INotificationManagerCreate['priority']) : void | ||
| static success(message?: INotificationManagerCreate['message'], title?: INotificationManagerCreate['title'], timeOut?: INotificationManagerCreate['timeout'], onClick?: INotificationManagerCreate['onClick'], priority?: INotificationManagerCreate['priority']) : void | ||
| static warning(message?: INotificationManagerCreate['message'], title?: INotificationManagerCreate['title'], timeOut?: INotificationManagerCreate['timeout'], onClick?: INotificationManagerCreate['onClick'], priority?: INotificationManagerCreate['priority']) : void | ||
| static error(message?: INotificationManagerCreate['message'], title?: INotificationManagerCreate['title'], timeOut?: INotificationManagerCreate['timeout'], onClick?: INotificationManagerCreate['onClick'], priority?: INotificationManagerCreate['priority']) : void | ||
| static remove(notification: Notification) : void | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There does seem to be a warning in VS Code about a "type" for this import. It doesn't stop things from working, but I couldn't find a means of solving this warning. If it's something we should look into further I can do
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's an open issue here that suggests a workaround: minhtranite/react-notifications#29