-
Notifications
You must be signed in to change notification settings - Fork 1
/
saga.js
123 lines (100 loc) · 3.26 KB
/
saga.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
// NPM Dependencies
import { all, call, delay, put, take, takeLatest, fork, select, race } from 'redux-saga/effects';
import es6promise from 'es6-promise';
import 'isomorphic-unfetch';
import axios from 'axios';
es6promise.polyfill();
import {
actionTypes,
failure,
replaceInvoiceStatus,
startRealtimeInvoiceStatus,
stopRealtimeInvoiceStatus,
createFundedQuiz,
replaceFundedQuiz
} from './actions'
function* createInvoiceSaga({ payload }) {
try {
const { amount, senderName, quizId } = payload;
const response = yield call(
axios.post,
'https://api.lightning.gifts/create',
{ amount, senderName }
);
const data = response.data;
yield put(replaceInvoiceStatus({ ...data, quizId }));
yield put(startRealtimeInvoiceStatus({ chargeId: data.chargeId }));
// yield put(startRealtimeInvoiceStatus({ orderId: data.orderId }));
} catch (err) {
console.log('err', err);
yield put(failure(err))
}
}
export function* startRealTimeCheckInvoiceStatusOnRequest({ payload }) {
while (true) {
try {
const { chargeId } = payload;
const response = yield call(axios.get, `https://api.lightning.gifts/status/${chargeId}`);
const data = response.data;
yield put(replaceInvoiceStatus(data));
if (data.status === 'paid') {
yield put(createFundedQuiz(data));
yield put(stopRealtimeInvoiceStatus());
}
yield delay(5000);
} catch (err) {
console.log('err', err);
yield put(failure(err));
yield delay(15000);
}
}
}
function* startRealtimeInvoiceStatusSaga() {
while (true) {
const req = yield take(actionTypes.START_RT_CHECK_INVOICE_STATUS);
yield race([
call(startRealTimeCheckInvoiceStatusOnRequest, req),
take(actionTypes.STOP_RT_CHECK_INVOICE_STATUS)
]);
}
}
function* createNewFundedQuizSaga() {
try {
const invoiceStatus = yield select(state => state.invoiceStatus);
console.log('invoiceStatus', invoiceStatus);
const { quizId, orderId, amount } = invoiceStatus;
const response = yield call(
axios,
{
method: 'post',
url: `http://138.68.239.62/fundedquiz/`,
data: {
quiz: quizId,
complete: false,
percent_correct: "0",
amount,
lightning_gift_order_id: orderId,
redeemed: false
},
auth: {
username: 'guest',
password: 'boltathon'
}
});
const data = response.data;
console.log('rewe', data);
yield put(replaceFundedQuiz(data));
} catch (err) {
console.log('err', err);
yield put(failure(err));
}
}
// function*
function* rootSaga() {
yield all([
takeLatest(actionTypes.CREATE_FUNDED_QUIZ, createNewFundedQuizSaga),
takeLatest(actionTypes.CREATE_INVOICE, createInvoiceSaga),
fork(startRealtimeInvoiceStatusSaga),
])
}
export default rootSaga