Skip to content

Instantly share code, notes, and snippets.

@kofno
Created December 14, 2018 16:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kofno/b964f577cff3ff5e10ab9f17f3bfd342 to your computer and use it in GitHub Desktop.
Save kofno/b964f577cff3ff5e10ab9f17f3bfd342 to your computer and use it in GitHub Desktop.
Snackbar Implementation
import { just, Maybe, nothing } from 'maybeasy';
import { action, computed, observable } from 'mobx';
import { Alert } from './types';
class AlertsStore {
@observable
alerts: Alert[] = [];
@computed
get current(): Maybe<Alert> {
return this.alerts.length === 0 ? nothing() : just(this.alerts[0]);
}
@action
hide = () => {
if (this.alerts.length > 0) {
this.alerts[0].display = false;
}
};
@action
process = () => {
this.alerts = this.alerts.slice(1);
};
@action
push = (alert: Alert) => {
this.hide();
this.alerts.push({ ...alert, display: true });
};
}
const alertsStore = new AlertsStore();
export default alertsStore;
interface BaseAlert {
message: string;
display: boolean;
}
export interface Success extends BaseAlert {
kind: 'success';
}
export interface Info extends BaseAlert {
kind: 'info';
}
export interface Warn extends BaseAlert {
kind: 'warn';
}
export interface Error extends BaseAlert {
kind: 'error';
}
export type Alert = Success | Info | Warn | Error;
export const alert = (message: string, kind: Alert['kind']): Alert =>
({
message,
kind,
display: true,
} as Alert);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment