-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
automerge: save a document between page loads
- Loading branch information
Showing
8 changed files
with
629 additions
and
25 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
import select from "../../utils/dom"; | ||
|
||
const notificationTypes = { | ||
info: "info", | ||
success: "success", | ||
error: "error", | ||
warning: "warning", | ||
} as const; | ||
|
||
type NotificationType = keyof typeof notificationTypes; | ||
|
||
const notificationDom = select("#notification"); | ||
const cleanNotificationClasses = () => | ||
notificationDom.removeClasses(Object.values(notificationTypes)); | ||
notificationDom.el.onclick = () => cleanNotificationClasses(); | ||
|
||
type NotificationFactoryProduct = { | ||
[key in NotificationType]: (message: string) => void; | ||
}; | ||
|
||
const notificationFactory = (): NotificationFactoryProduct => | ||
Object.values(notificationTypes).reduce( | ||
(acc, notificationType) => ({ | ||
...acc, | ||
[notificationType](message: string) { | ||
this.showNotification(message, notificationType); | ||
}, | ||
}), | ||
{} as NotificationFactoryProduct | ||
); | ||
|
||
type Notification = { | ||
autohideDuration: number; | ||
timer: number | undefined; | ||
removeAfter: () => void; | ||
showNotification: (message: string, type: NotificationType) => void; | ||
} & NotificationFactoryProduct; | ||
|
||
const notification = (autohideDuration = 5): Notification => ({ | ||
autohideDuration, | ||
timer: undefined, | ||
removeAfter() { | ||
this.timer = setTimeout(() => { | ||
cleanNotificationClasses(); | ||
}, this.autohideDuration * 1000); | ||
}, | ||
showNotification(message: string, type = notificationTypes.info) { | ||
cleanNotificationClasses(); | ||
notificationDom.innerHTML(message).addClass(type); | ||
clearTimeout(this.timer); | ||
this.removeAfter(); | ||
}, | ||
...notificationFactory(), | ||
}); | ||
|
||
const notify = notification(); | ||
export default notify; |
Oops, something went wrong.