Update update-avatar.ts
This commit is contained in:
parent
8e5ce7f8e3
commit
ef43721e32
|
@ -3,9 +3,9 @@ import { apiUrl } from '../../config';
|
||||||
import CropWindow from '../views/components/crop-window.vue';
|
import CropWindow from '../views/components/crop-window.vue';
|
||||||
import ProgressDialog from '../views/components/progress-dialog.vue';
|
import ProgressDialog from '../views/components/progress-dialog.vue';
|
||||||
|
|
||||||
export default (os: OS) => (cb, file = null) => {
|
export default (os: OS) => {
|
||||||
const fileSelected = file => {
|
|
||||||
|
|
||||||
|
const cropImage = file => new Promise((resolve, reject) => {
|
||||||
const w = os.new(CropWindow, {
|
const w = os.new(CropWindow, {
|
||||||
image: file,
|
image: file,
|
||||||
title: '%i18n:desktop.avatar-crop-title%',
|
title: '%i18n:desktop.avatar-crop-title%',
|
||||||
|
@ -19,27 +19,29 @@ export default (os: OS) => (cb, file = null) => {
|
||||||
|
|
||||||
os.api('drive/folders/find', {
|
os.api('drive/folders/find', {
|
||||||
name: '%i18n:desktop.avatar%'
|
name: '%i18n:desktop.avatar%'
|
||||||
}).then(iconFolder => {
|
}).then(avatarFolder => {
|
||||||
if (iconFolder.length === 0) {
|
if (avatarFolder.length === 0) {
|
||||||
os.api('drive/folders/create', {
|
os.api('drive/folders/create', {
|
||||||
name: '%i18n:desktop.avatar%'
|
name: '%i18n:desktop.avatar%'
|
||||||
}).then(iconFolder => {
|
}).then(iconFolder => {
|
||||||
upload(data, iconFolder);
|
resolve(upload(data, iconFolder));
|
||||||
});
|
});
|
||||||
} else {
|
} else {
|
||||||
upload(data, iconFolder[0]);
|
resolve(upload(data, avatarFolder[0]));
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
w.$once('skipped', () => {
|
w.$once('skipped', () => {
|
||||||
set(file);
|
resolve(file);
|
||||||
});
|
});
|
||||||
|
|
||||||
document.body.appendChild(w.$el);
|
w.$once('cancelled', reject);
|
||||||
};
|
|
||||||
|
|
||||||
const upload = (data, folder) => {
|
document.body.appendChild(w.$el);
|
||||||
|
});
|
||||||
|
|
||||||
|
const upload = (data, folder) => new Promise((resolve, reject) => {
|
||||||
const dialog = os.new(ProgressDialog, {
|
const dialog = os.new(ProgressDialog, {
|
||||||
title: '%i18n:desktop.uploading-avatar%'
|
title: '%i18n:desktop.uploading-avatar%'
|
||||||
});
|
});
|
||||||
|
@ -52,18 +54,19 @@ export default (os: OS) => (cb, file = null) => {
|
||||||
xhr.onload = e => {
|
xhr.onload = e => {
|
||||||
const file = JSON.parse((e.target as any).response);
|
const file = JSON.parse((e.target as any).response);
|
||||||
(dialog as any).close();
|
(dialog as any).close();
|
||||||
set(file);
|
resolve(file);
|
||||||
};
|
};
|
||||||
|
xhr.onerror = reject;
|
||||||
|
|
||||||
xhr.upload.onprogress = e => {
|
xhr.upload.onprogress = e => {
|
||||||
if (e.lengthComputable) (dialog as any).update(e.loaded, e.total);
|
if (e.lengthComputable) (dialog as any).update(e.loaded, e.total);
|
||||||
};
|
};
|
||||||
|
|
||||||
xhr.send(data);
|
xhr.send(data);
|
||||||
};
|
});
|
||||||
|
|
||||||
const set = file => {
|
const setAvatar = file => {
|
||||||
os.api('i/update', {
|
return os.api('i/update', {
|
||||||
avatarId: file.id
|
avatarId: file.id
|
||||||
}).then(i => {
|
}).then(i => {
|
||||||
os.store.commit('updateIKeyValue', {
|
os.store.commit('updateIKeyValue', {
|
||||||
|
@ -83,18 +86,21 @@ export default (os: OS) => (cb, file = null) => {
|
||||||
}]
|
}]
|
||||||
});
|
});
|
||||||
|
|
||||||
if (cb) cb(i);
|
return i;
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
|
||||||
if (file) {
|
return (file = null) => {
|
||||||
fileSelected(file);
|
const selectedFile = file
|
||||||
} else {
|
? Promise.resolve(file)
|
||||||
os.apis.chooseDriveFile({
|
: os.apis.chooseDriveFile({
|
||||||
multiple: false,
|
multiple: false,
|
||||||
title: '%fa:image% %i18n:desktop.choose-avatar%'
|
title: '%fa:image% %i18n:desktop.choose-avatar%'
|
||||||
}).then(file => {
|
|
||||||
fileSelected(file);
|
|
||||||
});
|
});
|
||||||
}
|
|
||||||
|
return selectedFile
|
||||||
|
.then(cropImage)
|
||||||
|
.then(setAvatar)
|
||||||
|
.catch(err => err && console.warn(err));
|
||||||
|
};
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in New Issue