Skip to content

Commit

Permalink
Merge pull request #77 from jagreene/simulator-refactor
Browse files Browse the repository at this point in the history
ADD: Rewritten Simulator Using Es6, Jsx, and Redux
  • Loading branch information
jagreene authored Jul 12, 2016
2 parents f4f0038 + f8964d9 commit 09512d9
Show file tree
Hide file tree
Showing 37 changed files with 6,618 additions and 1,114 deletions.
3 changes: 3 additions & 0 deletions .babelrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"presets": [ "es2015", "stage-2", "react" ]
}
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Simulator/wdc-simulator.js
Simulator/tests
25 changes: 25 additions & 0 deletions .eslintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
{
"extends": [
"airbnb"
],
"parser": "babel-eslint",
"env": {
"browser": true,
},
"globals": {
"toastr": false,
},
"rules": {
"max-len": "warn",
"indent": "warn",
"react/jsx-indent": "warn",
"no-unused-vars": "warn",
"prefer-const": "warn",
"no-console": ["warn", { "allow": ["warn", "error"] }],
"spaced-comment": "off",
"react/prop-types": "off",
"react/prefer-stateless-function": "off",
"no-use-before-define": "off",
"react/sort-comp": "off",
},
}
14 changes: 14 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
language: node_js
node_js:
- node
before_script:
- export DISPLAY=:99.0
- sh -e /etc/init.d/xvfb start
- sleep 3
script:
- '[ "${TRAVIS_PULL_REQUEST}" = "false" ] && npm run test:travis || npm run unit:coveralls'
addons:
sauce_connect:
username: jaustingreene
access_key:
secure: HJoFcs33myPRMQjYxgYnGG1VbxYdbzxUiNRK713qhmCKoIFfZahy7PpOimQ8+0dRJ2rv33DSK5v20LbH7tiOWVWEfEwVOVln8ItPjLTBHX3dUaO8OuO+JA18MFLVBwIWqdrd+h3lvACCjRTnl5DMYbVT37vMPe81xYgee0VqEFaD/3otHwvqVwkWC6GA3r6siXXA45ZeJod1qFQI4G9pixUuFQ6h5cjyk8PFi9n4XWeREgiRjFx6PUVzyK9MnA9swuRHRkLuICmlrpfGjxT0mt/a2m6vbgxf9prFijXV1ngV7GXaqT4hAsk5vnyAQt6kfnIDUBOywnRXLHOHZpzI2ZTRwV8JJ3DIMhEtJZ9At3u+FzXWvaFG7MMBinujQ39iUipKWYgKPJTEkjwOsfROVhclgMC6VZKG/f7FJw85/vKogssxkYimWfqLeDboHjzVzMhUzUPoky59JcNrt48/95Ko7TLMze46mwkmdFMY/5aeekGZhwS2qUHUEb+VV5LxloXkoV6lxlQCCgmNhMRvzYbh2otozjuDvCNOkUU4k+onE2+UfwkVtRlvisA4TLVAltwAMF6h4iVUEz/OiA37756E9qg3rMarV+HKJbjvOtnXOYEzY047LRL17dUl1Ovcg2yKdmaeJa3By0O6Z0mkIDSsIVtDp/pKpr44i2sKsmY=
6 changes: 4 additions & 2 deletions Examples/OAuthProxyExample/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,10 @@ app.use(express.static(__dirname + '/public'));
// -------------------------------------------------- //
// Variables
// -------------------------------------------------- //
var clientID = config.CLIENT_ID
var clientSecret = config.CLIENT_SECRET
var clientID = process.env.FOURSQUARE_CLIENT_ID || config.CLIENT_ID;
var clientSecret = process.env.FOURSQUARE_CLIENT_SECRET || config.CLIENT_SECRET;
console.log(clientID);
console.log(clientSecret);
var redirectURI = config.HOSTPATH + ":" + config.PORT + config.REDIRECT_PATH

