Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
import { createLogic } from 'redux-logic';
import { NOTIFY_CREATE, NOTIFY_REMOVE, NOTIFY_QUEUE,
NOTIFY_DISPLAY_QUEUED,
notifyQueue, notifyRemove, notifyDisplayQueued
} from './actions';
import { selectors as notifySel } from './reducer';
export const MAX_DISPLAY = 3;
export const DISPLAY_TIME = 3000;
export const notifyCreateLogic = createLogic({
type: NOTIFY_CREATE,
// check to see if we can add directly
validate({ getState, action }, allow, reject) {
const state = getState();
const current = notifySel.messages(state);
const queue = notifySel.queue(state);
if (current.length < MAX_DISPLAY && !queue.length) {
allow(action);
} else {
reject(notifyQueue(action.payload));
}
},
// if we had added directly then schedule remove
process({ action }, dispatch, done) {
return;
}
const sub = apolloClient.subscribe({ query: newTodosQuery }).subscribe({
next(todo) {
dispatch(todoReceived(todo));
},
error(err) {
console.error('todo subscription error', err);
}
});
subscriptions['todo'] = sub
dispatch(subscribeTodosSucceeded(sub._networkSubscriptionId));
}
});
export const todoUnsubscribeLogic = createLogic({
type: UNSUBSCRIBE,
latest: true,
process({ apolloClient, subscriptions }, dispatch) {
const sub = subscriptions['todo'];
sub.unsubscribe();
subscriptions['todo'] = null;
dispatch(unsubscribeTodosSucceeded(sub._networkSubscriptionId));
}
});
export const todoCreateLogic = createLogic({
type: CREATE,
processOptions: {
dispatchReturn: true,
import { createLogic } from 'redux-logic';
import { LOGIN_CHECK, LOGOUT, LOGIN_SUCCESS, LOGIN_FAILURE } from './actions';
const logoutLogic = createLogic({
type: LOGOUT,
process({ action }, dispatch, done) {
localStorage.removeItem('user');
sessionStorage.removeItem('loggedIn');
dispatch({
type: LOGIN_CHECK
});
done();
}
});
const loginCheckLogic = createLogic({
type: LOGIN_CHECK,
async process({ Db }, dispatch, done) {
let user;
try {
if (!isValid) {
dispatch(quiz.answer.update({ isMarked: true, isValid: false }));
}
if (isValid && !isDisabled) {
dispatch(quiz.answer.check());
}
if (isValid && isDisabled) {
dispatch(quiz.answer.confirm());
}
done();
},
});
export const confirmAnswerLogic = createLogic({
type: quiz.answer.confirm,
latest: true,
validate({ getState, action }, allow, reject) {
const { value, isValid, isDisabled, isIgnored } = selectAnswer(getState());
if (value && isValid && isDisabled && !isIgnored) {
allow(action);
} else {
reject();
}
},
process(_, dispatch, done) {
dispatch(quiz.answer.record.request());
done();
},
});
})
.then(({ data }) => {
dispatch(trackViewerSetTrackUID(data.uid));
})
.catch((e) => {
dispatch(toastsAddError(`Nepodarilo sa nahrať súbor: ${e.message}`));
})
.then(() => {
storeDispatch(stopProgress(pid));
done();
});
}
},
});
export const gpxLoadLogic = createLogic({
type: 'GPX_LOAD',
process({ getState }, dispatch, done) {
const pid = Math.random();
dispatch(startProgress(pid));
axios.get(getState().trackViewer.gpxUrl, { validateStatus: status => status === 200 })
.then(({ data }) => {
dispatch(trackViewerSetData({ trackGpx: data }));
})
.catch((e) => {
dispatch(toastsAddError(`Nastala chyba pri zÃskavanà GPX záznamu: ${e.message}`));
})
.then(() => {
dispatch(stopProgress(pid));
done();
});
const { quiz: { minimumSrsToReview } } = selectSettings(getState());
const serverSettings = deserializeSettings(payload);
api.saveSettings({ id, settings: serverSettings })
.then(() => {
if (payload.quiz.minimumSrsToReview !== minimumSrsToReview) {
dispatch(app.reviews.queue.clear());
dispatch(app.lessons.queue.clear());
dispatch(app.user.load.request());
}
dispatch(app.settings.save.success(payload));
done();
});
},
});
export const resetProgressLogic = createLogic({
type: app.settings.resetProgress.request,
processOptions: {
failType: app.settings.resetProgress.failure,
},
process({ action: { payload } }, dispatch, done) {
api.resetProgress(payload).then(() => {
dispatch(app.reviews.queue.clear());
dispatch(app.user.load.request());
done();
});
},
});
export default [
resetProgressLogic,
saveSettingsLogic,
payload: { role, reason: { type: 'info', text: 'Role has been created successfully.' } }
});
} else {
dispatch({
type: actionTypes.SAVE_ROLE_FAILURE,
payload: { role, reason: { type: 'error', text: 'Error while creating the Role.' } }
});
}
}
dispatch({ type: actionTypes.FETCH_ROLES });
done();
}
});
const saveUserLogic = createLogic({
type: actionTypes.SAVE_USER,
async process({ action, Db }, dispatch, done) {
const user = await Db.User.findOne({
where: { userId: { [Db.sequelize.Op.eq]: action.payload.user.id } },
include: [{ model: Db.Role }, { model: Db.Status }]
});
if (user) {
const loggedInUser = Helper.getLogginUser();
if (loggedInUser) {
if (loggedInUser.userId === user.userId) {
const users = await Db.User.findAll({
include: [
{
model: Db.Status,
where: {
body: JSON.stringify({
content,
author: {
name: authorName,
email: authorEmail,
},
}),
})
.then(res => res.json())
.then(data => dispatch(Comments.added(data)))
.catch(err => dispatch(Posts.error(err)))
.then(() => done());
},
});
const removeCommentLogic = createLogic({
type: COMMENTS.REMOVE,
process({ action }, dispatch, done) {
const { id, cid } = action.payload;
fetch(`/populated/posts/${id}/comments/${cid}`, {
method: 'DELETE',
headers: {
'Content-Type': 'application/json',
},
})
.then(res => res.json())
.then(deletedCommentInPost =>
dispatch(Comments.removed(deletedCommentInPost)),
)
.catch(err => dispatch(Posts.error(err)))
.then(() => done());
},
successType: app.reviews.queue.load.success,
failType: app.reviews.queue.load.failure,
},
process({ getState, action: { payload } }) {
return api.getCurrentReviews(payload).then(({ body }) => {
const { reviews, ids } = serializeQueueResponse(body);
return {
reviews,
ids: difference(ids, [sel.selectCurrentId(getState())]),
};
});
},
});
export const lessonsQueueLoadLogic = createLogic({
type: app.lessons.queue.load.request,
warnTimeout: 8000,
latest: true,
processOptions: {
successType: app.lessons.queue.load.success,
failType: app.lessons.queue.load.failure,
},
process({ action: { payload } }) {
return api
.getCurrentLessons(payload)
.then(({ body }) => serializeQueueResponse(body));
},
});
import { createLogic } from 'redux-logic'
import { showNotification, hideNotification } from './actions'
let nextNotificationId = 0
const showNotificationWithTimeoutLogic = createLogic({
type: 'SHOW_NOTIFICATION_WITH_TIMEOUT',
process ({ getState, action }, dispatch, done) {
const id = nextNotificationId++
dispatch(showNotification(id, action.text))
setTimeout(() => {
dispatch(hideNotification(id))
done()
}, 5000)
}
})
export default [
showNotificationWithTimeoutLogic
]