| 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 |
2x
24x
2x
2x
16x
14x
16x
5x
15x
4x
4x
4x
4x
4x
1x
1x
1x
1x
8x
4x
1x
1x
2x
| import { actionTypes as dataFetchActionTypes } from '@bufferapp/async-data-fetch';
import { actionTypes as profileActionTypes } from '@bufferapp/analyze-profile-selector';
const getFirstProfileForService = (service, profiles) => (
profiles.find(profile => profile.service === service)
);
const SUPPORTED_TABS_BY_SERVICE = {
facebook: ['overview', 'posts', 'audience', 'answers'],
instagram: ['overview', 'posts', 'stories', 'audience', 'answers'],
twitter: ['overview', 'posts', 'answers'],
};
const initialState = {
activeChannel: null,
channels: [],
currentTab: 'overview',
facebookProfile: null,
instagramProfile: null,
twitterProfile: null,
};
function getChannelSupportedTabs(profile) {
if (profile) {
return SUPPORTED_TABS_BY_SERVICE[profile.service];
}
}
function getChannelByProfile(profile) {
return { ...profile, supportedTabs: getChannelSupportedTabs(profile) };
}
function getActiveChannelsFromState(state) {
return [
getChannelByProfile(state.facebookProfile),
getChannelByProfile(state.twitterProfile),
getChannelByProfile(state.instagramProfile),
].filter(e => e.id);
}
function setChannelsAndProfilesOnFetch(profiles) {
const facebookProfile = getFirstProfileForService('facebook', profiles);
const twitterProfile = getFirstProfileForService('twitter', profiles);
const instagramProfile = getFirstProfileForService('instagram', profiles);
const newState = {
facebookProfile,
twitterProfile,
instagramProfile,
};
return { ...newState, channels: getActiveChannelsFromState(newState) };
}
function updateChannelsAndProfiles(profile, state) {
const newState = {
...state,
};
newState[`${profile.service}Profile`] = profile;
newState.channels = getActiveChannelsFromState(newState);
return newState;
}
export default (state = initialState, action) => {
switch (action.type) {
case `profiles_${dataFetchActionTypes.FETCH_SUCCESS}`:
return {
...state,
...setChannelsAndProfilesOnFetch(action.result),
};
case `profiles_${dataFetchActionTypes.FETCH_FAIL}`:
return {
...initialState,
hasError: true,
};
case profileActionTypes.SELECT_PROFILE:
return {
...updateChannelsAndProfiles(action.profile, state),
activeChannel: getChannelByProfile(action.profile),
};
default:
return state;
}
};
|