// -------------------------------------------------- //
Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
Currently in development is version 2.0 of the web data connector API.
# Tableau Web Data Connector SDK
[![Coverage Status](https://coveralls.io/repos/github/jagreene/webdataconnector/badge.svg?branch=tests)](https://coveralls.io/github/jagreene/webdataconnector?branch=tests) [![Build Status](https://travis-ci.org/jagreene/webdataconnector.svg?branch=tests)](https://travis-ci.org/jagreene/webdataconnector)

Currently in development is version 2.0 of the web data connector API.

All of the information for this content can be found in our beta docs: [http://tableau.github.io/webdataconnector](http://tableau.github.io/webdataconnector/).

Expand Down
263 changes: 263 additions & 0 deletions Simulator/actions/messaging_actions.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,263 @@
import { eventNames, phases } from '../utils/consts';
import { validateData, validateSchema } from '../utils/validation.js';
import * as simulatorActions from './simulator_actions';

// Receiving Message Thunks
export function receiveMessage(payload) {
// Routes received messages to other thunks as necessary
return (dispatch) => {
try {
const { msgData, msgName, props: attrs, version } = JSON.parse(payload.data);

if(version=== "1.1.0" || version === "1.1.1") {
var errmsg = 'this simulator only supports wdcs that are using version 2.0 of the api or later, your wdc\'s version is: ' + wdcapiversion;
toastr.error(errmsg, 'unsupported wdc version error:')
}

if (attrs) {
dispatch(simulatorActions.setWdcAttrs(attrs));
}

switch (msgName) {
case eventNames.LOG: {
dispatch(handleLog(msgData.logMsg));
break;
}

case eventNames.LOADED: {
dispatch(handleLoaded(version));
handleLoaded();
break;
}

case eventNames.INIT_CB: {
dispatch(handleInit());
break;
}

case eventNames.SUBMIT: {
dispatch(handleSubmit());
break;
}

case eventNames.SCHEMA_CB: {
const schema = msgData.schema;
dispatch(handleSchemaCallback(schema));
break;
}

case eventNames.DATA_CB: {
const tableName = msgData.tableName;
const data = msgData.data;
dispatch(handleDataCallback(tableName, data));
break;
}

case eventNames.SHUTDOWN_CB: {
dispatch(handleShutdownCallback());
break;
}

case eventNames.ABORT: {
const errorMsg = msgData.errorMsg;
dispatch(handleAbort(errorMsg));
break;
}

case eventNames.DATA_DONE_CB: {
dispatch(handleDataDoneCallback());
break;
}

case eventNames.ABORT_AUTH: {
const errorMsg = msgData.errorMsg;
dispatch(handleAbortForAuth(errorMsg));
break;
}
default: {
// The message was not for the simulator, ignore it.
break;
}
}
} catch (e) {
// The message was not for the simulator, ignore it.
return;
}
};
}

export function handleLog(msg) {
return () => {
/* eslint-disable no-console */
console.log(msg);
/* eslint-enable no-console */
};
}

export function handleLoaded() {
return (dispatch) => {
dispatch(sendInit());
};
}

export function handleInit() {
return (dispatch, getState) => {
const { currentPhase } = getState();
dispatch(simulatorActions.setPhaseInitCallbackCalled(true));
dispatch(simulatorActions.setPhaseInProgress(true));
// If we are in the Gather Data Phase, ask wdc for headers
if (currentPhase === phases.GATHER_DATA) {
dispatch(sendGetHeaders());
}
};
}

export function handleSubmit() {
return (dispatch) => {
// Clean up simulator and start Data Gather Phase
dispatch(simulatorActions.setPhaseSubmitCalled(true));
dispatch(simulatorActions.setPhaseInProgress(false));
dispatch(simulatorActions.closeSimulatorWindow());
dispatch(simulatorActions.resetTables());
dispatch(simulatorActions.startGatherDataPhase());
};
}

export function handleSchemaCallback(schema) {
return (dispatch, getState) => {
// Validate schema, and populate store with new table objects
// using the schema info
const { wdcShouldFetchAllTables } = getState();
if (validateSchema(schema)) {
const newTables = schema.reduce((tables, tableInfo) => {
const newTable = {
schema: tableInfo,
data: [],
};

return { ...tables, [tableInfo.id]: newTable };
}, {});
dispatch(simulatorActions.addTables(newTables));

// if we are supposed to automatically fetch the tables, do it
if (wdcShouldFetchAllTables) {
dispatch(fetchAllData());
} else {
// We we aren't going to fetch immediately
// consider the phase paused until the getGeta
// message is sent. This is so we can properly
// disable the Fetch Data button
dispatch(simulatorActions.setPhaseInProgress(false));
}
} else {
toastr.error('Please see debug console for details.', 'WDC Validation Error');
}
};
}

export function handleDataCallback(tableName, data) {
return (dispatch, getState) => {
// if data is valid, add it to the appropriate tables object in the store
if (validateData(data)) {
const { tables } = getState();
const newTables = Object.assign({}, tables);
newTables[tableName].data = newTables[tableName].data.concat(data);
dispatch(simulatorActions.setTables(newTables));
} else {
toastr.error('Please see debug console for details.', 'WDC Validation Error');
}
};
}

export function handleDataDoneCallback() {
return (dispatch) => {
// Tell the wdc to shutdown now that we have our data
dispatch(sendShutdown());
};
}

export function handleShutdownCallback() {
return (dispatch) => {
// Wdc has told us it's shutdown, end our phase
dispatch(simulatorActions.setPhaseInProgress(false));
};
}

export function handleAbort(errMsg) {
return (dispatch) => {
// Something went wrong, end phase, alert user
dispatch(simulatorActions.setPhaseInProgress(false));
console.error(errMsg);
toastr.error(errMsg, 'The WDC reported an error:');
};
}

export function handleAbortForAuth(errMsg) {
return (dispatch) => {
// Need auth, close the simulator, tell the user
const toastTitle = 'The WDC has been aborted for auth, ' +
'use the "Start Auth Phase" to test ' +
'your WDC Auth Mode:';
dispatch(simulatorActions.setPhaseInProgress(false));
dispatch(simulatorActions.closeSimulatorWindow());
toastr.error(errMsg, toastTitle);
};
}

// Send message thunks
export function sendMessage(messageName, payload = {}) {
return (dispatch, getState) => {
// Construct payload and send info to whichever
// window we are posting too
const { simulatorWindow, wdcAttrs } = getState();
const messagePayload = JSON.stringify({
msgName: messageName,
msgData: payload,
props: wdcAttrs,
});
simulatorWindow.postMessage(messagePayload, '*');
};
}

export function sendInit() {
return (dispatch, getState) => {
const { currentPhase } = getState();
dispatch(sendMessage(eventNames.INIT, { phase: currentPhase }));
};
}

export function sendGetHeaders() {
return (dispatch) => {
dispatch(sendMessage(eventNames.SCHEMA_GET));
};
}

// Takes an array of tableuInfo/incValue pairs
export function sendGetData(tablesAndIncrementValues, isFreshFetch) {
return (dispatch) => {
if (isFreshFetch) {
const tableKey = tablesAndIncrementValues[0].tableInfo.id;
dispatch(simulatorActions.resetTableData(tableKey));
}

dispatch(sendMessage(eventNames.DATA_GET, { tablesAndIncrementValues }));
dispatch(simulatorActions.setPhaseInProgress(true));
};
}

export function sendShutdown() {
return (dispatch) => {
dispatch(sendMessage(eventNames.SHUTDOWN));
};
}

export function fetchAllData() {
return (dispatch, getState) => {
// Loop through our tables and tell the wdc to give us the data
// for each of them
const { tables } = getState();
Object.keys(tables).forEach(key => {
dispatch(sendGetData([{ tableInfo: tables[key].schema }]));
});
};
}
Loading

0 comments on commit 09512d9

Please sign in to comment.