pinafore/routes/_actions/media.js
Nolan Lawson 8d5690d63d
remove get() with string from Svelte calls (#169)
* remove get() with string pt 1

* remove get() with string pt 2

* fix typo

* fix some null exceptions in get()

* fixup code style
2018-04-19 09:37:05 -07:00

50 lines
1.5 KiB
JavaScript

import { store } from '../_store/store'
import { uploadMedia } from '../_api/media'
import { toast } from '../_utils/toast'
import { scheduleIdleTask } from '../_utils/scheduleIdleTask'
export async function doMediaUpload (realm, file) {
let { currentInstance, accessToken } = store.get()
store.set({uploadingMedia: true})
try {
let response = await uploadMedia(currentInstance, accessToken, file)
let composeMedia = store.getComposeData(realm, 'media') || []
composeMedia.push({
data: response,
file: { name: file.name }
})
let composeText = store.getComposeData(realm, 'text') || ''
composeText += ' ' + response.text_url
store.setComposeData(realm, {
media: composeMedia,
text: composeText
})
scheduleIdleTask(() => store.save())
} catch (e) {
console.error(e)
toast.say('Failed to upload media: ' + (e.message || ''))
} finally {
store.set({uploadingMedia: false})
}
}
export function deleteMedia (realm, i) {
let composeMedia = store.getComposeData(realm, 'media')
let deletedMedia = composeMedia.splice(i, 1)[0]
let composeText = store.getComposeData(realm, 'text') || ''
composeText = composeText.replace(' ' + deletedMedia.data.text_url, '')
let mediaDescriptions = store.getComposeData(realm, 'mediaDescriptions') || []
if (mediaDescriptions[i]) {
mediaDescriptions[i] = null
}
store.setComposeData(realm, {
media: composeMedia,
text: composeText,
mediaDescriptions: mediaDescriptions
})
scheduleIdleTask(() => store.save())
}