Skip to content

Instantly share code, notes, and snippets.

Created August 15, 2016 12:32
Show Gist options
  • Save anonymous/2cfc3bfe6cfda8f4eefa2c5b8d7d32f6 to your computer and use it in GitHub Desktop.
Save anonymous/2cfc3bfe6cfda8f4eefa2c5b8d7d32f6 to your computer and use it in GitHub Desktop.
JS Bin // source http://jsbin.com/zekusiloxi
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width">
<title>JS Bin</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/redux/3.5.2/redux.min.js"></script>
<!-- <script src="https://npmcdn.com/react-redux@4.4.5/dist/react-redux.min.js"></script> -->
</head>
<body>
<script id="jsbin-javascript">
var CREATE_AUTH_TOKEN, DELETE_AUTH_TOKEN, UPDATE_CURRENT_PROFILE, auth, authService, combineReducers, createAuthToken, createStore, currentProfile, deleteAuthToken, store, unsubscribe, updateCurrentProfile;
createStore = Redux.createStore;
combineReducers = Redux.combineReducers;
CREATE_AUTH_TOKEN = "CREATE_AUTH_TOKEN";
DELETE_AUTH_TOKEN = "DELETE_AUTH_TOKEN";
UPDATE_CURRENT_PROFILE = "SET_CURRENT_PROFILE";
createAuthToken = (function(_this) {
return function() {
return {
type: CREATE_AUTH_TOKEN,
token: "random_secret_token"
};
};
})(this);
deleteAuthToken = (function(_this) {
return function() {
return {
type: DELETE_AUTH_TOKEN,
token: null
};
};
})(this);
updateCurrentProfile = (function(_this) {
return function(profile) {
return {
type: UPDATE_CURRENT_PROFILE,
profile: profile
};
};
})(this);
auth = (function(_this) {
return function(state, action) {
if (state == null) {
state = {
token: null
};
}
switch (action.type) {
case CREATE_AUTH_TOKEN:
return Object.assign({}, state, {
token: action.token
});
case DELETE_AUTH_TOKEN:
return Object.assign({}, state, {
token: action.token
});
default:
return state;
}
};
})(this);
currentProfile = (function(_this) {
return function(state, action) {
if (state == null) {
state = null;
}
switch (action.type) {
case UPDATE_CURRENT_PROFILE:
return Object.assign({}, state, action.profile);
default:
return state;
}
};
})(this);
authService = combineReducers({
auth: auth,
currentProfile: currentProfile
});
store = createStore(authService);
console.log(store.getState());
unsubscribe = store.subscribe((function(_this) {
return function() {
return console.log(store.getState());
};
})(this));
store.dispatch(createAuthToken());
store.dispatch(updateCurrentProfile({
id: 1,
username: "test"
}));
store.dispatch(deleteAuthToken());
store.dispatch(updateCurrentProfile(null));
unsubscribe();
</script>
<script id="jsbin-source-javascript" type="text/javascript">createStore = Redux.createStore
combineReducers = Redux.combineReducers
# Action Types
CREATE_AUTH_TOKEN = "CREATE_AUTH_TOKEN"
DELETE_AUTH_TOKEN = "DELETE_AUTH_TOKEN"
UPDATE_CURRENT_PROFILE = "SET_CURRENT_PROFILE"
# Action Creators
createAuthToken = =>
{type: CREATE_AUTH_TOKEN, token: "random_secret_token"}
deleteAuthToken = =>
{type: DELETE_AUTH_TOKEN, token: null}
updateCurrentProfile = (profile)=>
{type: UPDATE_CURRENT_PROFILE, profile: profile}
# Reducers
auth = (state = {token: null}, action)=>
switch action.type
when CREATE_AUTH_TOKEN
return Object.assign({}, state, {token: action.token})
when DELETE_AUTH_TOKEN
return Object.assign({}, state, {token: action.token})
else
return state
currentProfile = (state = null, action)=>
switch action.type
when UPDATE_CURRENT_PROFILE
return Object.assign({}, state, action.profile)
else
return state
authService = combineReducers
auth: auth
currentProfile: currentProfile
# Store
store = createStore authService
# Code
console.log(store.getState())
unsubscribe = store.subscribe(() =>
console.log(store.getState())
)
store.dispatch createAuthToken()
store.dispatch updateCurrentProfile({id: 1, username: "test"})
store.dispatch deleteAuthToken()
store.dispatch updateCurrentProfile(null)
unsubscribe()</script></body>
</html>
var CREATE_AUTH_TOKEN, DELETE_AUTH_TOKEN, UPDATE_CURRENT_PROFILE, auth, authService, combineReducers, createAuthToken, createStore, currentProfile, deleteAuthToken, store, unsubscribe, updateCurrentProfile;
createStore = Redux.createStore;
combineReducers = Redux.combineReducers;
CREATE_AUTH_TOKEN = "CREATE_AUTH_TOKEN";
DELETE_AUTH_TOKEN = "DELETE_AUTH_TOKEN";
UPDATE_CURRENT_PROFILE = "SET_CURRENT_PROFILE";
createAuthToken = (function(_this) {
return function() {
return {
type: CREATE_AUTH_TOKEN,
token: "random_secret_token"
};
};
})(this);
deleteAuthToken = (function(_this) {
return function() {
return {
type: DELETE_AUTH_TOKEN,
token: null
};
};
})(this);
updateCurrentProfile = (function(_this) {
return function(profile) {
return {
type: UPDATE_CURRENT_PROFILE,
profile: profile
};
};
})(this);
auth = (function(_this) {
return function(state, action) {
if (state == null) {
state = {
token: null
};
}
switch (action.type) {
case CREATE_AUTH_TOKEN:
return Object.assign({}, state, {
token: action.token
});
case DELETE_AUTH_TOKEN:
return Object.assign({}, state, {
token: action.token
});
default:
return state;
}
};
})(this);
currentProfile = (function(_this) {
return function(state, action) {
if (state == null) {
state = null;
}
switch (action.type) {
case UPDATE_CURRENT_PROFILE:
return Object.assign({}, state, action.profile);
default:
return state;
}
};
})(this);
authService = combineReducers({
auth: auth,
currentProfile: currentProfile
});
store = createStore(authService);
console.log(store.getState());
unsubscribe = store.subscribe((function(_this) {
return function() {
return console.log(store.getState());
};
})(this));
store.dispatch(createAuthToken());
store.dispatch(updateCurrentProfile({
id: 1,
username: "test"
}));
store.dispatch(deleteAuthToken());
store.dispatch(updateCurrentProfile(null));
unsubscribe();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment