-
Notifications
You must be signed in to change notification settings - Fork 400
/
Copy pathrecommendations.js
203 lines (175 loc) · 4.88 KB
/
recommendations.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
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
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* @flow */
import invariant from 'invariant';
import { createInternalAddon } from 'amo/reducers/addons';
import { SET_LANG } from 'amo/reducers/api';
import type { AddonType, PartialExternalAddonType } from 'amo/types/addons';
export const ABORT_FETCH_RECOMMENDATIONS: 'ABORT_FETCH_RECOMMENDATIONS' =
'ABORT_FETCH_RECOMMENDATIONS';
export const FETCH_RECOMMENDATIONS: 'FETCH_RECOMMENDATIONS' =
'FETCH_RECOMMENDATIONS';
export const LOAD_RECOMMENDATIONS: 'LOAD_RECOMMENDATIONS' =
'LOAD_RECOMMENDATIONS';
export const OUTCOME_CURATED: 'curated' = 'curated';
export const OUTCOME_RECOMMENDED: 'recommended' = 'recommended';
export const OUTCOME_RECOMMENDED_FALLBACK: 'recommended_fallback' =
'recommended_fallback';
export type FallbackReasonType = 'no_results' | 'timeout' | 'invalid_results';
export type OutcomeType =
| typeof OUTCOME_CURATED
| typeof OUTCOME_RECOMMENDED
| typeof OUTCOME_RECOMMENDED_FALLBACK;
export type Recommendations = {|
addons: Array<AddonType> | null,
fallbackReason: FallbackReasonType | null,
loading: boolean,
outcome: OutcomeType | null,
|};
export type RecommendationsState = {|
byGuid: {
[guid: string]: Recommendations,
},
lang: string,
|};
export const initialState: RecommendationsState = {
byGuid: {},
// We default lang to '' to avoid having to add a lot of invariants to our
// code, and protect against a lang of '' in selectLocalizedContent.
lang: '',
};
export type AbortFetchRecommendationsParams = {|
guid: string,
|};
type AbortFetchRecommendationsAction = {|
type: typeof ABORT_FETCH_RECOMMENDATIONS,
payload: AbortFetchRecommendationsParams,
|};
export const abortFetchRecommendations = ({
guid,
}: AbortFetchRecommendationsParams): AbortFetchRecommendationsAction => {
invariant(guid, 'guid is required');
return {
type: ABORT_FETCH_RECOMMENDATIONS,
payload: { guid },
};
};
type FetchRecommendationsParams = {|
errorHandlerId: string,
guid: string,
recommended?: boolean,
|};
export type FetchRecommendationsAction = {|
type: typeof FETCH_RECOMMENDATIONS,
payload: FetchRecommendationsParams,
|};
export const fetchRecommendations = ({
errorHandlerId,
guid,
recommended = true,
}: FetchRecommendationsParams): FetchRecommendationsAction => {
invariant(errorHandlerId, 'errorHandlerId is required');
invariant(guid, 'guid is required');
return {
type: FETCH_RECOMMENDATIONS,
payload: { errorHandlerId, guid, recommended },
};
};
export type LoadRecommendationsParams = {|
addons: Array<PartialExternalAddonType>,
fallbackReason?: FallbackReasonType,
guid: string,
outcome: OutcomeType,
|};
type LoadRecommendationsAction = {|
type: typeof LOAD_RECOMMENDATIONS,
payload: LoadRecommendationsParams,
|};
export const loadRecommendations = ({
addons,
fallbackReason,
guid,
outcome,
}: LoadRecommendationsParams): LoadRecommendationsAction => {
invariant(addons, 'addons is required');
invariant(guid, 'guid is required');
invariant(outcome, 'outcome is required');
return {
type: LOAD_RECOMMENDATIONS,
payload: { addons, guid, outcome, fallbackReason },
};
};
type GetRecommendationsByGuidParams = {|
guid: string,
state: RecommendationsState,
|};
export const getRecommendationsByGuid = ({
guid,
state,
}: GetRecommendationsByGuidParams): Recommendations | null => {
invariant(guid, 'guid is required');
invariant(state, 'state is required');
return state.byGuid[guid] || null;
};
type Action =
| AbortFetchRecommendationsAction
| FetchRecommendationsAction
| LoadRecommendationsAction;
const reducer = (
// eslint-disable-next-line default-param-last
state: RecommendationsState = initialState,
action: Action,
): RecommendationsState => {
switch (action.type) {
case SET_LANG:
return {
...state,
lang: action.payload.lang,
};
case ABORT_FETCH_RECOMMENDATIONS:
return {
...state,
byGuid: {
...state.byGuid,
[action.payload.guid]: {
addons: null,
fallbackReason: null,
loading: false,
outcome: null,
},
},
};
case FETCH_RECOMMENDATIONS:
return {
...state,
byGuid: {
...state.byGuid,
[action.payload.guid]: {
addons: null,
fallbackReason: null,
loading: true,
outcome: null,
},
},
};
case LOAD_RECOMMENDATIONS: {
const { fallbackReason, guid, outcome } = action.payload;
const addons = action.payload.addons.map((addon) =>
createInternalAddon(addon, state.lang),
);
return {
...state,
byGuid: {
...state.byGuid,
[guid]: {
addons,
fallbackReason,
loading: false,
outcome,
},
},
};
}
default:
return state;
}
};
export default reducer;