Skip to content

Commit 72708bf

Browse files
committed
#29 Added onRemoval to notification
1 parent c52f863 commit 72708bf

File tree

8 files changed

+38
-54
lines changed

8 files changed

+38
-54
lines changed

src/react-notification-component.js

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import React from 'react';
2-
import ReactNotification from 'src/react-notification';
2+
import ReactNotification from './react-notification';
33
import PropTypes from 'prop-types';
44
import store from './store';
55
import {
66
getNotificationsForEachContainer,
77
getNotificationsForMobileView
8-
} from 'src/helpers';
8+
} from './utils/helpers';
99

1010
import 'src/scss/notification.scss';
1111

@@ -28,8 +28,7 @@ export default class ReactNotificationComponent extends React.Component {
2828
static propTypes = {
2929
isMobile: PropTypes.bool,
3030
breakpoint: PropTypes.number,
31-
types: PropTypes.array,
32-
onNotificationRemoval: PropTypes.func
31+
types: PropTypes.array
3332
}
3433

3534
static defaultProps = {
@@ -77,14 +76,7 @@ export default class ReactNotificationComponent extends React.Component {
7776
}));
7877
}
7978

80-
toggleRemoval(id, removalFlag) {
81-
const { onNotificationRemoval } = this.props;
82-
const callback = () => {
83-
if (onNotificationRemoval) {
84-
onNotificationRemoval(id, removalFlag);
85-
}
86-
};
87-
79+
toggleRemoval(id, callback) {
8880
this.setState(({ notifications }) => ({
8981
notifications: notifications.filter(({ id: nId }) => nId !== id)
9082
}), callback);

src/react-notification.js

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import React from 'react';
22
import PropTypes from 'prop-types';
3-
import Timer from './timer';
4-
import { REMOVAL } from './constants';
3+
import Timer from './utils/timer';
54
import {
65
getTransition,
76
hasFullySwiped,
87
getHtmlClassesForType,
98
shouldNotificationHaveSliding
10-
} from 'src/helpers';
9+
} from './utils/helpers';
10+
import { REMOVAL } from './utils/constants';
1111

1212
export default class ReactNotification extends React.Component {
1313
constructor(props) {
@@ -89,13 +89,13 @@ export default class ReactNotification extends React.Component {
8989

9090
removeNotification(removalFlag) {
9191
const { notification, toggleRemoval } = this.props;
92-
const { dismiss: { waitForAnimation } } = notification;
92+
const { id, onRemoval, dismiss: { waitForAnimation } } = notification;
9393
const htmlClassList = [
9494
...notification.animationOut,
9595
...getHtmlClassesForType(notification)
9696
];
9797

98-
const onTransitionEnd = () => toggleRemoval(notification.id, removalFlag);
98+
const onTransitionEnd = () => toggleRemoval(id, () => onRemoval(id, removalFlag));
9999
const parentStyle = {
100100
height: 0,
101101
transition: getTransition(notification.slidingExit, 'height')
@@ -147,6 +147,7 @@ export default class ReactNotification extends React.Component {
147147
toggleRemoval,
148148
notification: {
149149
id,
150+
onRemoval,
150151
slidingExit,
151152
touchSlidingExit: { swipe, fade }
152153
}
@@ -160,7 +161,9 @@ export default class ReactNotification extends React.Component {
160161
if (hasFullySwiped(distance)) {
161162
const t1 = getTransition(swipe, 'left');
162163
const t2 = getTransition(fade, 'opacity');
163-
const onTransitionEnd = () => toggleRemoval(id, REMOVAL.TOUCH);
164+
const onTransitionEnd = () => {
165+
toggleRemoval(id, () => onRemoval(id, REMOVAL.TOUCH));
166+
}
164167

165168
return this.setState(({ parentStyle }) => ({
166169
touchEnabled: false,

src/store.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import { parseNotification } from './helpers';
2-
import { validateTransition, validators } from './validators';
1+
import { parseNotification } from './utils/helpers';
2+
import { validateTransition, validators } from './utils/validators';
33

44
function Store() {
55
this.types = null;

src/utils.js

Lines changed: 0 additions & 23 deletions
This file was deleted.

src/constants.js renamed to src/utils/constants.js

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,5 +55,6 @@ export const ERROR = {
5555
TRANSITION_DURATION_NUMBER: 'Validation error. `duration` property of `transition` option must be a Number',
5656
TRANSITION_TIMING_FUNCTION: 'Validation error. `timingFunction` property of `transition` option must be a String',
5757
TRANSITION_DELAY_NUMBER: 'Validation error. `delay` property of `transition` option must be a Number',
58-
TYPE_NOT_FOUND: 'Validation error. Custom type not found'
58+
TYPE_NOT_FOUND: 'Validation error. Custom type not found',
59+
REMOVAL_FUNC: 'Validation error. `onRemoval` must be a function'
5960
};

src/helpers.js renamed to src/utils/helpers.js

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ import {
33
INSERTION,
44
NOTIFICATION_BASE_CLASS,
55
NOTIFICATION_TYPE as NT,
6-
} from 'src/constants';
7-
import { isNull, getRandomId } from 'src/utils';
6+
} from './constants';
7+
8+
const getRandomId = () => Math.random().toString(36).substr(2, 9);
9+
const isNull = (object) => object === null || object === undefined;
810

911
export function isBottomContainer(container) {
1012
return container === CONTAINER.BOTTOM_LEFT
@@ -190,7 +192,8 @@ export function parseNotification(options, userDefinedTypes) {
190192
touchRevert,
191193
touchSlidingExit,
192194
dismiss,
193-
width
195+
width,
196+
onRemoval
194197
} = notification;
195198

196199
notification.id = id || getRandomId();
@@ -209,6 +212,7 @@ export function parseNotification(options, userDefinedTypes) {
209212
notification.dismiss = defaultDismiss(dismiss);
210213
notification.animationIn = animationIn || [];
211214
notification.animationOut = animationOut || [];
215+
notification.onRemoval = onRemoval || (() => {});
212216

213217
const t = (duration, timingFunction, delay) => ({
214218
duration,
File renamed without changes.

src/validators.js renamed to src/utils/validators.js

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React from 'react';
22
import { ERROR, NOTIFICATION_TYPE as NT } from './constants';
3-
import {
4-
isNull,
5-
isString,
6-
isNumber,
7-
isBoolean,
8-
isArray
9-
} from 'src/utils';
3+
4+
const isNull = (object) => object === null || object === undefined;
5+
const isString = (object) => typeof object === 'string';
6+
const isNumber = (object) => typeof object === 'number';
7+
const isBoolean = (object) => typeof object === 'boolean';
8+
const isFunction = (object) => !!(object && object.constructor && object.call && object.apply);
9+
const isArray = (object) => !isNull(object) && object.constructor === Array;
1010

1111
function isClassComponent(component) {
1212
return typeof component === 'function'
@@ -148,6 +148,13 @@ export const validators = [
148148
}
149149
},
150150

151+
function onRemoval({ onRemoval }) {
152+
if (!onRemoval) return;
153+
if (!isFunction(onRemoval)) {
154+
throw new Error(ERROR.REMOVAL_FUNC);
155+
}
156+
},
157+
151158
function dismiss({ dismiss }) {
152159
if (!dismiss) return;
153160

0 commit comments

Comments
 (0)