add uploading state of image

This commit is contained in:
Sinclair Chen 2022-07-06 20:21:37 -07:00
parent ac436480f5
commit 29a8a77ead
3 changed files with 27 additions and 17 deletions

View File

@ -7,6 +7,7 @@ import clsx from 'clsx'
import { useEffect } from 'react' import { useEffect } from 'react'
import { Linkify } from './linkify' import { Linkify } from './linkify'
import { uploadImage } from 'web/lib/firebase/storage' import { uploadImage } from 'web/lib/firebase/storage'
import { useMutation } from 'react-query'
const LINE_HEIGHT = 2 const LINE_HEIGHT = 2
@ -23,6 +24,10 @@ export function useTextEditor(props: {
const rowsClass = rows && `box-content min-h-[${LINE_HEIGHT * rows}em]` const rowsClass = rows && `box-content min-h-[${LINE_HEIGHT * rows}em]`
const upload = useMutation((files: File[]) =>
Promise.all(files.map((file) => uploadImage('default', file)))
)
const editor = useEditor({ const editor = useEditor({
editorProps: { editorProps: {
attributes: { attributes: {
@ -38,18 +43,16 @@ export function useTextEditor(props: {
} }
event.preventDefault() event.preventDefault()
;(async () => { upload.mutate(files, {
// TODO: show loading state, compress large images onSuccess: (urls) => {
const urls = await Promise.all( let trans = view.state.tr
files.map((file) => uploadImage('default', file)) urls.forEach((src: any) => {
) const node = view.state.schema.nodes.image.create({ src })
let trans = view.state.tr trans = trans.insert(view.state.selection.to, node)
urls.forEach((src: any) => { })
const node = view.state.schema.nodes.image.create({ src }) view.dispatch(trans)
trans = trans.insert(view.state.selection.to, node) },
}) })
view.dispatch(trans)
})()
}, },
}, },
extensions: [ extensions: [
@ -65,7 +68,7 @@ export function useTextEditor(props: {
editor?.setEditable(!disabled) editor?.setEditable(!disabled)
}, [editor, disabled]) }, [editor, disabled])
return editor return { editor, upload }
} }
function RichContent(props: { content: JSONContent }) { function RichContent(props: { content: JSONContent }) {

View File

@ -7,6 +7,7 @@ import {
const storage = getStorage() const storage = getStorage()
// TODO: compress large images
export const uploadImage = async ( export const uploadImage = async (
username: string, username: string,
file: File, file: File,

View File

@ -188,7 +188,7 @@ export function NewContract(props: {
? `e.g. This question resolves to "YES" if they receive the majority of votes...` ? `e.g. This question resolves to "YES" if they receive the majority of votes...`
: `e.g. I will choose the answer according to...` : `e.g. I will choose the answer according to...`
const editor = useTextEditor({ const { editor, upload } = useTextEditor({
rows: 3, rows: 3,
max: MAX_DESCRIPTION_LENGTH, max: MAX_DESCRIPTION_LENGTH,
placeholder: descriptionPlaceholder, placeholder: descriptionPlaceholder,
@ -442,12 +442,18 @@ export function NewContract(props: {
<Spacer h={6} /> <Spacer h={6} />
<div className="form-control mb-1 items-start"> <div className="form-control mb-1 items-start gap-1">
<label className="label mb-1 gap-2"> <label className="label gap-2">
<span className="mb-1">Description</span> <span className="mb-1">Description</span>
<InfoTooltip text="Optional. Describe how you will resolve this question." /> <InfoTooltip text="Optional. Describe how you will resolve this question." />
</label> </label>
<EditorContent editor={editor} className="w-full" /> <EditorContent editor={editor} className="w-full" />
{upload.isLoading && (
<span className="text-xs">Uploading image...</span>
)}
{upload.isError && (
<span className="text-error text-xs">Error uploading image :(</span>
)}
</div> </div>
<Spacer h={6} /> <Spacer h={6} />
@ -484,7 +490,7 @@ export function NewContract(props: {
'btn btn-primary normal-case', 'btn btn-primary normal-case',
isSubmitting && 'loading disabled' isSubmitting && 'loading disabled'
)} )}
disabled={isSubmitting || !isValid} disabled={isSubmitting || !isValid || upload.isLoading}
onClick={(e) => { onClick={(e) => {
e.preventDefault() e.preventDefault()
submit() submit()