From b4b549459c8760df9e6efb002f0822c48295d7b1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Arda=20Nak=C4=B1=C5=9F=C3=A7=C4=B1?= Date: Tue, 2 Aug 2022 10:44:17 +0300 Subject: [PATCH] Change collection info image to file selection (#15) --- components/CollectionInfo.tsx | 52 +++++++++++++++++++++++++++++------ 1 file changed, 44 insertions(+), 8 deletions(-) diff --git a/components/CollectionInfo.tsx b/components/CollectionInfo.tsx index 818cd8e..010b387 100644 --- a/components/CollectionInfo.tsx +++ b/components/CollectionInfo.tsx @@ -1,10 +1,13 @@ +import clsx from 'clsx' import { Button } from 'components/Button' import { FormControl } from 'components/FormControl' import { FormGroup } from 'components/FormGroup' import { useInputState, useNumberInputState } from 'components/forms/FormInput.hooks' import { InputDateTime } from 'components/InputDateTime' +import type { ChangeEvent } from 'react' import React, { useState } from 'react' import useCollapse from 'react-collapsed' +import { toast } from 'react-hot-toast' import { useMutation } from 'react-query' import { Conditional } from './Conditional' @@ -23,6 +26,8 @@ export const CollectionInfo = () => { const [wlendDate, setwlEndDate] = useState(undefined) const [whitelistArray, setWhitelistArray] = useState([]) + const [coverImage, setCoverImage] = useState(null) + const nameState = useInputState({ id: 'name', name: 'name', @@ -37,13 +42,6 @@ export const CollectionInfo = () => { subtitle: '', }) - const imageState = useInputState({ - id: 'image', - name: 'image', - title: 'Image', - subtitle: '', - }) - const externalImageState = useInputState({ id: 'externalImage', name: 'externalImage', @@ -130,6 +128,22 @@ export const CollectionInfo = () => { setWhitelistArray(data) } + const selectCoverImage = (event: ChangeEvent) => { + if (event.target.files === null) return toast.error('Error selecting cover image') + if (event.target.files.length === 0) { + setCoverImage(null) + return toast.error('No files selected.') + } + const reader = new FileReader() + reader.onload = (e) => { + if (!e.target?.result) return toast.error('Error parsing file.') + if (!event.target.files) return toast.error('No files selected.') + const imageFile = new File([e.target.result], event.target.files[0].name, { type: 'image/jpg' }) + setCoverImage(imageFile) + } + reader.readAsArrayBuffer(event.target.files[0]) + } + return (
@@ -137,7 +151,29 @@ export const CollectionInfo = () => { - +
+ + + {coverImage !== null && ( +
+ cover-preview +
+ )} +