diff --git a/src/js/actions/FacebookActionCreators.js b/src/js/actions/FacebookActionCreators.js
deleted file mode 100644
index 08b0547c5..000000000
--- a/src/js/actions/FacebookActionCreators.js
+++ /dev/null
@@ -1,104 +0,0 @@
-const web_app_config = require("../config");
-import FacebookDispatcher from "../dispatcher/FacebookDispatcher";
-import VoterActions from "../actions/VoterActions";
-import FacebookConstants from "../constants/FacebookConstants";
-const cookies = require("../utils/cookies");
-
-const FacebookActionCreators = {
-
- appLogout: function (){
- cookies.removeItem("voter_device_id");
- VoterActions.signOut();
- VoterActions.retrieveVoter();
- },
-
- initFacebook: function () {
- window.fbAsyncInit = function () {
- window.FB.init({
- appId: web_app_config.FACEBOOK_APP_ID,
- xfbml: true,
- version: "v2.5"
- });
-
- // after initialization, get the login status
- FacebookActionCreators.getLoginStatus();
- },
- (function (d, s, id){
- var js, fjs = d.getElementsByTagName(s)[0];
- if (d.getElementById(id)) {return;}
- js = d.createElement(s); js.id = id;
- js.src = "//connect.facebook.net/en_US/sdk.js";
- fjs.parentNode.insertBefore(js, fjs);
- }(document, "script", "facebook-jssdk"));
- },
-
- getLoginStatus: function () {
- window.FB.getLoginStatus((response) => {
- FacebookDispatcher.dispatch({
- actionType: FacebookConstants.FACEBOOK_INITIALIZED,
- data: response
- });
- });
- },
-
- login: () => {
- try {
- window.FB.login((response) => {
- if (response.status === "connected") {
- // Logged into We Vote and Facebook
- FacebookDispatcher.dispatch({
- actionType: FacebookConstants.FACEBOOK_LOGGED_IN,
- data: response
- });
- } else if (response.status === "not_authorized") {
- // The person is logged into Facebook, but not We Vote
- } else {
- // The person is not logged into Facebook
- }
- });
- } catch (e) {
- // If FB already logged in, carry on
- }
- },
-
- logout: () => {
- window.FB.logout((response) => {
- FacebookDispatcher.dispatch({
- actionType: FacebookConstants.FACEBOOK_LOGGED_OUT,
- data: response
- });
- });
- },
-
- // Dale considering the need for this here
- //connectWithFacebook: () => {
- // // Add connection between We Vote and Facebook
- // FacebookDispatcher.dispatch({
- // actionType: FacebookConstants.FACEBOOK_SIGN_IN_CONNECT,
- // data: true
- // });
- //},
-
- disconnectFromFacebook: () => {
- // Removing connection between We Vote and Facebook
- FacebookDispatcher.dispatch({
- actionType: FacebookConstants.FACEBOOK_SIGN_IN_DISCONNECT,
- data: true
- });
- },
-
- getFacebookProfilePicture: (userId) => {
- FacebookDispatcher.dispatch({
- actionType: FacebookConstants.FACEBOOK_GETTING_PICTURE,
- data: null
- });
- window.FB.api(`/${userId}/picture?type=large`, (response) => {
- FacebookDispatcher.dispatch({
- actionType: FacebookConstants.FACEBOOK_RECEIVED_PICTURE,
- data: response
- });
- });
- }
-};
-
-module.exports = FacebookActionCreators;
diff --git a/src/js/actions/FacebookActions.js b/src/js/actions/FacebookActions.js
new file mode 100644
index 000000000..921e38578
--- /dev/null
+++ b/src/js/actions/FacebookActions.js
@@ -0,0 +1,100 @@
+const web_app_config = require("../config");
+import Dispatcher from "../dispatcher/Dispatcher";
+import VoterActions from "../actions/VoterActions";
+import FacebookConstants from "../constants/FacebookConstants";
+const cookies = require("../utils/cookies");
+
+module.exports = {
+
+ initFacebook: function () {
+ window.fbAsyncInit = function () {
+ window.FB.init({
+ appId: web_app_config.FACEBOOK_APP_ID,
+ xfbml: true,
+ version: "v2.5"
+ });
+ };
+
+ (function (d, s, id){
+ var js, fjs = d.getElementsByTagName(s)[0];
+ if (d.getElementById(id)) {return;}
+ js = d.createElement(s); js.id = id;
+ js.src = "//connect.facebook.net/en_US/sdk.js";
+ fjs.parentNode.insertBefore(js, fjs);
+ }(document, "script", "facebook-jssdk"));
+ },
+
+ facebookSignIn: function (facebook_id, facebook_email){
+ Dispatcher.loadEndpoint("facebookSignIn", {
+ facebook_id: facebook_id,
+ facebook_email: facebook_email
+ });
+ },
+
+ facebookDisconnect: function (){
+ Dispatcher.loadEndpoint("facebookDisconnect");
+ },
+
+ appLogout: function (){
+ cookies.setItem("voter_device_id", "", -1, "/");
+ VoterActions.signOut();
+ VoterActions.retrieveVoter();
+ },
+
+ login: function () {
+ window.FB.getLoginStatus(function (response) {
+ if (response.status === "connected") {
+ Dispatcher.dispatch({
+ type: FacebookConstants.FACEBOOK_LOGGED_IN,
+ data: response
+ });
+ } else {
+ window.FB.login( (res) =>{
+ Dispatcher.dispatch({
+ type: FacebookConstants.FACEBOOK_LOGGED_IN,
+ data: res
+ });
+ }, {scope: "public_profile,email"});
+ }
+ });
+ },
+
+ logout: function () {
+ window.FB.logout((response) => {
+ Dispatcher.dispatch({
+ type: FacebookConstants.FACEBOOK_LOGGED_OUT,
+ data: response
+ });
+ });
+ },
+
+ disconnectFromFacebook: function () {
+ // Removing connection between We Vote and Facebook
+ Dispatcher.dispatch({
+ type: FacebookConstants.FACEBOOK_SIGN_IN_DISCONNECT,
+ data: true
+ });
+ },
+
+ getFacebookProfilePicture: function (userId) {
+ window.FB.api(`/${userId}/picture?type=large`, (response) => {
+ Dispatcher.dispatch({
+ type: FacebookConstants.FACEBOOK_RECEIVED_PICTURE,
+ data: response
+ });
+ });
+ },
+
+ getFacebookEmail: function (){
+ window.FB.api("/me?fields=id,email", (response) => {
+ Dispatcher.dispatch({
+ type: FacebookConstants.FACEBOOK_RECEIVED_EMAIL,
+ data: response
+ });
+ });
+ },
+
+ savePhoto: function (url){
+ Dispatcher.loadEndpoint("voterPhotoSave", { facebook_profile_image_url_https: url } );
+ },
+};
diff --git a/src/js/actions/VoterActions.js b/src/js/actions/VoterActions.js
index c829179e3..e8818b4c1 100644
--- a/src/js/actions/VoterActions.js
+++ b/src/js/actions/VoterActions.js
@@ -3,7 +3,7 @@ import Dispatcher from "../dispatcher/Dispatcher";
module.exports = {
signOut: function (){
- Dispatcher.loadEndpoint("voterSignOut", {sign_out_all_devices: true});
+ Dispatcher.loadEndpoint("voterSignOut", {sign_out_all_devices: false});
},
retrieveVoter: function () {
diff --git a/src/js/components/Facebook/FacebookDisconnect.jsx b/src/js/components/Facebook/FacebookDisconnect.jsx
index 2229541b7..9d02c431c 100644
--- a/src/js/components/Facebook/FacebookDisconnect.jsx
+++ b/src/js/components/Facebook/FacebookDisconnect.jsx
@@ -1,5 +1,5 @@
import React from "react";
-import FacebookActionCreators from "../../actions/FacebookActionCreators";
+import FacebookActions from "../../actions/FacebookActions";
class FacebookDisconnect extends React.Component {
constructor (props) {
@@ -8,7 +8,7 @@ class FacebookDisconnect extends React.Component {
didClickDisconnectFromFacebookButton (e) {
console.log("didClickDisconnectFromFacebookButton");
- FacebookActionCreators.disconnectFromFacebook();
+ FacebookActions.disconnectFromFacebook();
}
render () {
diff --git a/src/js/components/Facebook/FacebookDownloadPicture.jsx b/src/js/components/Facebook/FacebookDownloadPicture.jsx
index 012e44f6c..4c1c47930 100644
--- a/src/js/components/Facebook/FacebookDownloadPicture.jsx
+++ b/src/js/components/Facebook/FacebookDownloadPicture.jsx
@@ -1,6 +1,6 @@
import React, { PropTypes } from "react";
-import FacebookActionCreators from "../../actions/FacebookActionCreators";
+import FacebookActions from "../../actions/FacebookActions";
class FacebookDownloadPicture extends React.Component {
static propTypes = {
@@ -19,7 +19,7 @@ class FacebookDownloadPicture extends React.Component {
}
didClickDownloadPicture () {
- FacebookActionCreators.getFacebookProfilePicture(this.props.userId);
+ FacebookActions.getFacebookProfilePicture(this.props.userId);
}
}
diff --git a/src/js/components/Facebook/FacebookLogin.jsx b/src/js/components/Facebook/FacebookLogin.jsx
index 38c73c158..2a1198451 100644
--- a/src/js/components/Facebook/FacebookLogin.jsx
+++ b/src/js/components/Facebook/FacebookLogin.jsx
@@ -1,5 +1,5 @@
import React from "react";
-import FacebookActionCreators from "../../actions/FacebookActionCreators";
+import FacebookActions from "../../actions/FacebookActions";
class FacebookLogin extends React.Component {
constructor (props) {
@@ -10,7 +10,7 @@ class FacebookLogin extends React.Component {
}
didClickFacebookLoginButton (e) {
- FacebookActionCreators.login();
+ FacebookActions.login();
}
}
diff --git a/src/js/components/Facebook/FacebookLogout.jsx b/src/js/components/Facebook/FacebookLogout.jsx
index f8ab2bb00..f54af3fa2 100644
--- a/src/js/components/Facebook/FacebookLogout.jsx
+++ b/src/js/components/Facebook/FacebookLogout.jsx
@@ -1,5 +1,5 @@
import React from "react";
-import FacebookActionCreators from "../../actions/FacebookActionCreators";
+import FacebookActions from "../../actions/FacebookActions";
class FacebookLogout extends React.Component {
constructor (props) {
@@ -10,7 +10,7 @@ class FacebookLogout extends React.Component {
}
didClickFacebookLogoutButton (e) {
- FacebookActionCreators.logout();
+ FacebookActions.logout();
}
}
diff --git a/src/js/components/Facebook/FacebookPicture.jsx b/src/js/components/Facebook/FacebookPicture.jsx
index 2e8e31021..d0e434105 100644
--- a/src/js/components/Facebook/FacebookPicture.jsx
+++ b/src/js/components/Facebook/FacebookPicture.jsx
@@ -4,9 +4,6 @@ import FacebookConstants from "../../constants/FacebookConstants";
export default class FacebookPicture extends React.Component {
static propTypes = {
- history: PropTypes.object,
- children: PropTypes.object,
- params: PropTypes.object.isRequired,
facebookPictureStatus: PropTypes.string,
facebookPictureUrl: PropTypes.string
};
diff --git a/src/js/components/Facebook/FacebookSignIn.jsx b/src/js/components/Facebook/FacebookSignIn.jsx
index 468f31254..2e0273bb4 100644
--- a/src/js/components/Facebook/FacebookSignIn.jsx
+++ b/src/js/components/Facebook/FacebookSignIn.jsx
@@ -1,5 +1,5 @@
import React from "react";
-import FacebookActionCreators from "../../actions/FacebookActionCreators";
+import FacebookActions from "../../actions/FacebookActions";
class FacebookSignIn extends React.Component {
constructor (props) {
@@ -12,8 +12,7 @@ class FacebookSignIn extends React.Component {
}
didClickFacebookLoginButton () {
- console.log("didClickFacebookLoginButton");
- FacebookActionCreators.login(); // We call FacebookActionCreators.connectWithFacebook() within login()
+ FacebookActions.login();
}
}
diff --git a/src/js/components/Facebook/Main.js b/src/js/components/Facebook/Main.js
index 16531f87d..6f95ae82f 100644
--- a/src/js/components/Facebook/Main.js
+++ b/src/js/components/Facebook/Main.js
@@ -1,15 +1,13 @@
import React from "react";
-
-import FacebookActionCreators from "../../actions/FacebookActionCreators";
+import FacebookActions from "../../actions/FacebookActions";
import FacebookStore from "../../stores/FacebookStore";
-import FacebookLogin from "../../components/Facebook/FacebookLogin";
-import FacebookLogout from "../../components/Facebook/FacebookLogout";
import FacebookDownloadPicture from "../../components/Facebook/FacebookDownloadPicture";
import FacebookPicture from "../../components/Facebook/FacebookPicture";
+import VoterStore from "../../stores/VoterStore";
class Main extends React.Component {
constructor (props) {
- super();
+ super(props);
this.state = this.getFacebookState();
}
@@ -19,33 +17,34 @@ class Main extends React.Component {
loggedIn: FacebookStore.loggedIn,
userId: FacebookStore.userId,
facebookPictureStatus: FacebookStore.facebookPictureStatus,
- facebookPictureUrl: FacebookStore.facebookPictureUrl
+ facebookPictureUrl: VoterStore.getPhoto()
};
}
componentDidMount () {
- FacebookActionCreators.initFacebook();
- this.changeListener = this._onFacebookChange.bind(this);
- FacebookStore.addChangeListener(this.changeListener);
+ FacebookActions.initFacebook();
+ this.listener = FacebookStore.addListener(this._onChange.bind(this));
+ this.voterListener = VoterStore.addListener(this._onChange.bind(this));
}
componentWillUnmount () {
- FacebookStore.removeChangeListener(this.changeListener);
- }
+ this.listener.remove();
+ this.voterListener.remove();
+ }
- _onFacebookChange () {
+ _onChange () {
this.setState(this.getFacebookState());
}
-
render () {
return
- {!this.state.loggedIn ?
: null}
- {this.state.loggedIn ?
: null}
Facebook logged in: {this.state.loggedIn ? "true" : "false"}
Facebook access token: {this.state.accessToken}
User ID is: {this.state.userId}
- {this.state.userId ?
: null}
+ {this.state.userId ?
+
:
+
+ }
diff --git a/src/js/components/Header.jsx b/src/js/components/Header.jsx
index 1bfb3f23f..8d64a8f18 100644
--- a/src/js/components/Header.jsx
+++ b/src/js/components/Header.jsx
@@ -1,7 +1,7 @@
import React, { Component, PropTypes } from "react";
import { Link } from "react-router";
import HeaderIcons from "./Navigation/HeaderIcons";
-import FacebookActionCreators from "../actions/FacebookActionCreators";
+import FacebookActions from "../actions/FacebookActions";
export default class Header extends Component {
static propTypes = {
@@ -48,7 +48,7 @@ export default class Header extends Component {
var { visible } = this.state;
let location = this.props.location;
var { signed_in_personal } = this.props.voter;
- const logOut = FacebookActionCreators.appLogout;
+ const logOut = FacebookActions.appLogout;
const header =
diff --git a/src/js/components/MoreMenu.jsx b/src/js/components/MoreMenu.jsx
index 373eb8408..ffd4035e3 100644
--- a/src/js/components/MoreMenu.jsx
+++ b/src/js/components/MoreMenu.jsx
@@ -1,6 +1,6 @@
import React, { PropTypes, Component } from "react";
import { Link } from "react-router";
-import FacebookActionCreators from "../actions/FacebookActionCreators";
+import FacebookActions from "../actions/FacebookActions";
export default class MoreMenu extends Component {
static propTypes = {
@@ -20,7 +20,7 @@ export default class MoreMenu extends Component {
}
render () {
- const logOut = FacebookActionCreators.appLogout;
+ const logOut = FacebookActions.appLogout;
return
diff --git a/src/js/constants/FacebookConstants.js b/src/js/constants/FacebookConstants.js
index cccffa26b..5aa0bbccf 100644
--- a/src/js/constants/FacebookConstants.js
+++ b/src/js/constants/FacebookConstants.js
@@ -7,6 +7,7 @@ const FacebookConstants = {
FACEBOOK_RECEIVED_PICTURE: null,
FACEBOOK_LOGGED_IN: null,
FACEBOOK_LOGGED_OUT: null,
+ FACEBOOK_RECEIVED_EMAIL: null,
//FACEBOOK_SIGN_IN_CONNECT: null, // Dale exploring need for this
FACEBOOK_SIGN_IN_DISCONNECT: null,
IMAGE_UPLOADED: null,
diff --git a/src/js/routes/More/SignIn.jsx b/src/js/routes/More/SignIn.jsx
index fe5db2a3d..f93314d0d 100755
--- a/src/js/routes/More/SignIn.jsx
+++ b/src/js/routes/More/SignIn.jsx
@@ -1,5 +1,5 @@
import React, { Component } from "react";
-import FacebookActionCreators from "../../actions/FacebookActionCreators";
+import FacebookActions from "../../actions/FacebookActions";
import FacebookStore from "../../stores/FacebookStore";
import FacebookSignIn from "../../components/Facebook/FacebookSignIn";
import Main from "../../components/Facebook/Main";
@@ -15,15 +15,14 @@ export default class SignIn extends Component {
componentDidMount () {
this._onVoterStoreChange();
- FacebookActionCreators.initFacebook();
- this.changeListener = this._onFacebookChange.bind(this);
- FacebookStore.addChangeListener(this.changeListener);
+ FacebookActions.initFacebook();
+ this.facebookListener = FacebookStore.addListener(this._onFacebookChange.bind(this));
this.listener = VoterStore.addListener(this._onVoterStoreChange.bind(this));
}
componentWillUnmount () {
this.listener.remove();
- FacebookStore.removeChangeListener(this.changeListener);
+ this.facebookListener.remove();
}
_onVoterStoreChange () {
@@ -45,7 +44,6 @@ export default class SignIn extends Component {
}
render () {
- console.log(this.state);
var { voter} = this.state;
if (!voter){
return LoadingWheel;
@@ -56,7 +54,7 @@ export default class SignIn extends Component {
{voter.signed_in_personal ? My Account : Sign In}
{voter.signed_in_facebook ?
-
+
Sign Out :
}
{/*
diff --git a/src/js/stores/FacebookStore.js b/src/js/stores/FacebookStore.js
index e1405d781..ce80deaa0 100644
--- a/src/js/stores/FacebookStore.js
+++ b/src/js/stores/FacebookStore.js
@@ -1,236 +1,92 @@
-import { $ajax } from "../utils/service";
+var FluxMapStore = require("flux/lib/FluxMapStore");
import FacebookConstants from "../constants/FacebookConstants";
-import FacebookDispatcher from "../dispatcher/FacebookDispatcher";
-import VoterStore from "../stores/VoterStore";
+import FacebookActions from "../actions/FacebookActions";
+import Dispatcher from "../dispatcher/Dispatcher";
import VoterActions from "../actions/VoterActions";
-import {EventEmitter} from "events";
-import service from "../utils/service";
-
-const cookies = require("../utils/cookies");
-const FACEBOOK_CHANGE_EVENT = "FACEBOOK_CHANGE_EVENT";
-
-const FacebookAPIWorker = {
- voterFacebookPhotoSave: function (photo_url, success ) {
- console.log("FacebookAPIWorker.voterFacebookPhotoSave");
- return service.get({
- endpoint: "voterPhotoSave",
- query: {
- voter_device_id: cookies.getItem("voter_device_id"),
- facebook_profile_image_url_https: photo_url
- }, success
- });
- },
-
- facebookSignIn: function (facebook_id, facebook_email, callback) {
- // console.log("In FacebookStore.js, FacebookAPIWorker.facebookSignIn, facebook_id: ", facebook_id);
- return $ajax({
- type: "GET",
- endpoint: "facebookSignIn",
- data: {
- facebook_id: facebook_id,
- facebook_email: facebook_email
- },
- success: (result) => {
- callback(result);
- },
- error: (err) => {
- callback(err);
- }
- });
- },
-
- /**
- * Disconnect facebook from this account by removing the facebook_id from the db
- * @param {String} voter_device_id will be passed
- * @return {Boolean} Was the disconnection successful?
- */
- facebookDisconnect: function (callback) {
- // console.log("In FacebookStore.js, FacebookAPIWorker.facebookSignIn");
- if (callback instanceof Function === false) throw new Error("facebookDisconnect, missing callback function");
-
- $ajax({
- type: "GET",
- endpoint: "facebookDisconnect",
- success: (response) => {
- callback(response);
- },
- error: (err) => callback(err)
- });
+class FacebookStore extends FluxMapStore {
+ getInitialState (){
+ return {
+ authData: {},
+ emailData: {}
+ };
}
-};
-
-class FacebookStore extends EventEmitter {
- constructor () {
- super();
- this.facebookAuthData = {};
- this.faebookPictureData = {};
- }
-
- setFacebookAuthData (data) {
- this.facebookAuthData = data;
- this.emitChange();
- }
-
- get loggedIn () {
- if (!this.facebookAuthData) {
- return;
- }
- return this.facebookAuthData.status === "connected";
- }
-
- get userId () {
- if (!this.facebookAuthData || !this.facebookAuthData.authResponse) {
- return;
- }
-
- return this.facebookAuthData.authResponse.userID;
- }
-
- get accessToken () {
- if (!this.facebookAuthData || !this.facebookAuthData.authResponse) {
- return;
- }
+ get facebookAuthData (){
+ return this.getState().authData;
+ }
- return this.facebookAuthData.authResponse.accessToken;
- }
+ get facebookEmailData (){
+ return this.getState().emailData;
+ }
- get facebookPictureUrl () {
- if (!this.facebookPictureData || !this.facebookPictureData.url) {
- return;
- }
+ get loggedIn () {
+ if (!this.facebookAuthData) {
+ return undefined;
+ }
- return this.facebookPictureData.url;
- }
+ return this.facebookAuthData.status === "connected";
+ }
- setFacebookPictureData (type, data) {
- this.facebookPictureStatus = type;
+ get userId () {
+ if (!this.facebookAuthData || !this.facebookAuthData.authResponse) {
+ return undefined;
+ }
- if (data) {
- this.facebookPictureData = data.data;
- } else {
- this.facebookPictureData = {};
- }
+ return this.facebookAuthData.authResponse.userID;
+ }
- this.emitChange();
- }
+ get accessToken () {
+ if (!this.facebookAuthData || !this.facebookAuthData.authResponse) {
+ return undefined;
+ }
- saveFacebookPictureData (data) {
- if (data) {
- FacebookAPIWorker
- .voterFacebookPhotoSave(
- data.data.url, () => this.emit(FACEBOOK_CHANGE_EVENT)
- );
- }
- }
+ return this.facebookAuthData.authResponse.accessToken;
+ }
- saveFacebookAuthData () {
- if (this.facebookAuthData) {
- console.log("In FacebookStore.js, saveFacebookAuthData: ", this.facebookAuthData);
- console.log("userID: ", this.facebookAuthData.authResponse.userID);
- FacebookAPIWorker
- .facebookSignIn(
- this.facebookAuthData.authResponse.userID, false, () => this.emit(FACEBOOK_CHANGE_EVENT)
- );
+ reduce (state, action) {
+ switch (action.type) {
+
+ case "facebookSignIn":
+ // Once we have connected to Facebook, grab a fresh version of the voter
+ VoterActions.retrieveVoter();
+ return state;
+
+ case FacebookConstants.FACEBOOK_LOGGED_IN:
+ FacebookActions.getFacebookEmail();
+ return {
+ ...state,
+ authData: action.data
+ };
+
+ case FacebookConstants.FACEBOOK_RECEIVED_EMAIL:
+ //TODO: this is hackish to save the email by using facebookSignIn because will cause re-fetching voter.
+ //swap out with updateVoter endpoint once created.
+ FacebookActions.facebookSignIn(this.facebookAuthData.authResponse.userID, action.data.email);
+ return {
+ ...state,
+ emailData: action.data
+ };
+
+ case "voterSignOut":
+ return {
+ authData: {},
+ pictureData: {},
+ emailData: {}
+ };
+
+ case FacebookConstants.FACEBOOK_SIGN_IN_DISCONNECT:
+ this.disconnectFromFacebook();
+ return state;
+
+ case FacebookConstants.FACEBOOK_RECEIVED_PICTURE:
+ FacebookActions.savePhoto(action.data.data.url);
+ return state;
+
+ default:
+ return state;
}
- }
-
- connectWithFacebook () {
- if (this.facebookAuthData) {
- // console.log("In FacebookStore.js, connectWithFacebook, this.facebookAuthData: ", this.facebookAuthData);
- // console.log("userID: ", this.facebookAuthData.authResponse.userID);
- FacebookAPIWorker
- .facebookSignIn(this.facebookAuthData.authResponse.userID, false, () => {
- // console.log("Call to FacebookAPIWorker.facebookSignIn has completed");
- this.emit(FACEBOOK_CHANGE_EVENT);
- // Once we have connected to Facebook, grab a fresh version of the voter
- VoterActions.retrieveVoter();
- }
- );
}
}
- disconnectFromFacebook () {
- FacebookAPIWorker
- .facebookDisconnect(
- () => {
- // console.log("FacebookAPIWorker.facebookDisconnect has completed");
- this.emit(FACEBOOK_CHANGE_EVENT);
- // Once we have disconnected from Facebook, grab a fresh version of the voter
- VoterStore.getLocation( (err) => {
- if (err) handleVoterError(err);
- VoterStore.retrieveFreshVoterObject( (_err, voter_object) => {
- if (_err) {
- handleVoterError(_err);
- } else {
- // console.log("facebookStore.dispatchToken: FACEBOOK_SIGN_IN_DISCONNECT, voter: ", voter_object);
- // Finally, update all components listening for changes in Voter Store
- VoterStore.emitChange();
- }
- });
- });
- }
- );
- }
-
- emitChange () {
- this.emit(FACEBOOK_CHANGE_EVENT);
- }
-
- addChangeListener (callback) {
- this.on(FACEBOOK_CHANGE_EVENT, callback);
- }
-
- removeChangeListener (callback) {
- this.removeListener(FACEBOOK_CHANGE_EVENT, callback);
- }
-}
-
-function handleVoterError (err) {
- console.error("FacebookStore.js, Error initializing voter object", err);
-}
-
-// initialize the store as a singleton
-const facebookStore = new FacebookStore();
-
-facebookStore.dispatchToken = FacebookDispatcher.register((action) => {
- if (action.actionType === FacebookConstants.FACEBOOK_INITIALIZED) {
- facebookStore.setFacebookAuthData(action.data);
- }
-
- if (action.actionType === FacebookConstants.FACEBOOK_LOGGED_IN) {
- // console.log("facebookStore, actionType: FACEBOOK_LOGGED_IN, action.data: ", action.data);
- facebookStore.setFacebookAuthData(action.data); // TODO set this up so following functions are dependent
- facebookStore.connectWithFacebook();
- }
-
- if (action.actionType === FacebookConstants.FACEBOOK_LOGGED_OUT) {
- facebookStore.setFacebookAuthData(action.data);
- }
-
- //if (action.actionType === FacebookConstants.FACEBOOK_SIGN_IN_CONNECT) {
- // // Dale exploring need for this vs. 'FACEBOOK_LOGGED_IN'?
- // console.log("facebookStore.dispatchToken: FACEBOOK_SIGN_IN_CONNECT");
- // facebookStore.connectWithFacebook();
- //}
-
- if (action.actionType === FacebookConstants.FACEBOOK_SIGN_IN_DISCONNECT) {
- // console.log("facebookStore.dispatchToken: FACEBOOK_SIGN_IN_DISCONNECT");
- facebookStore.disconnectFromFacebook();
- }
-
- if (action.actionType === FacebookConstants.FACEBOOK_GETTING_PICTURE) {
- facebookStore.setFacebookPictureData(action.actionType, action.data);
- }
-
- if (action.actionType === FacebookConstants.FACEBOOK_RECEIVED_PICTURE) {
- console.log("FACEBOOK_RECEIVED_PICTURE");
- facebookStore.setFacebookPictureData(action.actionType, action.data);
- facebookStore.saveFacebookPictureData(action.data);
- facebookStore.saveFacebookAuthData();
- // We could use facebookStore.connectWithFacebook() here
- }
-});
-
-module.exports = facebookStore;
+module.exports = new FacebookStore(Dispatcher);
diff --git a/src/js/stores/VoterStore.js b/src/js/stores/VoterStore.js
index f59e855fc..d043c475c 100644
--- a/src/js/stores/VoterStore.js
+++ b/src/js/stores/VoterStore.js
@@ -24,6 +24,10 @@ class VoterStore extends FluxMapStore {
return this.getState().address.text_for_map_search;
}
+ getPhoto (){
+ return this.getState().voter.facebook_profile_image_url_https;
+ }
+
voterDeviceId () {
return this.getState().voter.voter_device_id || cookies.getItem("voter_device_id");
}
@@ -60,6 +64,12 @@ class VoterStore extends FluxMapStore {
google_civic_election_id: action.res.google_civic_election_id }
};
+ case "voterPhotoSave":
+ return {
+ ...state,
+ voter: {...state.voter, facebook_profile_image_url_https: action.res.facebook_profile_image_url_https}
+ };
+
case "error-voterRetrieve" || "error-voterAddressRetrieve" || "error-voterAddressSave":
console.log(action);
return state;