Fix Attachments spinner

Signed-off-by: Anna No <anna.no@xored.com>
This commit is contained in:
Anna No 2022-04-13 17:46:19 +07:00
parent 1d5bebcd4c
commit d7bbf86b97
No known key found for this signature in database
GPG Key ID: 08C11FFC23177C87
3 changed files with 54 additions and 43 deletions

View File

@ -2,7 +2,7 @@
import { Class, Doc, Ref, Space } from '@anticrm/core' import { Class, Doc, Ref, Space } from '@anticrm/core'
import { getClient } from '@anticrm/presentation' import { getClient } from '@anticrm/presentation'
import { CircleButton, IconAdd } from '@anticrm/ui' import { CircleButton, IconAdd } from '@anticrm/ui'
import { createAttachment } from '../utils' import { createAttachments } from '../utils'
export let loading: number = 0 export let loading: number = 0
export let inputFile: HTMLInputElement export let inputFile: HTMLInputElement
@ -10,16 +10,18 @@
export let objectClass: Ref<Class<Doc>> export let objectClass: Ref<Class<Doc>>
export let objectId: Ref<Doc> export let objectId: Ref<Doc>
export let space: Ref<Space> export let space: Ref<Space>
const client = getClient() const client = getClient()
function fileSelected() { async function fileSelected() {
const list = inputFile.files const list = inputFile.files
if (list === null || list.length === 0) return if (list === null || list.length === 0) return
for (let index = 0; index < list.length; index++) {
const file = list.item(index) loading++
if (file !== null) createAttachment(client, file, loading, { objectClass, objectId, space }) try {
await createAttachments(client, list, {objectClass, objectId, space})
} finally {
loading--
} }
} }
@ -31,13 +33,9 @@
<div> <div>
{#if $$slots.control} {#if $$slots.control}
<slot name="control" click={openFile}/> <slot name="control" click={openFile} />
{:else} {:else}
<CircleButton <CircleButton icon={IconAdd} size="small" selected on:click={openFile} />
icon={IconAdd}
size="small"
selected
on:click={openFile} />
{/if} {/if}
<input <input
bind:this={inputFile} bind:this={inputFile}

View File

@ -17,25 +17,28 @@
import { Class, Doc, Ref, Space } from '@anticrm/core' import { Class, Doc, Ref, Space } from '@anticrm/core'
import { getClient } from '@anticrm/presentation' import { getClient } from '@anticrm/presentation'
import { createAttachment } from '../utils' import { createAttachments } from '../utils'
export let loading: number = 0 export let loading: number = 0
export let objectClass: Ref<Class<Doc>> export let objectClass: Ref<Class<Doc>>
export let objectId: Ref<Doc> export let objectId: Ref<Doc>
export let space: Ref<Space> export let space: Ref<Space>
export let canDrop: ((e: DragEvent) => boolean) | undefined = undefined export let canDrop: ((e: DragEvent) => boolean) | undefined = undefined
export let dragover = false export let dragover = false
const client = getClient() const client = getClient()
function fileDrop(e: DragEvent) { async function fileDrop(e: DragEvent) {
dragover = false dragover = false
const list = e.dataTransfer?.files const list = e.dataTransfer?.files
if (list === undefined || list.length === 0) return if (list === undefined || list.length === 0) return
for (let index = 0; index < list.length; index++) {
const file = list.item(index) loading++
if (file !== null) createAttachment(client, file, loading, { objectClass, objectId, space }) try {
await createAttachments(client, list, {objectClass, objectId, space})
} finally {
loading--
} }
} }

View File

@ -14,13 +14,13 @@
// limitations under the License. // limitations under the License.
// //
import type { Class, Doc, Ref, Space, TxOperations as Client } from '@anticrm/core' import type { Class, Doc, Ref, Space, TxOperations as Client } from '@anticrm/core'
import login from '@anticrm/login' import login from '@anticrm/login'
import { getMetadata, setPlatformStatus, unknownError } from '@anticrm/platform' import { getMetadata, setPlatformStatus, unknownError } from '@anticrm/platform'
import attachment from './plugin' import attachment from './plugin'
export async function uploadFile (file: File, opts?: { space: Ref<Space>, attachedTo: Ref<Doc> }): Promise<string> { export async function uploadFile(file: File, opts?: { space: Ref<Space>; attachedTo: Ref<Doc> }): Promise<string> {
const uploadUrl = getMetadata(login.metadata.UploadUrl) const uploadUrl = getMetadata(login.metadata.UploadUrl)
if (uploadUrl === undefined) { if (uploadUrl === undefined) {
@ -30,12 +30,16 @@ export async function uploadFile (file: File, opts?: { space: Ref<Space>, attach
const data = new FormData() const data = new FormData()
data.append('file', file) data.append('file', file)
const params = opts !== undefined const params =
? [['space', opts.space], ['attachedTo', opts.attachedTo]] opts !== undefined
.filter((x): x is [string, Ref<any>] => x[1] !== undefined) ? [
.map(([name, value]) => `${name}=${value}`) ['space', opts.space],
.join('&') ['attachedTo', opts.attachedTo]
: '' ]
.filter((x): x is [string, Ref<any>] => x[1] !== undefined)
.map(([name, value]) => `${name}=${value}`)
.join('&')
: ''
const suffix = params === '' ? params : `?${params}` const suffix = params === '' ? params : `?${params}`
const url = `${uploadUrl}${suffix}` const url = `${uploadUrl}${suffix}`
@ -54,7 +58,7 @@ export async function uploadFile (file: File, opts?: { space: Ref<Space>, attach
return await resp.text() return await resp.text()
} }
export async function deleteFile (id: string): Promise<void> { export async function deleteFile(id: string): Promise<void> {
const uploadUrl = getMetadata(login.metadata.UploadUrl) const uploadUrl = getMetadata(login.metadata.UploadUrl)
const url = `${uploadUrl as string}?file=${id}` const url = `${uploadUrl as string}?file=${id}`
@ -70,27 +74,33 @@ export async function deleteFile (id: string): Promise<void> {
} }
} }
export async function createAttachment (client: Client, file: File, loading: number, attachTo: {objectClass: Ref<Class<Doc>>, space: Ref<Space>, objectId: Ref<Doc>}) { export async function createAttachments(
loading++ client: Client,
const {objectClass, objectId, space} = attachTo list: FileList,
attachTo: { objectClass: Ref<Class<Doc>>; space: Ref<Space>; objectId: Ref<Doc> }
) {
const { objectClass, objectId, space } = attachTo
try { try {
const uuid = await uploadFile(file, { space, attachedTo: objectId }) for (let index = 0; index < list.length; index++) {
console.log('uploaded file uuid', uuid) const file = list.item(index)
client.addCollection(attachment.class.Attachment, space, objectId, objectClass, 'attachments', { if (file !== null) {
name: file.name, const uuid = await uploadFile(file, { space, attachedTo: objectId })
file: uuid, console.log('uploaded file uuid', uuid)
type: file.type, client.addCollection(attachment.class.Attachment, space, objectId, objectClass, 'attachments', {
size: file.size, name: file.name,
lastModified: file.lastModified file: uuid,
}) type: file.type,
size: file.size,
lastModified: file.lastModified
})
}
}
} catch (err: any) { } catch (err: any) {
setPlatformStatus(unknownError(err)) setPlatformStatus(unknownError(err))
} finally {
loading--
} }
} }
export function getType (type: string): 'image' | 'video' | 'audio' | 'pdf' | 'other' { export function getType(type: string): 'image' | 'video' | 'audio' | 'pdf' | 'other' {
if (type.startsWith('image/')) { if (type.startsWith('image/')) {
return 'image' return 'image'
} }