Migrate actions,reducers and action types to TS #15
This commit is contained in:
14
app/reducers/votes/IVoteAction.ts
Normal file
14
app/reducers/votes/IVoteAction.ts
Normal file
@@ -0,0 +1,14 @@
|
||||
import {VoteActionType} from 'constants/voteActionType'
|
||||
|
||||
|
||||
/**
|
||||
* Vote action interface
|
||||
*
|
||||
* @export
|
||||
* @interface IVoteAction
|
||||
*/
|
||||
export interface IVoteAction {
|
||||
payload: any,
|
||||
type: VoteActionType
|
||||
|
||||
}
|
||||
26
app/reducers/votes/VoteState.ts
Normal file
26
app/reducers/votes/VoteState.ts
Normal file
@@ -0,0 +1,26 @@
|
||||
import { Vote } from "domain/votes";
|
||||
|
||||
/**
|
||||
* Vote state
|
||||
*
|
||||
* @export
|
||||
* @class VoteState
|
||||
*/
|
||||
export class VoteState {
|
||||
|
||||
/**
|
||||
* The list of posts vote
|
||||
*
|
||||
* @type {({[postId: string]: {[voteId: string]: Vote}} | null)}
|
||||
* @memberof VoteState
|
||||
*/
|
||||
postVotes: {[postId: string]: {[voteId: string]: Vote}} | null = null;
|
||||
|
||||
/**
|
||||
* If posts vote are loaded {true} or not {false}
|
||||
*
|
||||
* @type {Boolean}
|
||||
* @memberof VoteState
|
||||
*/
|
||||
loaded: Boolean = false;
|
||||
}
|
||||
3
app/reducers/votes/index.ts
Normal file
3
app/reducers/votes/index.ts
Normal file
@@ -0,0 +1,3 @@
|
||||
import { voteReducer } from "./voteReducer";
|
||||
|
||||
export {voteReducer};
|
||||
79
app/reducers/votes/voteReducer.ts
Normal file
79
app/reducers/votes/voteReducer.ts
Normal file
@@ -0,0 +1,79 @@
|
||||
// - Import react components
|
||||
import moment from 'moment'
|
||||
import _ from 'lodash'
|
||||
|
||||
// - Import action types
|
||||
import {VoteActionType} from 'constants/voteActionType'
|
||||
|
||||
// Import domain
|
||||
import { Vote } from "domain/votes";
|
||||
|
||||
|
||||
import { VoteState } from "./VoteState";
|
||||
import { IVoteAction } from "./IVoteAction";
|
||||
|
||||
|
||||
/**
|
||||
* Vote actions
|
||||
* @param {object} state
|
||||
* @param {object} action
|
||||
*/
|
||||
export var voteReducer = (state: VoteState = new VoteState(), action: IVoteAction) => {
|
||||
var { payload } = action
|
||||
switch (action.type) {
|
||||
|
||||
/* _____________ CRUD _____________ */
|
||||
case VoteActionType.ADD_VOTE:
|
||||
return {
|
||||
...state,
|
||||
postVotes: {
|
||||
...state.postVotes,
|
||||
[payload.postId]: {
|
||||
...state.postVotes![payload.postId],
|
||||
[payload.id]: {
|
||||
...payload
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
case VoteActionType.ADD_VOTE_LIST:
|
||||
return {
|
||||
...state,
|
||||
postVotes: {
|
||||
...payload
|
||||
},
|
||||
loaded:true
|
||||
}
|
||||
|
||||
case VoteActionType.DELETE_VOTE:
|
||||
var parsedVotes = {}
|
||||
if (state.postVotes![payload.postId])
|
||||
Object.keys(state.postVotes![payload.postId]).map((id) => {
|
||||
if (id !== payload.id) {
|
||||
_.merge(parsedVotes, { [id]: { ...state.postVotes![payload.postId][id] } })
|
||||
}
|
||||
|
||||
})
|
||||
return {
|
||||
...state,
|
||||
postVotes: {
|
||||
...state.postVotes,
|
||||
[payload.postId]: {
|
||||
...parsedVotes
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
case VoteActionType.CLEAR_ALL_DATA_VOTE:
|
||||
return new VoteState();
|
||||
|
||||
|
||||
default:
|
||||
return state;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user