@42.nl/react-flash-messages 0.0.2 → 2.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/lib/actions.d.ts CHANGED
@@ -3,45 +3,50 @@ import { FlashMessage, FlashMessageConfig, FlashMessageCreatorConfig } from './m
3
3
  * Adds a FlashMessage to the store and removes it after the
4
4
  * duration, if the duration is not false.
5
5
  *
6
- * @param flashMessage The flashMessage you want to add.
6
+ * @param flashMessage The flash message you want to add.
7
+ */
8
+ export declare function addFlashMessage<Data>(flashMessageConfig: FlashMessageConfig<Data>): FlashMessage<Data>;
9
+ /**
10
+ * Manually removes the flash message from the flash message store.
11
+ *
12
+ * @param flashMessage The flash message you want to remove.
7
13
  */
8
- export declare function addFlashMessage<Data>(flashMessageCreatorConfig: FlashMessageConfig<Data>): FlashMessage<Data>;
9
14
  export declare function removeFlashMessage<Data>(flashMessage: FlashMessage<Data>): void;
10
15
  /**
11
16
  * Adds a flash message of the type 'ERROR' on the flash message queue.
12
17
  *
13
- * @param {FlashMessageCreatorConfig} { text, onClick, data }
18
+ * @param {FlashMessageCreatorConfig} Config The config of the flash message
14
19
  */
15
- export declare function addError<Data>({ text, onClick, data }: FlashMessageCreatorConfig<Data>): FlashMessage<Data>;
20
+ export declare function addError<Data>(config: FlashMessageCreatorConfig<Data>): FlashMessage<Data>;
16
21
  /**
17
22
  * Adds a flash message of the type 'WARNING' on the flash message queue.
18
23
  * After 7000 milliseconds it will automatically be removed
19
24
  * from the queue.
20
25
  *
21
- * @param {FlashMessageCreatorConfig} { text, onClick, data }
26
+ * @param {FlashMessageCreatorConfig} Config The config of the flash message
22
27
  */
23
- export declare function addWarning<Data>({ text, onClick, data }: FlashMessageCreatorConfig<Data>): FlashMessage<Data>;
28
+ export declare function addWarning<Data>(config: FlashMessageCreatorConfig<Data>): FlashMessage<Data>;
24
29
  /**
25
30
  * Adds a flash message of the type 'SUCCESS' on the flash message queue.
26
31
  * After 2000 milliseconds it will automatically be removed
27
32
  * from the queue.
28
33
  *
29
- * @param {FlashMessageCreatorConfig} { text, onClick, data }
34
+ * @param {FlashMessageCreatorConfig} Config The config of the flash message
30
35
  */
31
- export declare function addSuccess<Data>({ text, onClick, data }: FlashMessageCreatorConfig<Data>): FlashMessage<Data>;
36
+ export declare function addSuccess<Data>(config: FlashMessageCreatorConfig<Data>): FlashMessage<Data>;
32
37
  /**
33
38
  * Adds a flash message of the type 'INFO' on the flash message queue.
34
39
  * After 5000 milliseconds it will automatically be removed
35
40
  * from the queue.
36
41
  *
37
- * @param {FlashMessageCreatorConfig} { text, onClick, data }
42
+ * @param {FlashMessageCreatorConfig} Config The config of the flash message
38
43
  */
39
- export declare function addInfo<Data>({ text, onClick, data }: FlashMessageCreatorConfig<Data>): FlashMessage<Data>;
44
+ export declare function addInfo<Data>(config: FlashMessageCreatorConfig<Data>): FlashMessage<Data>;
40
45
  /**
41
46
  * Adds a flash message of the type 'APOCALYPSE' on the flash message queue.
42
47
  * This message is never removed from the queue automatically.
43
48
  *
44
- * @param {FlashMessageCreatorConfig} { text, onClick, data }
49
+ * @param {FlashMessageCreatorConfig} Config The config of the flash message
45
50
  */
46
- export declare function addApocalypse<Data>({ text, onClick, data }: FlashMessageCreatorConfig<Data>): FlashMessage<Data>;
51
+ export declare function addApocalypse<Data>(config: FlashMessageCreatorConfig<Data>): FlashMessage<Data>;
47
52
  export declare function resetNextFlashMessageId(): void;
package/lib/actions.js CHANGED
@@ -1,38 +1,46 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.resetNextFlashMessageId = exports.addApocalypse = exports.addInfo = exports.addSuccess = exports.addWarning = exports.addError = exports.removeFlashMessage = exports.addFlashMessage = void 0;
3
4
  const service_1 = require("./service");
5
+ /**
6
+ * Each flash message gets an unique id so we can identify the
7
+ * flash message.
8
+ */
4
9
  let nextFlashMessageId = 1;
5
- // This function does not do anything.
6
- const noop = () => { };
7
10
  /**
8
11
  * Adds a FlashMessage to the store and removes it after the
9
12
  * duration, if the duration is not false.
10
13
  *
11
- * @param flashMessage The flashMessage you want to add.
14
+ * @param flashMessage The flash message you want to add.
12
15
  */
13
- function addFlashMessage(flashMessageCreatorConfig) {
14
- const flashMessage = addIdAndOnClick(flashMessageCreatorConfig);
16
+ function addFlashMessage(flashMessageConfig) {
17
+ const flashMessage = addIdAndOnClick(flashMessageConfig);
15
18
  service_1.flashMessageService.addFlashMessage(flashMessage);
16
19
  const duration = flashMessage.duration;
17
20
  if (duration !== false) {
18
21
  setTimeout(() => {
19
- service_1.flashMessageService.removeFlashMessage(flashMessage);
22
+ service_1.flashMessageService.removeFlashMessage(flashMessage, 'duration-elapsed');
20
23
  }, duration);
21
24
  }
22
25
  return flashMessage;
23
26
  }
24
27
  exports.addFlashMessage = addFlashMessage;
28
+ /**
29
+ * Manually removes the flash message from the flash message store.
30
+ *
31
+ * @param flashMessage The flash message you want to remove.
32
+ */
25
33
  function removeFlashMessage(flashMessage) {
26
- service_1.flashMessageService.removeFlashMessage(flashMessage);
34
+ service_1.flashMessageService.removeFlashMessage(flashMessage, 'manually-removed');
27
35
  }
28
36
  exports.removeFlashMessage = removeFlashMessage;
29
37
  /**
30
38
  * Adds a flash message of the type 'ERROR' on the flash message queue.
31
39
  *
32
- * @param {FlashMessageCreatorConfig} { text, onClick, data }
40
+ * @param {FlashMessageCreatorConfig} Config The config of the flash message
33
41
  */
34
- function addError({ text, onClick, data }) {
35
- return addFlashMessage({ type: 'ERROR', duration: 10000, text, onClick, data });
42
+ function addError(config) {
43
+ return addFlashMessage(Object.assign({ type: 'ERROR', duration: 10000 }, config));
36
44
  }
37
45
  exports.addError = addError;
38
46
  /**
@@ -40,10 +48,10 @@ exports.addError = addError;
40
48
  * After 7000 milliseconds it will automatically be removed
41
49
  * from the queue.
42
50
  *
43
- * @param {FlashMessageCreatorConfig} { text, onClick, data }
51
+ * @param {FlashMessageCreatorConfig} Config The config of the flash message
44
52
  */
45
- function addWarning({ text, onClick, data }) {
46
- return addFlashMessage({ type: 'WARNING', duration: 7000, text, onClick, data });
53
+ function addWarning(config) {
54
+ return addFlashMessage(Object.assign({ type: 'WARNING', duration: 7000 }, config));
47
55
  }
48
56
  exports.addWarning = addWarning;
49
57
  /**
@@ -51,10 +59,10 @@ exports.addWarning = addWarning;
51
59
  * After 2000 milliseconds it will automatically be removed
52
60
  * from the queue.
53
61
  *
54
- * @param {FlashMessageCreatorConfig} { text, onClick, data }
62
+ * @param {FlashMessageCreatorConfig} Config The config of the flash message
55
63
  */
56
- function addSuccess({ text, onClick, data }) {
57
- return addFlashMessage({ type: 'SUCCESS', duration: 2000, text, onClick, data });
64
+ function addSuccess(config) {
65
+ return addFlashMessage(Object.assign({ type: 'SUCCESS', duration: 2000 }, config));
58
66
  }
59
67
  exports.addSuccess = addSuccess;
60
68
  /**
@@ -62,27 +70,28 @@ exports.addSuccess = addSuccess;
62
70
  * After 5000 milliseconds it will automatically be removed
63
71
  * from the queue.
64
72
  *
65
- * @param {FlashMessageCreatorConfig} { text, onClick, data }
73
+ * @param {FlashMessageCreatorConfig} Config The config of the flash message
66
74
  */
67
- function addInfo({ text, onClick, data }) {
68
- return addFlashMessage({ type: 'INFO', duration: 5000, text, onClick, data });
75
+ function addInfo(config) {
76
+ return addFlashMessage(Object.assign({ type: 'INFO', duration: 5000 }, config));
69
77
  }
70
78
  exports.addInfo = addInfo;
71
79
  /**
72
80
  * Adds a flash message of the type 'APOCALYPSE' on the flash message queue.
73
81
  * This message is never removed from the queue automatically.
74
82
  *
75
- * @param {FlashMessageCreatorConfig} { text, onClick, data }
83
+ * @param {FlashMessageCreatorConfig} Config The config of the flash message
76
84
  */
77
- function addApocalypse({ text, onClick, data }) {
78
- return addFlashMessage({ type: 'APOCALYPSE', duration: false, text, onClick, data });
85
+ function addApocalypse(config) {
86
+ return addFlashMessage(Object.assign({ type: 'APOCALYPSE', duration: false }, config));
79
87
  }
80
88
  exports.addApocalypse = addApocalypse;
81
89
  function addIdAndOnClick(flashMessage) {
82
90
  const onClick = flashMessage.onClick !== undefined ? flashMessage.onClick : noop;
91
+ const onRemove = flashMessage.onRemove !== undefined ? flashMessage.onRemove : noop;
83
92
  // By adding the onClick it is no longer optional, TypeScript
84
93
  // does not recognize this so the cast forces it to.
85
- let f = flashMessage;
94
+ const f = flashMessage;
86
95
  // Assign a unique id to the flash message;
87
96
  f.id = nextFlashMessageId;
88
97
  nextFlashMessageId += 1;
@@ -94,6 +103,14 @@ function addIdAndOnClick(flashMessage) {
94
103
  f.onClick = () => {
95
104
  onClick(f);
96
105
  };
106
+ // The FlashMessageConfig's onRemove requires a FlashMessage instance
107
+ // but the FlashMessage's onRemove does not. The reason for this we
108
+ // do not want your user to have to manually provide the flashMessage
109
+ // for the onRemove. So we create an onRemove which calls the provided
110
+ // onRemove from the config with the FlashMessage.
111
+ f.onRemove = (reason) => {
112
+ onRemove(f, reason);
113
+ };
97
114
  return f;
98
115
  }
99
116
  // This export is purely for unit testing
@@ -101,3 +118,8 @@ function resetNextFlashMessageId() {
101
118
  nextFlashMessageId = 1;
102
119
  }
103
120
  exports.resetNextFlashMessageId = resetNextFlashMessageId;
121
+ // This function does not do anything, and is used as the default
122
+ // for the onClick and onRemove
123
+ function noop() {
124
+ return undefined;
125
+ }
package/lib/hooks.d.ts CHANGED
@@ -1,5 +1,6 @@
1
1
  import { FlashMessage } from './models';
2
2
  /**
3
- * Returns the flashMessages.
3
+ * Provides the active flash messages which are currently in the
4
+ * flash message store.
4
5
  */
5
- export declare function useFlashMessages(): FlashMessage<any>[];
6
+ export declare function useFlashMessages(): FlashMessage<unknown>[];
package/lib/hooks.js CHANGED
@@ -1,13 +1,15 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.useFlashMessages = void 0;
3
4
  const react_1 = require("react");
4
5
  const service_1 = require("./service");
5
6
  /**
6
- * Returns the flashMessages.
7
+ * Provides the active flash messages which are currently in the
8
+ * flash message store.
7
9
  */
8
10
  function useFlashMessages() {
9
- const [state, setState] = react_1.useState(service_1.flashMessageService.getFlashMessages());
10
- react_1.useEffect(() => {
11
+ const [state, setState] = (0, react_1.useState)(service_1.flashMessageService.getFlashMessages());
12
+ (0, react_1.useEffect)(() => {
11
13
  const subscriber = (nextState) => {
12
14
  setState(nextState);
13
15
  };
package/lib/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  export { addError, addWarning, addSuccess, addInfo, addApocalypse, addFlashMessage, removeFlashMessage } from './actions';
2
- export { FlashMessage, FlashMessageConfig, FlashMessageCreatorConfig, OnFlashMessageClicked, } from './models';
2
+ export { FlashMessage, FlashMessageConfig, FlashMessageCreatorConfig, OnFlashMessageClicked, OnFlashMessageRemoved, FlashMessageRemovedReason } from './models';
3
3
  export { useFlashMessages } from './hooks';
4
4
  export { FlashMessagesProvider, FlashMessagesContext } from './provider';
5
5
  export { flashMessageService } from './service';
package/lib/index.js CHANGED
@@ -1,17 +1,18 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.flashMessageService = exports.FlashMessagesContext = exports.FlashMessagesProvider = exports.useFlashMessages = exports.removeFlashMessage = exports.addFlashMessage = exports.addApocalypse = exports.addInfo = exports.addSuccess = exports.addWarning = exports.addError = void 0;
3
4
  var actions_1 = require("./actions");
4
- exports.addError = actions_1.addError;
5
- exports.addWarning = actions_1.addWarning;
6
- exports.addSuccess = actions_1.addSuccess;
7
- exports.addInfo = actions_1.addInfo;
8
- exports.addApocalypse = actions_1.addApocalypse;
9
- exports.addFlashMessage = actions_1.addFlashMessage;
10
- exports.removeFlashMessage = actions_1.removeFlashMessage;
5
+ Object.defineProperty(exports, "addError", { enumerable: true, get: function () { return actions_1.addError; } });
6
+ Object.defineProperty(exports, "addWarning", { enumerable: true, get: function () { return actions_1.addWarning; } });
7
+ Object.defineProperty(exports, "addSuccess", { enumerable: true, get: function () { return actions_1.addSuccess; } });
8
+ Object.defineProperty(exports, "addInfo", { enumerable: true, get: function () { return actions_1.addInfo; } });
9
+ Object.defineProperty(exports, "addApocalypse", { enumerable: true, get: function () { return actions_1.addApocalypse; } });
10
+ Object.defineProperty(exports, "addFlashMessage", { enumerable: true, get: function () { return actions_1.addFlashMessage; } });
11
+ Object.defineProperty(exports, "removeFlashMessage", { enumerable: true, get: function () { return actions_1.removeFlashMessage; } });
11
12
  var hooks_1 = require("./hooks");
12
- exports.useFlashMessages = hooks_1.useFlashMessages;
13
+ Object.defineProperty(exports, "useFlashMessages", { enumerable: true, get: function () { return hooks_1.useFlashMessages; } });
13
14
  var provider_1 = require("./provider");
14
- exports.FlashMessagesProvider = provider_1.FlashMessagesProvider;
15
- exports.FlashMessagesContext = provider_1.FlashMessagesContext;
15
+ Object.defineProperty(exports, "FlashMessagesProvider", { enumerable: true, get: function () { return provider_1.FlashMessagesProvider; } });
16
+ Object.defineProperty(exports, "FlashMessagesContext", { enumerable: true, get: function () { return provider_1.FlashMessagesContext; } });
16
17
  var service_1 = require("./service");
17
- exports.flashMessageService = service_1.flashMessageService;
18
+ Object.defineProperty(exports, "flashMessageService", { enumerable: true, get: function () { return service_1.flashMessageService; } });
package/lib/models.d.ts CHANGED
@@ -1,21 +1,144 @@
1
- export declare type OnFlashMessageClicked<Data> = (msg: FlashMessage<Data>) => void;
2
- export interface FlashMessage<Data> {
1
+ export declare type OnFlashMessageClicked<Data> = (flashMessage: FlashMessage<Data>) => void;
2
+ /**
3
+ * The reason a flash message was removed.
4
+ *
5
+ * When it is 'duration-elapsed' it means that the flash message
6
+ * was removed because the duration of the message expired. When
7
+ * it is 'manually-removed' it was because the `removeFlashMessage`
8
+ * action was called.
9
+ */
10
+ export declare type FlashMessageRemovedReason = 'duration-elapsed' | 'manually-removed';
11
+ export declare type OnFlashMessageRemoved<Data> = (flashMessage: FlashMessage<Data>, reason: FlashMessageRemovedReason) => void;
12
+ /**
13
+ * Represents a FlashMessage as it will be represented in the
14
+ * flashMessageService.
15
+ */
16
+ export declare type FlashMessage<Data> = {
17
+ /**
18
+ * The id of the flashMessage must be unique for each flash message.
19
+ */
3
20
  id: number;
21
+ /**
22
+ * The type of flash message, can be useful to distinguish
23
+ * between types of messages. For example you might have a type
24
+ * 'error', 'warning' or 'info'.
25
+ */
4
26
  type: string;
27
+ /**
28
+ * The text message you want to show to the user.
29
+ */
5
30
  text: string;
31
+ /**
32
+ * How long a flash message should be should to the user.
33
+ *
34
+ * When `false` it will never disappear.
35
+ */
6
36
  duration: number | false;
37
+ /**
38
+ * The onClick handler for the flash message, used to perform
39
+ * actions when the user clicks on the FlashMessage
40
+ *
41
+ * You must call onClick from inside your component representing
42
+ * the flash message when it is clicked. Otherwise the onClick
43
+ * callback will never be called.
44
+ */
7
45
  onClick: () => void;
46
+ /**
47
+ * When the flash message is removed this callback is executed,
48
+ * to let the user know the message was removed. The reason
49
+ * for removal must be provided as the second argument it can either
50
+ * be 'duration-elapsed' or 'manually-removed'.
51
+ *
52
+ * When it is 'duration-elapsed' it means that the flash message
53
+ * was removed because the duration of the message expired. When
54
+ * it is 'manually-removed' it was because the `removeFlashMessage`
55
+ * action was called.
56
+ */
57
+ onRemove: (reason: FlashMessageRemovedReason) => void;
58
+ /**
59
+ * This 'data' object can be used to store any custom data you want
60
+ * to associate with the Flash Message. The data 'key' will never
61
+ * be used / manipulated now and in the future.
62
+ */
8
63
  data?: Data;
9
- }
10
- export interface FlashMessageConfig<Data> {
64
+ };
65
+ /**
66
+ * Represents an a configuration from which an actual FlashMessage
67
+ * instance can be created. FlashMessageConfig is used by the
68
+ * `addFlashMessage` action.
69
+ *
70
+ * The reason it exists is so we can hide complexity for the developer
71
+ * when spawning flash messages vs consuming them to show them in the UI.
72
+ * For example the FlashMessage onClick and onRemove does not have the
73
+ * the instance of the flash message as the first argument, but the
74
+ * FlashMessageConfig does.
75
+ *
76
+ * Behind the scenes the onClick and onRemove will be changed before
77
+ * creating a the FlashMessage from the FlashMessageConfig and they
78
+ * will be re-written.
79
+ */
80
+ export declare type FlashMessageConfig<Data> = {
81
+ /**
82
+ * The type of flash message, can be useful to distinguish
83
+ * between types of messages. For example you might have a type
84
+ * 'error', 'warning' or 'info'.
85
+ */
11
86
  type: string;
87
+ /**
88
+ * The text message you want to show to the user.
89
+ */
12
90
  text: string;
91
+ /**
92
+ * How long a flash message should be should to the user.
93
+ *
94
+ * When `false` it will never disappear.
95
+ */
13
96
  duration: number | false;
97
+ /**
98
+ * Optional callback for what needs to happen when the message is
99
+ * clicked. Should receive the FlashMessage which is clicked.
100
+ */
14
101
  onClick?: OnFlashMessageClicked<Data>;
102
+ /**
103
+ * Optional callback for what needs to happen when the message is
104
+ * removed. Should receive the FlashMessage which is clicked, and
105
+ * the reason for closure.
106
+ */
107
+ onRemove?: OnFlashMessageRemoved<Data>;
108
+ /**
109
+ * This 'data' object can be used to store any custom data you want
110
+ * to associate with the Flash Message. The data 'key' will never
111
+ * be used / manipulated now and in the future.
112
+ */
15
113
  data?: Data;
16
- }
17
- export interface FlashMessageCreatorConfig<Data> {
114
+ };
115
+ /**
116
+ * Represents an a configuration from which an actual FlashMessage
117
+ * instance can be created. FlashMessageCreatorConfig is used by the
118
+ * flash message creator actions such as: `addError` and `addWarning`
119
+ * etc etc, so the developer can quickly spawn flash messages without
120
+ * resorting to the more low level `addFlashMessage` action.
121
+ */
122
+ export declare type FlashMessageCreatorConfig<Data> = {
123
+ /**
124
+ * The text message you want to show to the user.
125
+ */
18
126
  text: string;
127
+ /**
128
+ * Optional callback for what needs to happen when the message is
129
+ * clicked. Should receive the FlashMessage which is clicked.
130
+ */
19
131
  onClick?: OnFlashMessageClicked<Data>;
132
+ /**
133
+ * Optional callback for what needs to happen when the message is
134
+ * removed. Should receive the FlashMessage which is clicked, and
135
+ * the reason for closure.
136
+ */
137
+ onRemove?: OnFlashMessageRemoved<Data>;
138
+ /**
139
+ * This 'data' object can be used to store any custom data you want
140
+ * to associate with the Flash Message. The data 'key' will never
141
+ * be used / manipulated now and in the future.
142
+ */
20
143
  data?: Data;
21
- }
144
+ };
package/lib/provider.d.ts CHANGED
@@ -1,7 +1,14 @@
1
1
  import React from 'react';
2
2
  import { FlashMessage } from './models';
3
- export declare const FlashMessagesContext: React.Context<FlashMessage<any>[]>;
4
- export interface Props {
3
+ export declare const FlashMessagesContext: React.Context<FlashMessage<unknown>[]>;
4
+ export declare type Props = {
5
5
  children: React.ReactNode;
6
- }
6
+ };
7
+ /**
8
+ * Provider which provides the a flash message context. Should only
9
+ * be used when needing the `FlashMessagesContext`. We recommend
10
+ * using the `useFlashMessages` hook instead were possible.
11
+ *
12
+ * @param children The children to render
13
+ */
7
14
  export declare function FlashMessagesProvider({ children }: Props): JSX.Element;
package/lib/provider.js CHANGED
@@ -3,12 +3,20 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
3
3
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
4
  };
5
5
  Object.defineProperty(exports, "__esModule", { value: true });
6
+ exports.FlashMessagesProvider = exports.FlashMessagesContext = void 0;
6
7
  const react_1 = __importDefault(require("react"));
7
8
  const hooks_1 = require("./hooks");
8
9
  const service_1 = require("./service");
9
10
  exports.FlashMessagesContext = react_1.default.createContext(service_1.flashMessageService.getFlashMessages());
11
+ /**
12
+ * Provider which provides the a flash message context. Should only
13
+ * be used when needing the `FlashMessagesContext`. We recommend
14
+ * using the `useFlashMessages` hook instead were possible.
15
+ *
16
+ * @param children The children to render
17
+ */
10
18
  function FlashMessagesProvider({ children }) {
11
- const flashMessages = hooks_1.useFlashMessages();
19
+ const flashMessages = (0, hooks_1.useFlashMessages)();
12
20
  return (react_1.default.createElement(exports.FlashMessagesContext.Provider, { value: flashMessages }, children));
13
21
  }
14
22
  exports.FlashMessagesProvider = FlashMessagesProvider;
package/lib/service.d.ts CHANGED
@@ -1,12 +1,12 @@
1
- import { FlashMessage } from './models';
2
- export declare type Subscriber = (flashMessages: FlashMessage<any>[]) => void;
3
- export interface FlashMessageService {
4
- addFlashMessage(flashMessage: FlashMessage<any>): void;
5
- removeFlashMessage(flashMessage: FlashMessage<any>): void;
1
+ import { FlashMessage, FlashMessageRemovedReason } from './models';
2
+ export declare type Subscriber = (flashMessages: FlashMessage<unknown>[]) => void;
3
+ export declare type FlashMessageService = {
4
+ addFlashMessage(flashMessage: FlashMessage<unknown>): void;
5
+ removeFlashMessage(flashMessage: FlashMessage<unknown>, reason: FlashMessageRemovedReason): void;
6
6
  clearFlashMessages(): void;
7
7
  subscribe(subscriber: Subscriber): void;
8
8
  unsubscribe(subscriber: Subscriber): void;
9
- getFlashMessages: () => FlashMessage<any>[];
10
- }
9
+ getFlashMessages: () => FlashMessage<unknown>[];
10
+ };
11
11
  export declare function makeFlashMessageService(): FlashMessageService;
12
12
  export declare const flashMessageService: FlashMessageService;
package/lib/service.js CHANGED
@@ -1,5 +1,6 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.flashMessageService = exports.makeFlashMessageService = void 0;
3
4
  function makeFlashMessageService() {
4
5
  let flashMessages = [];
5
6
  let subscribers = [];
@@ -9,14 +10,23 @@ function makeFlashMessageService() {
9
10
  clearFlashMessages,
10
11
  getFlashMessages,
11
12
  subscribe,
12
- unsubscribe,
13
+ unsubscribe
13
14
  };
14
15
  function addFlashMessage(flashMessage) {
15
16
  flashMessages.push(flashMessage);
16
17
  informSubscribers();
17
18
  }
18
- function removeFlashMessage(flashMessage) {
19
- flashMessages = flashMessages.filter(f => f !== flashMessage);
19
+ function removeFlashMessage(flashMessage, reason) {
20
+ const remainingFlashMessages = [];
21
+ flashMessages.forEach((f) => {
22
+ if (f !== flashMessage) {
23
+ remainingFlashMessages.push(f);
24
+ }
25
+ else {
26
+ f.onRemove(reason);
27
+ }
28
+ });
29
+ flashMessages = remainingFlashMessages;
20
30
  informSubscribers();
21
31
  }
22
32
  function clearFlashMessages() {
@@ -31,10 +41,10 @@ function makeFlashMessageService() {
31
41
  subscriber(flashMessages);
32
42
  }
33
43
  function unsubscribe(subscriber) {
34
- subscribers = subscribers.filter(s => s !== subscriber);
44
+ subscribers = subscribers.filter((s) => s !== subscriber);
35
45
  }
36
46
  function informSubscribers() {
37
- subscribers.forEach(subscriber => subscriber([...flashMessages]));
47
+ subscribers.forEach((subscriber) => subscriber([...flashMessages]));
38
48
  }
39
49
  }
40
50
  exports.makeFlashMessageService = makeFlashMessageService;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@42.nl/react-flash-messages",
3
- "version": "0.0.2",
3
+ "version": "2.0.1",
4
4
  "description": "Storing flash messages via a nice api for use with React.",
5
5
  "files": [
6
6
  "lib"
@@ -21,60 +21,72 @@
21
21
  },
22
22
  "homepage": "https://github.com/42BV/flash-messages#readme",
23
23
  "devDependencies": {
24
- "@testing-library/react": "8.0.4",
25
- "@types/fetch-mock": "7.2.3",
26
- "@types/jest": "24.0.9",
27
- "@types/react": "16.8.6",
28
- "@types/react-dom": "16.8.2",
29
- "@types/react-router-dom": "4.3.1",
30
- "@typescript-eslint/eslint-plugin": "1.4.2",
31
- "@typescript-eslint/parser": "1.4.2",
32
- "eslint": "5.15.3",
33
- "jest": "24.8.0",
34
- "jest-dom": "3.5.0",
35
- "prettier": "1.16.4",
36
- "ts-jest": "24.0.2",
37
- "typescript": "3.5.2",
38
- "husky": "1.3.1",
39
- "lint-staged": "8.1.5",
40
- "react": "16.8.6",
41
- "react-addons-test-utils": "15.6.2",
42
- "react-dom": "16.8.6"
24
+ "@testing-library/jest-dom": "5.16.2",
25
+ "@testing-library/react": "12.1.4",
26
+ "@types/jest": "27.4.1",
27
+ "@types/react": "17.0.40",
28
+ "@types/react-dom": "17.0.13",
29
+ "@typescript-eslint/eslint-plugin": "5.14.0",
30
+ "@typescript-eslint/parser": "5.14.0",
31
+ "eslint": "8.10.0",
32
+ "eslint-config-react-app": "7.0.0",
33
+ "eslint-plugin-jest": "26.1.1",
34
+ "eslint-plugin-react": "7.29.3",
35
+ "eslint-plugin-react-hooks": "4.3.0",
36
+ "husky": "7.0.4",
37
+ "jest": "27.5.1",
38
+ "jest-watch-typeahead": "1.0.0",
39
+ "lint-staged": "12.3.5",
40
+ "np": "^7.6.0",
41
+ "prettier": "2.5.1",
42
+ "react": "17.0.2",
43
+ "react-dom": "17.0.2",
44
+ "ts-jest": "27.1.3",
45
+ "typescript": "4.6.2"
43
46
  },
44
47
  "scripts": {
45
- "start": "jest test --watch",
46
- "test": "npm run lint && jest test --coverage",
47
- "ts": "tsc --version && tsc",
48
- "coverage": "npm test -- --coverage",
49
- "lint": "eslint \"src/**\"",
50
- "prepublish": "rm -rf lib && npm test && npm run ts"
48
+ "start": "jest --watch --coverage",
49
+ "clean": "rm -rf lib",
50
+ "test": "npm run lint && npm run test:ts && npm run test:coverage",
51
+ "test:ts": "tsc --version && tsc --noEmit",
52
+ "test:coverage": "jest test --no-cache --coverage",
53
+ "docs": "jekyll serve --source docs",
54
+ "tsc": "npm run clean && tsc --version && tsc",
55
+ "lint": "npm run lint:test && npm run lint:src",
56
+ "lint:test": "eslint \"tests/**\" --max-warnings=0",
57
+ "lint:src": "eslint \"src/**\" --max-warnings=0",
58
+ "release": "npm run tsc && np --otp",
59
+ "dev:publish": "./scripts/dev-publish.sh",
60
+ "version": "npm run tsc && jekyll build",
61
+ "prepare": "husky install"
51
62
  },
52
63
  "jest": {
53
- "globals": {
54
- "ts-jest": {
55
- "tsConfig": "<rootDir>/tsconfig.json"
64
+ "preset": "ts-jest",
65
+ "roots": [
66
+ "src",
67
+ "tests"
68
+ ],
69
+ "collectCoverageFrom": [
70
+ "./src/**/*.{ts,tsx}"
71
+ ],
72
+ "coverageThreshold": {
73
+ "global": {
74
+ "branches": 100,
75
+ "functions": 100,
76
+ "lines": 100,
77
+ "statements": 100
56
78
  }
57
79
  },
58
- "moduleFileExtensions": [
59
- "ts",
60
- "tsx",
61
- "js",
62
- "node"
80
+ "restoreMocks": true,
81
+ "watchPlugins": [
82
+ "jest-watch-typeahead/filename",
83
+ "jest-watch-typeahead/testname"
63
84
  ],
64
- "transform": {
65
- "\\.(ts|tsx)$": "ts-jest"
66
- },
67
- "testRegex": "/tests/.*\\.(ts|tsx)$"
85
+ "testEnvironment": "jsdom"
68
86
  },
69
87
  "lint-staged": {
70
88
  "{src,test}/**/*.{js,jsx,json,scss,tsx,ts}": [
71
- "prettier --single-quote --write",
72
- "git add"
89
+ "prettier --single-quote --trailing-comma none --write"
73
90
  ]
74
- },
75
- "husky": {
76
- "hooks": {
77
- "pre-commit": "lint-staged && node check-translations"
78
- }
79
91
  }
80
92
  }
package/CHANGELOG.md DELETED
@@ -1,12 +0,0 @@
1
- # Changelog
2
- All notable changes to this project will be documented in this file.
3
-
4
- The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
5
- and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).
6
-
7
- ## [Unreleased]
8
-
9
- ## [0.0.1] - 2019-08-08
10
-
11
- ### Added
12
- - The first version of this library.