2021-09-13 20:41:05 +00:00
|
|
|
import { call, put, select, take, takeLatest } from 'redux-saga/effects';
|
2020-03-30 19:50:27 +00:00
|
|
|
import { sanitizedRaw } from '@nozbe/watermelondb/RawRecord';
|
|
|
|
|
|
|
|
import { CREATE_DISCUSSION, LOGIN } from '../actions/actionsTypes';
|
2021-09-13 20:41:05 +00:00
|
|
|
import { createDiscussionFailure, createDiscussionSuccess } from '../actions/createDiscussion';
|
2020-03-30 19:50:27 +00:00
|
|
|
import database from '../lib/database';
|
2022-06-06 14:17:51 +00:00
|
|
|
import { events, logEvent } from '../lib/methods/helpers/log';
|
2022-04-28 20:37:25 +00:00
|
|
|
import { Services } from '../lib/services';
|
2020-03-30 19:50:27 +00:00
|
|
|
|
|
|
|
const handleRequest = function* handleRequest({ data }) {
|
2021-02-11 21:44:50 +00:00
|
|
|
logEvent(events.CD_CREATE);
|
2020-03-30 19:50:27 +00:00
|
|
|
try {
|
|
|
|
const auth = yield select(state => state.login.isAuthenticated);
|
|
|
|
if (!auth) {
|
|
|
|
yield take(LOGIN.SUCCESS);
|
|
|
|
}
|
2022-04-28 20:37:25 +00:00
|
|
|
const result = yield Services.createDiscussion(data);
|
2020-03-30 19:50:27 +00:00
|
|
|
|
|
|
|
if (result.success) {
|
|
|
|
const { discussion: sub } = result;
|
|
|
|
|
|
|
|
try {
|
|
|
|
const db = database.active;
|
2021-02-26 16:25:51 +00:00
|
|
|
const subCollection = db.get('subscriptions');
|
2021-09-13 20:41:05 +00:00
|
|
|
yield db.action(async () => {
|
|
|
|
await subCollection.create(s => {
|
2020-03-30 19:50:27 +00:00
|
|
|
s._raw = sanitizedRaw({ id: sub.rid }, subCollection.schema);
|
|
|
|
Object.assign(s, sub);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
} catch {
|
|
|
|
// do nothing
|
|
|
|
}
|
|
|
|
yield put(createDiscussionSuccess(sub));
|
|
|
|
} else {
|
2021-02-11 21:44:50 +00:00
|
|
|
logEvent(events.CD_CREATE_F);
|
2020-03-30 19:50:27 +00:00
|
|
|
yield put(createDiscussionFailure(result));
|
|
|
|
}
|
|
|
|
} catch (err) {
|
2021-02-11 21:44:50 +00:00
|
|
|
logEvent(events.CD_CREATE_F);
|
2020-03-30 19:50:27 +00:00
|
|
|
yield put(createDiscussionFailure(err));
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
const root = function* root() {
|
|
|
|
yield takeLatest(CREATE_DISCUSSION.REQUEST, handleRequest);
|
|
|
|
};
|
|
|
|
|
|
|
|
export default root;
|