Skip to content

Commit 2140028

Browse files
author
Shailendra Gupta
authored
Basic Setup for v1 (#1)
1 parent 8fdf408 commit 2140028

File tree

15 files changed

+584
-52
lines changed

15 files changed

+584
-52
lines changed

.eslintrc.js

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -19,16 +19,5 @@ module.exports = {
1919
rules: {
2020
'object-shorthand': ['error', 'always'],
2121
'spaced-comment': ['error', 'always'],
22-
'valid-jsdoc': [
23-
'error',
24-
{
25-
requireReturn: false,
26-
requireReturnDescription: false,
27-
requireParamDescription: true,
28-
prefer: {
29-
return: 'returns',
30-
},
31-
},
32-
],
3322
},
3423
};

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,4 +70,5 @@ package-lock.json
7070
dist/
7171
.DS_Store
7272
lib/
73+
docs/
7374
!.vscode

README.md

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,225 @@
11
# Flashr
2+
3+
Flash Messages handling with redux made simple.
4+
5+
![npm ico](https://nodeico.herokuapp.com/@peak-ai/flashr.svg)
6+
7+
![dependency status](https://img.shields.io/librariesio/release/npm/@peak-ai/flashr)
8+
![npm bundle size (scoped)](https://img.shields.io/bundlephobia/minzip/@peak-ai/flashr)
9+
![npm](https://img.shields.io/npm/dw/@peak-ai/flashr)
10+
![GitHub issues](https://img.shields.io/github/issues/peak-ai/flashr)
11+
![GitHub](https://img.shields.io/github/license/peak-ai/flashr)
12+
![npm (scoped)](https://img.shields.io/npm/v/@peak-ai/flashr)
13+
![GitHub contributors](https://img.shields.io/github/contributors/peak-ai/flashr)
14+
![GitHub top language](https://img.shields.io/github/languages/top/peak-ai/flashr)
15+
![Snyk Vulnerabilities for npm scoped package](https://img.shields.io/snyk/vulnerabilities/npm/@peak-ai/flashr)
16+
![GitHub Workflow Status](https://img.shields.io/github/workflow/status/peak-ai/flashr/Lint%20and%20Build)
17+
![GitHub forks](https://img.shields.io/github/forks/peak-ai/flashr?style=social)
18+
![GitHub Stars](https://img.shields.io/github/stars/peak-ai/flashr?style=social)
19+
![GitHub watchers](https://img.shields.io/github/watchers/peak-ai/flashr?style=social)
20+
21+
## To install
22+
23+
```bash
24+
npm install --save @peak-ai/flashr
25+
# or
26+
yarn add @peak-ai/flashr
27+
```
28+
29+
## Implementation
30+
31+
#### _Add Reducer_
32+
33+
```js
34+
import { createFlashReducer } from '@peak-ai/flashr';
35+
const flashMessages = createFlashReducer(config);
36+
const reducers = {
37+
// ... your other reducers ...
38+
flashMessages,
39+
};
40+
const rootReducer = combineReducers(reducers);
41+
```
42+
43+
#### _Add Redux Middleware_ (_Only needed if want to use hooks_)
44+
45+
```js
46+
import { createFlashMiddleware } from '@peak-ai/flashr';
47+
const { middleware } = createFlashMiddleware({});
48+
const store = createStore(rootReducer, applyMiddleware(middleware));
49+
```
50+
51+
## Config
52+
53+
#### _Create Reducer Config_
54+
55+
```typescript
56+
interface Config {
57+
/*
58+
Timeout after which notification should be removed.
59+
This can be overridden in addFlashMessage API.
60+
Default: 5000
61+
*/
62+
timeout: number;
63+
/*
64+
Position on the screen where the notification should appear.
65+
This can be overridden in addFlashMessage API.
66+
Default: 'left-top'
67+
*/
68+
position:
69+
| 'left-top'
70+
| 'center-top'
71+
| 'right-top'
72+
| 'left-bottom'
73+
| 'center-bottom'
74+
| 'right-bottom';
75+
/*
76+
Internally we maintain a queue for handling messages, this param gives only the desired number of messages at any point in time, rest will be available in the queue and added as messages are removed from message array.
77+
Default: 3
78+
*/
79+
stackCount: number;
80+
/*
81+
The above-said queue is a priority queue, this parameter tells if needs to be sorted.
82+
Default: false
83+
*/
84+
sortQueue: boolean;
85+
/*
86+
The above-said queue accepts custom comparator function, which somewhat works like Array.prototype.sort's callback function.
87+
It provides 2 numbers as input, a and b, return less than 0 if a is small || greater than 0 if b is small.
88+
For more insight see usage at `src/queue.ts:L26`
89+
Default: (a, b) => a - b;
90+
Higher priority elements dequeue first.
91+
*/
92+
comparator: Comparator;
93+
/*
94+
This function is used to create a unique id for every notification object.
95+
Default: uuid/v4
96+
*/
97+
keyFunction: () => string;
98+
/*
99+
Notification can have an action associated with them, like undo, stop etc...
100+
This can be overridden in addFlashMessage API.
101+
*/
102+
onActionClick: () => void;
103+
/*
104+
This in general onClick which can be used to add click on complete notification
105+
This can be overridden in addFlashMessage API.
106+
*/
107+
onClick: () => void;
108+
}
109+
```
110+
111+
#### _Create Redux Middleware Config_
112+
113+
```typescript
114+
interface Config {
115+
/*
116+
Optional
117+
With middleware you can hook custom functions before a message is added to the queue and after a message is added. this flag is used to disable those hooks.
118+
Default: false
119+
*/
120+
disableHooks: boolean;
121+
}
122+
```
123+
124+
#### _Hooks_
125+
126+
_When you create middleware, the function returns an object called hooks, which can be tapped with custom functions_
127+
128+
```typescript
129+
const {
130+
middleware,
131+
hooks: { preAdd, postAdd },
132+
} = createFlashMiddleware({});
133+
134+
preAdd.tap('PreAddAction', (action) => {
135+
console.log(action);
136+
});
137+
138+
postAdd.tap('PostAddAction', () => {
139+
console.log('PostAddAction');
140+
});
141+
```
142+
143+
## Action Creators
144+
145+
#### _addFlashMessage_
146+
147+
```typescript
148+
interface Message {
149+
/*
150+
Required*
151+
Text to be displayed in notification
152+
*/
153+
message: string;
154+
/*
155+
Optional
156+
Message type, like success, error, warn, etc..
157+
Default: text
158+
*/
159+
messageType: string;
160+
/*
161+
Optional
162+
Override specific timeout for single notification
163+
Default: text
164+
*/
165+
timeout: number;
166+
/*
167+
Optional
168+
Override specific position for single notification
169+
Default: 'left-top'
170+
*/
171+
position: string;
172+
/*
173+
Optional
174+
Icon for notification
175+
*/
176+
icon: string;
177+
/*
178+
Optional
179+
Override onActionClick for single notification
180+
*/
181+
onActionClick: () => void;
182+
/*
183+
Optional
184+
Override onClick for single notification
185+
*/
186+
onClick: () => void;
187+
/*
188+
Optional
189+
Add optional class to add in notification
190+
*/
191+
className: string;
192+
/*
193+
Optional
194+
Add priority to a message
195+
When enabled sorting of queue (see sortQueue Option), comparator function is called, if passed, else use maxComparator function, higher priority elements at first in the queue.
196+
Default: 0
197+
*/
198+
priority: number;
199+
}
200+
```
201+
202+
#### _clearFlashMessage_
203+
204+
```typescript
205+
interface ClearMessage {
206+
/*
207+
Removes notification from message array for given id. Currently, there is no option to remove messages from the queue, except clearAll.
208+
*/
209+
id: string;
210+
}
211+
```
212+
213+
#### _clearAll_
214+
215+
No params needed _that is easy_
216+
217+
#### _Action Constants Exported_
218+
219+
- _ADD_MESSAGE_
220+
- _CLEAR_MESSAGE_
221+
- _CLEAR_ALL_MESSAGES_
222+
223+
## License
224+
225+
BSD-3-Clause

package.json

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@peak-ai/flashr",
3-
"version": "0.0.1",
3+
"version": "0.0.1-pre-release",
44
"main": "lib/index.js",
55
"module": "esm/index.js",
66
"description": "Redux flash messages handling made simple..",
@@ -9,13 +9,13 @@
99
"license": "BSD-3-Clause",
1010
"scripts": {
1111
"build": "yarn run clean && yarn run build:cjs && yarn run build:esm && yarn run build:dist",
12-
"build:esm": "tsc -p . -m ESNext --outDir esm",
1312
"build:cjs": "tsc -p . -m CommonJS --outDir lib",
13+
"build:esm": "tsc -p . -m ESNext --outDir esm",
1414
"build:dist": "rollup -c",
1515
"build:docs": "rm -rf docs && typedoc --name Flash src",
16-
"lint": "eslint src/**/*.ts",
16+
"clean": "rm -rf lib/ && rm -rf esm/ && rm -rf dist",
1717
"format:check": "prettier --check src/**/*.ts",
18-
"clean": "rm -rf lib/ && rm -rf esm/ && rm -rf dist"
18+
"lint": "eslint src/**/*.ts"
1919
},
2020
"dependencies": {
2121
"tapable": "^1.1.3",
@@ -36,12 +36,8 @@
3636
"rollup": "^2.3.2",
3737
"rollup-plugin-terser": "^5.3.0",
3838
"rollup-plugin-typescript2": "^0.27.0",
39-
"ts-node": "^8.8.1",
4039
"typescript": "^3.8.3"
4140
},
42-
"peerDependencies": {
43-
"aws-sdk": "^2.585.0"
44-
},
4541
"husky": {
4642
"hooks": {
4743
"pre-commit": "lint-staged"

src/actions.ts

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
import { ADD_MESSAGE, CLEAR_MESSAGE, CLEAR_ALL_MESSAGES } from './constants';
2+
import { Message } from './factory';
3+
4+
export type AddMessageParams = Partial<Message> & { message: string };
5+
export interface AddMessageAction {
6+
type: typeof ADD_MESSAGE;
7+
payload: AddMessageParams;
8+
force?: boolean;
9+
}
10+
export type AddMessageActionCreator = (
11+
payload: AddMessageParams,
12+
force?: boolean
13+
) => AddMessageAction;
14+
export const addFlashMessage: AddMessageActionCreator = (
15+
payload: AddMessageParams,
16+
force = false
17+
) => {
18+
return {
19+
type: ADD_MESSAGE,
20+
payload,
21+
force,
22+
};
23+
};
24+
25+
export interface ClearMessageParams {
26+
id: string;
27+
}
28+
export interface ClearMessageAction {
29+
type: typeof CLEAR_MESSAGE;
30+
payload: {
31+
id: string;
32+
};
33+
}
34+
export type ClearFlashMessageActionCreator = (payload: ClearMessageParams) => ClearMessageAction;
35+
export const clearFlashMessage: ClearFlashMessageActionCreator = ({ id }) => {
36+
return {
37+
type: CLEAR_MESSAGE,
38+
payload: {
39+
id,
40+
},
41+
};
42+
};
43+
44+
export interface ClearAllMessageAction {
45+
type: typeof CLEAR_ALL_MESSAGES;
46+
}
47+
export type ClearAllActionCreator = () => ClearAllMessageAction;
48+
export const clearAll: ClearAllActionCreator = () => {
49+
return {
50+
type: CLEAR_ALL_MESSAGES,
51+
};
52+
};

src/comparator.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
export type ComparatorType = (a: number, b: number) => number;
2+
3+
export const maxComparator: ComparatorType = (a, b) => a - b;
4+
5+
export const noSortComparator: ComparatorType = () => 1;

src/constants.ts

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
import { v4 } from 'uuid';
2+
import { ComparatorType } from './comparator';
3+
import { noop } from './utils';
4+
import { maxComparator, noSortComparator } from './comparator';
5+
6+
export const ADD_MESSAGE = '@@flashr/__ADD_MESSAGE__';
7+
export const CLEAR_MESSAGE = '@@flashr/__CLEAR_MESSAGE__';
8+
export const CLEAR_ALL_MESSAGES = '@@flashr/__CLEAR_ALL_MESSAGES__';
9+
export const NO_OP_ACTION = '@@flashr/__NO_OP_ACTION__';
10+
11+
export interface Config {
12+
timeout: number;
13+
position:
14+
| 'left-top'
15+
| 'center-top'
16+
| 'right-top'
17+
| 'left-bottom'
18+
| 'center-bottom'
19+
| 'right-bottom';
20+
stackCount: number;
21+
sortQueue: boolean;
22+
comparator: ComparatorType;
23+
keyFunction: () => string;
24+
onActionClick: () => void;
25+
onClick: () => void;
26+
}
27+
28+
export function withDefaultsConfig(config: Partial<Config>): Config {
29+
const sortQueueConfig = config.sortQueue || false;
30+
return {
31+
timeout: config.timeout || 5000,
32+
position: config.position || 'left-top',
33+
stackCount: config.stackCount || 3,
34+
sortQueue: sortQueueConfig,
35+
comparator: sortQueueConfig ? config.comparator || maxComparator : noSortComparator,
36+
keyFunction: config.keyFunction || v4,
37+
onActionClick: config.onActionClick || noop,
38+
onClick: config.onClick || noop,
39+
};
40+
}

0 commit comments

Comments
 (0)