|
@@ -15,63 +15,63 @@ export const actions = {
|
|
|
changeForm: (data) => {
|
|
|
return {
|
|
|
type: 'SMS/CHANGE_FORM',
|
|
|
- data
|
|
|
+ data,
|
|
|
}
|
|
|
},
|
|
|
setRecipients: (data) => {
|
|
|
return {
|
|
|
type: 'SMS/SET_RECIPIENTS',
|
|
|
- data
|
|
|
+ data,
|
|
|
}
|
|
|
},
|
|
|
addRecipient: (data) => {
|
|
|
return {
|
|
|
type: 'SMS/ADD_RECIPIENT',
|
|
|
- data
|
|
|
+ data,
|
|
|
}
|
|
|
},
|
|
|
removeRecipient: (data) => {
|
|
|
return {
|
|
|
type: 'SMS/REMOVE_RECIPIENT',
|
|
|
- data
|
|
|
+ data,
|
|
|
}
|
|
|
},
|
|
|
sendSMSRequest: (data) => {
|
|
|
return {
|
|
|
type: 'SMS/SEND_REQUEST',
|
|
|
- data
|
|
|
+ data,
|
|
|
}
|
|
|
},
|
|
|
sendSMSSuccess: (data) => {
|
|
|
return {
|
|
|
type: 'SMS/SEND_SUCCESS',
|
|
|
- data
|
|
|
+ data,
|
|
|
}
|
|
|
},
|
|
|
sendSMSFailure: (error) => {
|
|
|
return {
|
|
|
type: 'SMS/SEND_FAILURE',
|
|
|
- error
|
|
|
+ error,
|
|
|
}
|
|
|
},
|
|
|
getCreditRequest: (data) => {
|
|
|
return {
|
|
|
type: 'SMS/CREDIT_REQUEST',
|
|
|
- data
|
|
|
+ data,
|
|
|
}
|
|
|
},
|
|
|
getCreditSuccess: (data) => {
|
|
|
return {
|
|
|
type: 'SMS/CREDIT_SUCCESS',
|
|
|
- data
|
|
|
+ data,
|
|
|
}
|
|
|
},
|
|
|
getCreditFailure: (error) => {
|
|
|
return {
|
|
|
type: 'SMS/CREDIT_FAILURE',
|
|
|
- error
|
|
|
+ error,
|
|
|
}
|
|
|
- }
|
|
|
+ },
|
|
|
}
|
|
|
console.log('State actions', actions)
|
|
|
|
|
@@ -83,12 +83,12 @@ export const state = {
|
|
|
sending: false,
|
|
|
newRecipient: '',
|
|
|
message: '',
|
|
|
- credit: '?'
|
|
|
+ credit: '?',
|
|
|
}
|
|
|
console.log('State state', state)
|
|
|
|
|
|
/** reducer is called by the redux dispatcher and handles all component actions */
|
|
|
-export function reducer (state = [], action) {
|
|
|
+export function reducer(state = [], action) {
|
|
|
switch (action.type) {
|
|
|
case 'SMS/CHANGE_FORM':
|
|
|
return { ...state, ...action.data }
|
|
@@ -97,19 +97,35 @@ export function reducer (state = [], action) {
|
|
|
case 'SMS/ADD_RECIPIENT':
|
|
|
const number = normalizePhone(action.data)
|
|
|
if (number) {
|
|
|
- return { ...state, recipients: [ number, ...state.recipients ], newRecipient: '' }
|
|
|
+ return {
|
|
|
+ ...state,
|
|
|
+ recipients: [number, ...state.recipients],
|
|
|
+ newRecipient: '',
|
|
|
+ }
|
|
|
} else {
|
|
|
return state
|
|
|
- }
|
|
|
+ }
|
|
|
case 'SMS/REMOVE_RECIPIENT':
|
|
|
console.log(action.data)
|
|
|
- return { ...state, recipients: [ ...state.recipients.slice(0, action.data), ...state.recipients.slice(action.data + 1) ] }
|
|
|
+ return {
|
|
|
+ ...state,
|
|
|
+ recipients: [
|
|
|
+ ...state.recipients.slice(0, action.data),
|
|
|
+ ...state.recipients.slice(action.data + 1),
|
|
|
+ ],
|
|
|
+ }
|
|
|
case 'SMS/SEND_REQUEST':
|
|
|
return { ...state, sending: true }
|
|
|
case 'SMS/SEND_SUCCESS':
|
|
|
- return { ...state, sending: false, recipients: [], newRecipient: '', body: '' }
|
|
|
+ return {
|
|
|
+ ...state,
|
|
|
+ sending: false,
|
|
|
+ recipients: [],
|
|
|
+ newRecipient: '',
|
|
|
+ body: '',
|
|
|
+ }
|
|
|
case 'SMS/SEND_FAILURE':
|
|
|
- return { ...state, sending: false, }
|
|
|
+ return { ...state, sending: false }
|
|
|
case 'SMS/CREDIT_REQUEST':
|
|
|
return { ...state, credit: '...' }
|
|
|
case 'SMS/CREDIT_SUCCESS':
|
|
@@ -121,22 +137,27 @@ export function reducer (state = [], action) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-function * sendSMS (action) {
|
|
|
+function* sendSMS(action) {
|
|
|
try {
|
|
|
const token = localStorage.getItem('accessToken')
|
|
|
console.log('Send SMS requested', action, token)
|
|
|
const { body, sender, recipients } = action.data
|
|
|
+ const recipientNumbers = recipients
|
|
|
+ .map((recipient) =>
|
|
|
+ typeof recipient === 'string' ? recipient : recipient.phone
|
|
|
+ )
|
|
|
+ .filter((recipient) => recipient !== null)
|
|
|
const response = yield call(fetch, `${SZTM_API}/api/sms/send`, {
|
|
|
method: 'POST',
|
|
|
headers: {
|
|
|
'Content-Type': 'application/json',
|
|
|
- 'x-access-token': token
|
|
|
+ 'x-access-token': token,
|
|
|
},
|
|
|
body: JSON.stringify({
|
|
|
- recipients,
|
|
|
+ recipients: recipientNumbers,
|
|
|
body,
|
|
|
- sender
|
|
|
- })
|
|
|
+ sender,
|
|
|
+ }),
|
|
|
})
|
|
|
if (response.status !== 200) {
|
|
|
console.log(response)
|
|
@@ -151,17 +172,19 @@ function * sendSMS (action) {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-function * getCredit (action) {
|
|
|
+function* getCredit(action) {
|
|
|
console.log('getting credit.')
|
|
|
- yield put(api.actions.apiRequest({
|
|
|
- path: 'api/sms/credits',
|
|
|
- onSuccess: actions.getCreditSuccess,
|
|
|
- onFailure: actions.getCreditFailure
|
|
|
- }))
|
|
|
+ yield put(
|
|
|
+ api.actions.apiRequest({
|
|
|
+ path: 'api/sms/credits',
|
|
|
+ onSuccess: actions.getCreditSuccess,
|
|
|
+ onFailure: actions.getCreditFailure,
|
|
|
+ })
|
|
|
+ )
|
|
|
}
|
|
|
|
|
|
/** sagas are asynchronous workers (JS generators) to handle the state. */
|
|
|
-export function * saga () {
|
|
|
+export function* saga() {
|
|
|
console.log('SMS saga started.')
|
|
|
yield takeEvery('SMS/SEND_REQUEST', sendSMS)
|
|
|
yield takeEvery('SMS/CREDIT_REQUEST', getCredit)
|