Migrate actions,reducers and action types to TS #15
This commit is contained in:
14
app/reducers/notifications/INotificationAction.ts
Normal file
14
app/reducers/notifications/INotificationAction.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import {NotificationActionType} from 'constants/notificationActionType'
|
||||
|
||||
/**
|
||||
* Notification action interface
|
||||
*
|
||||
* @export
|
||||
* @interface INotificationAction
|
||||
*/
|
||||
export interface INotificationAction {
|
||||
payload: any,
|
||||
type: NotificationActionType
|
||||
|
||||
}
|
||||
|
||||
26
app/reducers/notifications/NotificationState.ts
Normal file
26
app/reducers/notifications/NotificationState.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { Notification } from "domain/notifications";
|
||||
|
||||
/**
|
||||
* Notification state
|
||||
*
|
||||
* @export
|
||||
* @class NotificationState
|
||||
*/
|
||||
export class NotificationState {
|
||||
|
||||
/**
|
||||
* The list of users notification
|
||||
*
|
||||
* @type {({[userId: string]: {[notificationId: string]: Notification}} | null)}
|
||||
* @memberof NotificationState
|
||||
*/
|
||||
userNotifies: {[userId: string]: {[notificationId: string]: Notification}} = {};
|
||||
|
||||
/**
|
||||
* If user notifications are loaded {true} or not {false}
|
||||
*
|
||||
* @type {Boolean}
|
||||
* @memberof NotificationState
|
||||
*/
|
||||
loaded: Boolean = false;
|
||||
}
|
||||
3
app/reducers/notifications/index.ts
Normal file
3
app/reducers/notifications/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { notificationReducer } from "./notificationReducer";
|
||||
|
||||
export {notificationReducer};
|
||||
76
app/reducers/notifications/notificationReducer.ts
Normal file
76
app/reducers/notifications/notificationReducer.ts
Normal file
@@ -0,0 +1,76 @@
|
||||
// - Import react components
|
||||
import moment from 'moment'
|
||||
import _ from 'lodash'
|
||||
|
||||
// - Import domain
|
||||
import { Notification } from "domain/notifications";
|
||||
|
||||
// - Import action types
|
||||
import {NotificationActionType} from 'constants/notificationActionType'
|
||||
|
||||
import { NotificationState } from "./NotificationState";
|
||||
import { INotificationAction } from "./INotificationAction";
|
||||
|
||||
|
||||
/**
|
||||
* Notify actions
|
||||
* @param {object} state
|
||||
* @param {object} action
|
||||
*/
|
||||
export var notificationReducer = (state: NotificationState = new NotificationState(), action: INotificationAction) => {
|
||||
var { payload } = action
|
||||
switch (action.type) {
|
||||
|
||||
/* _____________ CRUD _____________ */
|
||||
case NotificationActionType.ADD_NOTIFY:
|
||||
return state
|
||||
|
||||
case NotificationActionType.ADD_NOTIFY_LIST:
|
||||
return {
|
||||
...state,
|
||||
userNotifies: {
|
||||
...payload
|
||||
},
|
||||
loaded:true
|
||||
}
|
||||
|
||||
case NotificationActionType.SEEN_NOTIFY:
|
||||
return {
|
||||
...state,
|
||||
userNotifies: {
|
||||
...state.userNotifies,
|
||||
[payload]:{
|
||||
...state.userNotifies![payload],
|
||||
isSeen:true
|
||||
}
|
||||
},
|
||||
loaded:true
|
||||
}
|
||||
|
||||
case NotificationActionType.DELETE_NOTIFY:
|
||||
var parsedNotifies = {}
|
||||
Object.keys(state.userNotifies!).map((id) => {
|
||||
if (id !== payload) {
|
||||
_.merge(parsedNotifies, { [id]: { ...state.userNotifies![id] } })
|
||||
}
|
||||
|
||||
})
|
||||
return {
|
||||
...state,
|
||||
userNotifies: {
|
||||
...parsedNotifies
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
case NotificationActionType.CLEAR_ALL_DATA_NOTIFY:
|
||||
return new NotificationState()
|
||||
|
||||
|
||||
default:
|
||||
return state;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user