Created
December 14, 2018 16:33
-
-
Save kofno/b964f577cff3ff5e10ab9f17f3bfd342 to your computer and use it in GitHub Desktop.
Snackbar Implementation
This file contains hidden or 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
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; |
This file contains hidden or 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
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