Files
pybricks-code/src/actions/notification.ts
T
David Lechner c373b3abc5 begin rework of notifications
The notification reducer should decide when to display notifications rather than having notifications scattered throughout the code.

Also start using translations so that user-visible strings are not scattered in code.
2020-05-26 21:20:08 -05:00

72 lines
1.8 KiB
TypeScript

// SPDX-License-Identifier: MIT
// Copyright (c) 2020 The Pybricks Authors
import { Action } from 'redux';
import { createCountFunc } from '../utils/iter';
export enum NotificationActionType {
/**
* Add a notification to the list of notifications.
*/
Add = 'notification.action.add',
/**
* Remove a notification from the list of notifications.
*/
Remove = 'notification.action.Remove',
}
export type NotificationLevel = 'error' | 'warning' | 'info';
export interface NotificationAddAction extends Action<NotificationActionType.Add> {
/**
* Unique ID for this notification instance.
*/
readonly id: number;
/**
* The type of notification.
*/
readonly level: NotificationLevel;
/**
* The message to be displayed to the user.
*/
readonly message: string;
/**
* URL for help or more information.
*/
readonly helpUrl?: string;
}
export interface NotificationRemoveAction
extends Action<NotificationActionType.Remove> {
/**
* ID of an existing notification.
*/
readonly id: number;
}
export type NotificationAction = NotificationAddAction | NotificationRemoveAction;
const nextId = createCountFunc();
/**
* Action to add a notification to the list.
* @param level The severity level
* @param message The message to display to the user
* @param helpUrl An optional URL for more info
*/
export function add(
level: NotificationLevel,
message: string,
helpUrl?: string,
): NotificationAddAction {
return { type: NotificationActionType.Add, id: -nextId(), level, message, helpUrl };
}
/**
* Action to removes a notification from the list.
* @param id The id of the notification to remove
*/
export function remove(id: number): NotificationRemoveAction {
return { type: NotificationActionType.Remove, id };
}