using await with image upload, keeping better track of selection

This commit is contained in:
Nouman Tahir 2021-04-02 21:04:03 +05:00
parent 902d913f74
commit 97df283214
2 changed files with 114 additions and 65 deletions

View File

@ -28,9 +28,10 @@ export default async ({ text, selection, setTextAndSelection, item, isImage = nu
} else {
newText = replaceBetween(text, selection, `\n${imagePrefix}[${itemText}](${itemUrl})\n`);
if (isImage) {
const newIndex = newText && newText.indexOf(itemUrl) + 2 + itemUrl.length;
newSelection = {
start: newText && newText.length,
end: newText && newText.length,
start: newIndex,
end: newIndex,
};
} else {
newSelection = {

View File

@ -352,12 +352,7 @@ class EditorContainer extends Component {
});
};
_handleMediaOnSelected = (media) => {
this.setState(
{
isUploading: true,
},
async () => {
_handleMediaOnSelected = async (media) => {
if (media.length > 0) {
for (let index = 0; index < media.length; index++) {
const element = media[index];
@ -366,8 +361,7 @@ class EditorContainer extends Component {
} else {
await this._uploadImage(media);
}
},
);
// For new image api
// const { currentAccount } = this.props;
// const digitPinCode = await getPinCode();
@ -381,10 +375,14 @@ class EditorContainer extends Component {
if (!isLoggedIn) return;
this.setState({
isUploading: true,
});
let sign = await signImage(media, currentAccount, pinCode);
uploadImage(media, currentAccount.name, sign)
.then((res) => {
try {
const res = await uploadImage(media, currentAccount.name, sign);
if (res.data && res.data.url) {
res.data.hash = res.data.url.split('/').pop();
this.setState({
@ -392,8 +390,7 @@ class EditorContainer extends Component {
isUploading: false,
});
}
})
.catch((error) => {
} catch (error) {
console.log(error, error.message);
if (error.toString().includes('code 413')) {
Alert.alert(
@ -433,7 +430,58 @@ class EditorContainer extends Component {
this.setState({
isUploading: false,
});
});
}
// uploadImage(media, currentAccount.name, sign).then((res) => {
// if (res.data && res.data.url) {
// res.data.hash = res.data.url.split('/').pop();
// this.setState({
// uploadedImage: res.data,
// isUploading: false,
// });
// }
// })
// .catch((error) => {
// console.log(error, error.message);
// if (error.toString().includes('code 413')) {
// Alert.alert(
// intl.formatMessage({
// id: 'alert.fail',
// }),
// intl.formatMessage({
// id: 'alert.payloadTooLarge',
// }),
// );
// } else if (error.toString().includes('code 429')) {
// Alert.alert(
// intl.formatMessage({
// id: 'alert.fail',
// }),
// intl.formatMessage({
// id: 'alert.quotaExceeded',
// }),
// );
// } else if (error.toString().includes('code 400')) {
// Alert.alert(
// intl.formatMessage({
// id: 'alert.fail',
// }),
// intl.formatMessage({
// id: 'alert.invalidImage',
// }),
// );
// } else {
// Alert.alert(
// intl.formatMessage({
// id: 'alert.fail',
// }),
// error.message || error.toString(),
// );
// }
// this.setState({
// isUploading: false,
// });
// });
};
_handleMediaOnSelectFailure = (error) => {