pinafore/routes/_actions/media.js

45 lines
1.4 KiB
JavaScript
Raw Normal View History

2018-03-02 06:21:49 +01:00
import { store } from '../_store/store'
import { uploadMedia } from '../_api/media'
import { toast } from '../_utils/toast'
2018-03-03 20:26:10 +01:00
import { scheduleIdleTask } from '../_utils/scheduleIdleTask'
2018-03-02 06:21:49 +01:00
export async function doMediaUpload (realm, file) {
2018-03-02 06:21:49 +01:00
let instanceName = store.get('currentInstance')
let accessToken = store.get('accessToken')
store.set({uploadingMedia: true})
try {
let response = await uploadMedia(instanceName, accessToken, file)
2018-03-03 23:15:50 +01:00
let composeMedia = store.getComposeData(realm, 'media') || []
composeMedia.push({
2018-03-02 06:21:49 +01:00
data: response,
2018-03-03 23:15:50 +01:00
file: { name: file.name }
2018-03-02 06:21:49 +01:00
})
2018-03-03 23:51:48 +01:00
let composeText = store.getComposeData(realm, 'text') || ''
composeText += ' ' + response.text_url
store.setComposeData(realm, {
media: composeMedia,
text: composeText
})
2018-03-03 20:26:10 +01:00
scheduleIdleTask(() => store.save())
2018-03-02 06:21:49 +01:00
} catch (e) {
console.error(e)
toast.say('Failed to upload media: ' + (e.message || ''))
} finally {
store.set({uploadingMedia: false})
}
2018-03-03 02:54:38 +01:00
}
2018-03-03 06:55:04 +01:00
export function deleteMedia (realm, i) {
2018-03-03 23:15:50 +01:00
let composeMedia = store.getComposeData(realm, 'media')
let deletedMedia = composeMedia.splice(i, 1)[0]
2018-03-03 20:26:10 +01:00
2018-03-03 23:51:48 +01:00
let composeText = store.getComposeData(realm, 'text') || ''
composeText = composeText.replace(' ' + deletedMedia.data.text_url, '')
2018-03-03 23:51:48 +01:00
store.setComposeData(realm, {
media: composeMedia,
text: composeText
})
2018-03-03 20:26:10 +01:00
scheduleIdleTask(() => store.save())
2018-03-03 06:55:04 +01:00
}