Pinafore-Web-Client-Frontend/src/routes/_actions/media.js

50 lines
1.6 KiB
JavaScript
Raw Normal View History

2018-03-02 06:21:49 +01:00
import { store } from '../_store/store'
import { uploadMedia } from '../_api/media'
2018-12-23 00:37:51 +01:00
import { toast } from '../_components/toast/toast'
2018-03-03 20:26:10 +01:00
import { scheduleIdleTask } from '../_utils/scheduleIdleTask'
import { setCachedMediaFile } from '../_utils/mediaUploadFileCache'
2018-03-02 06:21:49 +01:00
export async function doMediaUpload (realm, file) {
2019-08-03 22:49:37 +02:00
const { currentInstance, accessToken } = store.get()
store.set({ uploadingMedia: true })
2018-03-02 06:21:49 +01:00
try {
2019-08-03 22:49:37 +02:00
const response = await uploadMedia(currentInstance, accessToken, file)
const composeMedia = store.getComposeData(realm, 'media') || []
if (composeMedia.length === 4) {
throw new Error('Only 4 media max are allowed')
}
await setCachedMediaFile(response.id, file)
2018-03-03 23:15:50 +01:00
composeMedia.push({
2018-03-02 06:21:49 +01:00
data: response,
file: { name: file.name },
description: ''
2018-03-02 06:21:49 +01:00
})
2018-03-03 23:51:48 +01:00
store.setComposeData(realm, {
media: composeMedia
2018-03-03 23:51:48 +01:00
})
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-02 06:21:49 +01:00
}
2018-03-03 02:54:38 +01:00
}
2018-03-03 06:55:04 +01:00
export function deleteMedia (realm, i) {
2019-08-03 22:49:37 +02:00
const composeMedia = store.getComposeData(realm, 'media')
composeMedia.splice(i, 1)
2018-03-03 23:51:48 +01:00
store.setComposeData(realm, {
media: composeMedia
2018-03-03 23:51:48 +01:00
})
if (!composeMedia.length) {
const contentWarningShown = store.getComposeData(realm, 'contentWarningShown')
const contentWarning = store.getComposeData(realm, 'contentWarning')
store.setComposeData(realm, {
sensitive: contentWarningShown && contentWarning // reset sensitive if the last media is deleted
})
}
2018-03-03 20:26:10 +01:00
scheduleIdleTask(() => store.save())
2018-03-03 06:55:04 +01:00
}