Last active
October 7, 2017 01:52
-
-
Save WilliamAnputra/10f4dff3ada5b937f3e2b1d2e0a27b46 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const sendAttachment = function* sendAttachment(action) { | |
const sendAttachmentUrl = `http://${BASE_URL}.bamms.co/api/*****/*****/*****/*****`; | |
const attachment = require('../images/abc.jpg'); | |
const data = new FormData(); | |
data.append('inquiry_id', '265'); | |
data.append('created_at', '2017-10-05 13:01:22'); | |
data.append('note', 'halo'); | |
data.append('attachment', { | |
uri: attachment.toString(), | |
type: 'image/jpeg', | |
name: 'testPhotoName' | |
}); | |
let userAccessToken = ''; | |
yield AsyncStorage.getItem(ACCESS_TOKEN, (err, result) => { | |
userAccessToken = result; | |
return userAccessToken; | |
}); | |
if (userAccessToken) { | |
try { | |
const { response, timeout } = yield race({ | |
response: fetch(sendAttachmentUrl, { | |
method: 'post', | |
body: data, | |
headers: { | |
Authorization: `Bearer ${userAccessToken}`, | |
Accept: 'application/json' | |
} | |
}), | |
timeout: delay(60000) | |
}); | |
if (timeout) { | |
return yield put({ | |
type: constants.REQUEST_TIME_OUT_SEND_ATTACHMENT, | |
payload: { timeout: 'TIMEOUT' } | |
}); | |
} | |
console.log('resposne is', response); | |
// if there is a response | |
if (response) { | |
if (response.data.success) { | |
return yield put({ | |
type: constants.SEND_ATTACHMENT_SUCCESS, | |
payload: response | |
}); | |
} | |
return yield put({ | |
type: constants.SEND_ATTACHMENT_FAIL, | |
payload: { error: 'send attachment error' } | |
}); | |
} | |
} catch (e) { | |
console.log(e); | |
return yield put({ | |
type: constants.SEND_ATTACHMENT_FAIL, | |
payload: { error: 'send attachment error' } | |
}); | |
} | |
} | |
return null; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment