38 lines
828 B
TypeScript
Raw Permalink Normal View History

2018-08-13 15:11:36 +03:00
import { action, IObservableArray, observable } from "mobx";
2017-09-10 11:23:54 -06:00
import { MessageProps } from "semantic-ui-react";
import { getRandomId } from "@common/utils";
2017-05-30 16:45:25 -06:00
2017-09-10 12:30:23 -06:00
export interface UiMessage extends MessageProps {
2018-09-02 02:57:55 -06:00
id: number;
2017-05-30 16:45:25 -06:00
}
export interface UiMessageProps extends MessageProps {
2018-09-02 02:57:55 -06:00
timeout?: number;
}
2017-05-30 16:45:25 -06:00
export class UiStore {
2018-09-02 02:57:55 -06:00
messages: IObservableArray<UiMessage> = observable.array();
2017-05-30 16:45:25 -06:00
2018-09-02 02:57:55 -06:00
@action
addMessage(message: UiMessageProps): UiMessage {
const { timeout, ...otherProps } = message;
const msg = observable({
...otherProps,
id: getRandomId()
});
this.messages.push(msg);
if (timeout) {
setTimeout(() => {
this.removeMessage(msg);
}, timeout);
2018-08-13 15:11:36 +03:00
}
2018-09-02 02:57:55 -06:00
return msg;
}
2018-08-13 15:11:36 +03:00
2018-09-02 02:57:55 -06:00
@action
removeMessage(message: UiMessage) {
return this.messages.remove(message);
}
2017-05-30 16:45:25 -06:00
}