Skip to content

Instantly share code, notes, and snippets.

@nhunsaker
Created June 27, 2018 18:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nhunsaker/f6fd3c18dbeaee9bdf3ff39acadc4c85 to your computer and use it in GitHub Desktop.
Save nhunsaker/f6fd3c18dbeaee9bdf3ff39acadc4c85 to your computer and use it in GitHub Desktop.
remote/index.js
import { connect } from 'react-redux';
import RemoteView from './view';
import { HAS_READ_WRITE_PERMISSION } from '../../lib/permissions';
import { PH_AUTH_URL, IS_DEV_OR_STAGE } from '../../lib/env_vars';
import { Cookies } from 'react-cookie';
import {
receiveFacebookLogin,
receiveFacebookLogout,
receiveFacebookAccounts,
receiveFacebookPermissions,
} from '../../actions/facebook';
import { setFacebookAccounts } from '../../actions/draft';
import { setFacebookChannels } from '../../actions/channels';
import { clearToast } from '../../actions/toasts';
import { get } from 'lodash';
function mapStateToProps(state) {
const { draft } = state.draft;
const { notification } = state.toasts;
const { channels, userChannels, isUserChannelsLoading } = state.channels;
const { authentication } = state.facebook;
const {
isInstagramValid,
isFilesizeValid,
isFileTypeValid,
isValidDomain,
} = state.remote;
const { query } = state.routing.locationBeforeTransitions;
let isOrganic = query && Object.keys(query).includes('organic') && query.organic === 'true';
let hasSidebar = query && Object.keys(query).includes('sidebar') && query.sidebar === 'true';
const isFacebook = true;
const permissionsRevoked = (!!authentication.facebook.authResponse.accessToken
&& !authentication.facebook.permissions && isFacebook);
const needsAuthentication = (!authentication.facebook.authResponse.accessToken
&& isFacebook);
const accountRevoked = false;
const cookies = new Cookies();
const hasAutoLogin = (cookies.get(`pubhub_${window.GLOBAL.fbAppId}`, { path: '/' })) === '1';
const selectedDraftId = (draft) ? draft.uuid : null;
const media = get(query, ['media']);
const isPlatformSelect = ((['image', 'photo'].includes(media)) && IS_DEV_OR_STAGE);
const hasFacebookAccounts = (authentication.facebook.accounts.length > 0);
const hasInstagramAccounts = !!userChannels.find((channel) => channel.platform === 'instagram');
return {
isPlatformSelect,
isDraftDetailVisible: (!!draft),
authentication,
channels,
permissionsRevoked,
needsAuthentication,
accountRevoked,
hasReadWritePermissions: HAS_READ_WRITE_PERMISSION,
authenticationLoaded: authentication.facebook.loaded,
hasAutoLogin,
hasFacebookAccounts,
hasInstagramAccounts:true,
draft,
notification,
isOrganic,
selectedDraftId,
hasSidebar,
isUserChannelsLoading,
isInstagramValid,
isFilesizeValid,
isFileTypeValid,
isValidDomain,
};
}
function mapDispatchToProps(dispatch) {
return {
onFacebookLogin: (e) => dispatch(receiveFacebookLogin(e)),
onFacebookLogout: (e) => dispatch(receiveFacebookLogout(e)),
onFacebookAccounts: (e) => dispatch(receiveFacebookAccounts(e)),
onFacebookPermissions: (e) => dispatch(receiveFacebookPermissions(e)),
onClearToast: (e = {}, types = []) => dispatch(clearToast(e, types)),
onDraftFacebookAccounts: (e) => dispatch(setFacebookAccounts(e)),
onChannelFacebookAccounts: (e) => dispatch(setFacebookChannels(e)),
onSetType: (platform, before, after) => {
window.location = window.location.href.replace(`&media=${before}`, `&type=${platform}_${after}`);
},
onOpenAuth: (platform, before, after) => {
const baseUrl = `${PH_AUTH_URL}/auth/${platform}`;
const finalDestination = window.location.href.replace(`&media=${before}`, `&type=${platform}_${after}`);
window.location = `${baseUrl}?final_destination=${encodeURIComponent(finalDestination)}`;
},
};
}
export default connect(mapStateToProps, mapDispatchToProps)(RemoteView);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment