tutorial status is stored in user account if exists or in local storage
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { MYBADGES_CONNECT, MYBADGES_DISCONNECT, USER_LOADED, USER_LOADING, AUTH_ERROR, LOGIN_SUCCESS, LOGIN_FAIL, LOGOUT_SUCCESS, LOGOUT_FAIL, REFRESH_TOKEN_SUCCESS } from '../actions/types';
|
||||
import { MYBADGES_CONNECT, MYBADGES_DISCONNECT, GET_STATUS, USER_LOADED, USER_LOADING, AUTH_ERROR, LOGIN_SUCCESS, LOGIN_FAIL, LOGOUT_SUCCESS, LOGOUT_FAIL, REFRESH_TOKEN_SUCCESS } from '../actions/types';
|
||||
|
||||
import axios from 'axios';
|
||||
import { returnErrors, returnSuccess } from './messageActions'
|
||||
@@ -12,6 +12,10 @@ export const loadUser = () => (dispatch) => {
|
||||
});
|
||||
const config = {
|
||||
success: res => {
|
||||
dispatch({
|
||||
type: GET_STATUS,
|
||||
payload: res.data.user.status
|
||||
});
|
||||
dispatch({
|
||||
type: USER_LOADED,
|
||||
payload: res.data.user
|
||||
@@ -21,6 +25,15 @@ export const loadUser = () => (dispatch) => {
|
||||
if(err.response){
|
||||
dispatch(returnErrors(err.response.data.message, err.response.status));
|
||||
}
|
||||
console.log('auth failed');
|
||||
var status = [];
|
||||
if (window.localStorage.getItem('status')) {
|
||||
status = JSON.parse(window.localStorage.getItem('status'));
|
||||
}
|
||||
dispatch({
|
||||
type: GET_STATUS,
|
||||
payload: status
|
||||
});
|
||||
dispatch({
|
||||
type: AUTH_ERROR
|
||||
});
|
||||
@@ -31,6 +44,7 @@ export const loadUser = () => (dispatch) => {
|
||||
res.config.success(res);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
err.config.error(err);
|
||||
});
|
||||
};
|
||||
@@ -61,13 +75,26 @@ export const login = ({ email, password }) => (dispatch) => {
|
||||
type: LOGIN_SUCCESS,
|
||||
payload: res.data
|
||||
});
|
||||
dispatch({
|
||||
type: GET_STATUS,
|
||||
payload: res.data.user.status
|
||||
});
|
||||
dispatch(returnSuccess(res.data.message, res.status, 'LOGIN_SUCCESS'));
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
dispatch(returnErrors(err.response.data.message, err.response.status, 'LOGIN_FAIL'));
|
||||
dispatch({
|
||||
type: LOGIN_FAIL
|
||||
});
|
||||
var status = [];
|
||||
if (window.localStorage.getItem('status')) {
|
||||
status = JSON.parse(window.localStorage.getItem('status'));
|
||||
}
|
||||
dispatch({
|
||||
type: GET_STATUS,
|
||||
payload: status
|
||||
});
|
||||
});
|
||||
};
|
||||
|
||||
@@ -130,6 +157,14 @@ export const logout = () => (dispatch) => {
|
||||
dispatch({
|
||||
type: LOGOUT_SUCCESS
|
||||
});
|
||||
var status = [];
|
||||
if (window.localStorage.getItem('status')) {
|
||||
status = JSON.parse(window.localStorage.getItem('status'));
|
||||
}
|
||||
dispatch({
|
||||
type: GET_STATUS,
|
||||
payload: status
|
||||
});
|
||||
dispatch(returnSuccess(res.data.message, res.status, 'LOGOUT_SUCCESS'));
|
||||
clearTimeout(logoutTimerId);
|
||||
},
|
||||
@@ -138,6 +173,14 @@ export const logout = () => (dispatch) => {
|
||||
dispatch({
|
||||
type: LOGOUT_FAIL
|
||||
});
|
||||
var status = [];
|
||||
if (window.localStorage.getItem('status')) {
|
||||
status = JSON.parse(window.localStorage.getItem('status'));
|
||||
}
|
||||
dispatch({
|
||||
type: GET_STATUS,
|
||||
payload: status
|
||||
});
|
||||
clearTimeout(logoutTimerId);
|
||||
}
|
||||
};
|
||||
@@ -146,6 +189,7 @@ export const logout = () => (dispatch) => {
|
||||
res.config.success(res);
|
||||
})
|
||||
.catch(err => {
|
||||
console.log(err);
|
||||
if(err.response.status !== 401){
|
||||
err.config.error(err);
|
||||
}
|
||||
|
||||
@@ -3,21 +3,28 @@ import { MYBADGES_DISCONNECT, TUTORIAL_PROGRESS, GET_TUTORIAL, GET_TUTORIALS, TU
|
||||
import axios from 'axios';
|
||||
import { returnErrors, returnSuccess } from './messageActions';
|
||||
|
||||
export const getTutorial = (id) => (dispatch, getState) => {
|
||||
export const tutorialProgress = () => (dispatch) => {
|
||||
dispatch({type: TUTORIAL_PROGRESS});
|
||||
};
|
||||
|
||||
|
||||
export const getTutorial = (id) => (dispatch, getState) => {
|
||||
axios.get(`${process.env.REACT_APP_BLOCKLY_API}/tutorial/${id}`)
|
||||
.then(res => {
|
||||
var tutorial = res.data.tutorial;
|
||||
existingTutorial(tutorial, getState().tutorial.status).then(status => {
|
||||
console.log('progress',getState().auth.progress);
|
||||
console.log('status');
|
||||
dispatch({
|
||||
type: TUTORIAL_SUCCESS,
|
||||
payload: status
|
||||
});
|
||||
dispatch({type: TUTORIAL_PROGRESS});
|
||||
dispatch(updateStatus(status));
|
||||
dispatch({
|
||||
type: GET_TUTORIAL,
|
||||
payload: tutorial
|
||||
});
|
||||
dispatch({type: TUTORIAL_PROGRESS});
|
||||
dispatch(returnSuccess(res.data.message, res.status));
|
||||
});
|
||||
})
|
||||
@@ -30,7 +37,6 @@ export const getTutorial = (id) => (dispatch, getState) => {
|
||||
};
|
||||
|
||||
export const getTutorials = () => (dispatch, getState) => {
|
||||
dispatch({type: TUTORIAL_PROGRESS});
|
||||
axios.get(`${process.env.REACT_APP_BLOCKLY_API}/tutorial`)
|
||||
.then(res => {
|
||||
var tutorials = res.data.tutorials;
|
||||
@@ -40,6 +46,7 @@ export const getTutorials = () => (dispatch, getState) => {
|
||||
type: TUTORIAL_SUCCESS,
|
||||
payload: status
|
||||
});
|
||||
dispatch(updateStatus(status));
|
||||
dispatch({
|
||||
type: GET_TUTORIALS,
|
||||
payload: tutorials
|
||||
@@ -75,6 +82,24 @@ export const assigneBadge = (id) => (dispatch, getState) => {
|
||||
});
|
||||
};
|
||||
|
||||
export const updateStatus = (status) => (dispatch, getState) => {
|
||||
if(getState().auth.isAuthenticated){
|
||||
// update user account in database - sync with redux store
|
||||
axios.put(`${process.env.REACT_APP_BLOCKLY_API}/user/status`, {status: status})
|
||||
.then(res => {
|
||||
// dispatch(returnSuccess(badge, res.status, 'UPDATE_STATUS_SUCCESS'));
|
||||
})
|
||||
.catch(err => {
|
||||
if(err.response){
|
||||
// dispatch(returnErrors(err.response.data.message, err.response.status, 'UPDATE_STATUS_FAIL'));
|
||||
}
|
||||
});
|
||||
} else {
|
||||
// update locale storage - sync with redux store
|
||||
window.localStorage.setItem('status', JSON.stringify(status));
|
||||
}
|
||||
};
|
||||
|
||||
export const deleteTutorial = (id) => (dispatch, getState) => {
|
||||
var tutorial = getState().tutorial;
|
||||
var id = getState().builder.id;
|
||||
@@ -127,6 +152,7 @@ export const tutorialCheck = (status, step) => (dispatch, getState) => {
|
||||
type: status === 'success' ? TUTORIAL_SUCCESS : TUTORIAL_ERROR,
|
||||
payload: tutorialsStatus
|
||||
});
|
||||
dispatch(updateStatus(tutorialsStatus));
|
||||
dispatch(tutorialChange());
|
||||
dispatch(returnSuccess('','','TUTORIAL_CHECK_SUCCESS'));
|
||||
};
|
||||
@@ -149,6 +175,7 @@ export const storeTutorialXml = (code) => (dispatch, getState) => {
|
||||
type: TUTORIAL_XML,
|
||||
payload: tutorialsStatus
|
||||
});
|
||||
dispatch(updateStatus(tutorialsStatus));
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
@@ -23,6 +23,7 @@ export const NAME = 'NAME';
|
||||
export const TUTORIAL_PROGRESS = 'TUTORIAL_PROGRESS';
|
||||
export const GET_TUTORIAL = 'GET_TUTORIAL';
|
||||
export const GET_TUTORIALS = 'GET_TUTORIALS';
|
||||
export const GET_STATUS = 'GET_STATUS';
|
||||
export const TUTORIAL_SUCCESS = 'TUTORIAL_SUCCESS';
|
||||
export const TUTORIAL_ERROR = 'TUTORIAL_ERROR';
|
||||
export const TUTORIAL_CHANGE = 'TUTORIAL_CHANGE';
|
||||
|
||||
Reference in New Issue
Block a user