Newer
Older
import {
get as apiGet,
create,
remove,
update,
} from 'pubsweet-client/src/helpers/api'
const REQUEST = 'recommendations/REQUEST'
const ERROR = 'recommendations/ERROR'
const GET_RECOMMENDATIONS_SUCCESS = 'recommendations/GET_SUCCESS'
const GET_RECOMMENDATION_SUCCESS = 'recommendations/GET_ITEM_SUCCESS'
const CREATE_RECOMMENDATION_SUCCESS = 'recommendations/CREATE_SUCCESS'
const UPDATE_RECOMMENDATION_SUCCESS = 'recommendations/UPDATE_SUCCESS'
export const recommendationsRequest = () => ({
type: REQUEST,
})
export const recommendationsError = error => ({
type: ERROR,
error,
})
export const getRecommendationsSuccess = recommendations => ({
type: GET_RECOMMENDATIONS_SUCCESS,
payload: { recommendations },
})
export const getRecommendationSuccess = recommendation => ({
type: GET_RECOMMENDATION_SUCCESS,
payload: { recommendation },
})
export const createRecommendationSuccess = recommendation => ({
type: CREATE_RECOMMENDATION_SUCCESS,
payload: { recommendation },
})
export const updateRecommendationSuccess = recommendation => ({
type: UPDATE_RECOMMENDATION_SUCCESS,
payload: { recommendation },
})
// Selectors
export const selectFetching = state =>
get(state, 'recommendations.fetching') || false
export const selectError = state => get(state, 'recommendations.error')
export const selectRecommendations = state =>
get(state, 'recommendations.recommendations') || []
const sleep = ms => new Promise(resolve => setTimeout(resolve, ms))
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
export const getFragmentRecommendations = (
collectionId,
fragmentId,
) => dispatch => {
dispatch(recommendationsRequest())
const review = {
id: 'revuewiuuuid',
userId: 'uuuuuuid',
recommendation: 'publish',
recommendationType: 'review',
comments: [
{
content: 'Here is public text',
public: true,
files: [],
},
{
content: 'Here is PRIVATE text',
public: false,
files: [],
},
],
}
return sleep(1000).then(() => {
dispatch(getRecommendationsSuccess([review]))
return [review]
})
// return apiGet(
// `/collections/${collectionId}/fragments/${fragmentId}/recommendations`,
// ).then(
// r => dispatch(getRecommendationsSuccess(r)),
// err => dispatch(recommendationsError(err)),
// )
}
export const updateRecommendation = recommendation => dispatch => {
dispatch(recommendationsRequest())
return sleep(1000).then(() => {
dispatch(updateRecommendationSuccess(recommendation))
return recommendation
})
}
export const createRecommendation = recommendation => dispatch => {
dispatch(recommendationsRequest())
return sleep(1000).then(() => {
dispatch(updateRecommendationSuccess(recommendation))
return recommendation
})
}
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// State
const initialState = {
fetching: false,
error: null,
recommendations: [],
recommendation: {},
}
export default (state = initialState, action = {}) => {
switch (action.type) {
case REQUEST:
return {
...state,
fetching: true,
recommendations: [],
recommendation: {},
}
case ERROR:
return {
...state,
fetching: false,
error: action.error,
}
case GET_RECOMMENDATIONS_SUCCESS:
return {
...state,
fetching: false,
error: null,
recommendations: action.payload.recommendations,
}
case GET_RECOMMENDATION_SUCCESS:
return {
...state,
fetching: false,
error: null,
recommendation: action.payload.recommendation,
}
case UPDATE_RECOMMENDATION_SUCCESS:
case CREATE_RECOMMENDATION_SUCCESS:
return {
...state,
fetching: false,
error: null,
recommendations: [action.payload.recommendation],
recommendation: action.payload.recommendation,
}
default:
return state
}
}