Skip to content

Commit

Permalink
automerge: save a document between page loads
Browse files Browse the repository at this point in the history
  • Loading branch information
teomrd committed Dec 2, 2023
1 parent cc412e6 commit a92be9a
Show file tree
Hide file tree
Showing 8 changed files with 629 additions and 25 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
},
"plugins": ["prettier", "jest", "@typescript-eslint"],
"rules": {
"no-console": "error",
"no-console": "off",
"prettier/prettier": "error",
"indent": ["error", 2],
"linebreak-style": ["error", "unix"],
Expand Down
57 changes: 57 additions & 0 deletions src/js/components/molecules/notify.ts
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;
Loading

0 comments on commit a92be9a

Please sign in to comment.