Skip to content

Commit

Permalink
feat(create listing): add default photo and tags, implement drop feat…
Browse files Browse the repository at this point in the history
…ure (#41)

Implement default photo (kimitzu logo) and tags (Kimitzu).
Implement drag and drop feature.

fix #35
  • Loading branch information
24thsaint committed Jan 10, 2020
1 parent 31e1104 commit 79cc932
Show file tree
Hide file tree
Showing 5 changed files with 83 additions and 46 deletions.
7 changes: 7 additions & 0 deletions src/components/Thumbnav/ThumbnavSlideshow.css
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,10 @@
justify-content: center;
background-color: #0000;
}
.thumbnav-drag-attempt {
border: dodgerblue 5px dashed;
}
.thumbnav-image-preview {
object-fit: contain;
height: 100%;
}
75 changes: 61 additions & 14 deletions src/components/Thumbnav/ThumbnavSlideshow.tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import React, { useState } from 'react'
import React, { useEffect, useState } from 'react'

import { Button } from '../Button'
import { DropArea } from '../Upload'

import ImageUploaderInstance from '../../utils/ImageUploaderInstance'

Expand All @@ -21,6 +20,35 @@ const ThumbnavSlideshow = ({ images, onChange }: Props) => {

const [photos, setPhotos] = useState(images)
const [selectedIndex, setSelectedIndex] = useState(0)
const [dropStyle, setDropStyle] = useState('')
const [dropRef, setDropRef] = useState(React.createRef<HTMLDivElement>())

useEffect(() => {
const handleDragEvents = evt => {
evt.preventDefault()
evt.stopPropagation()

if (['dragenter', 'dragover'].includes(evt.type)) {
setDropStyle('thumbnav-drag-attempt')
}

if (['dragleave', 'drop'].includes(evt.type)) {
setDropStyle('')
}

if (evt.type === 'drop') {
const dataTransfer = evt.dataTransfer
if (dataTransfer && dataTransfer.files.length > 0) {
onImageOpen(dataTransfer.files)
}
}
}

dropRef.current!.addEventListener('dragenter', handleDragEvents, false)
dropRef.current!.addEventListener('dragover', handleDragEvents, false)
dropRef.current!.addEventListener('dragleave', handleDragEvents, false)
dropRef.current!.addEventListener('drop', handleDragEvents, false)
}, [])

const handleDelete = () => {
images.splice(selectedIndex, 1)
Expand All @@ -30,8 +58,7 @@ const ThumbnavSlideshow = ({ images, onChange }: Props) => {
onChange(newPhotos)
}

const onImageOpen = async (event: React.ChangeEvent<HTMLInputElement>) => {
const imageFiles = event.target.files
const onImageOpen = async (imageFiles: FileList) => {
const base64ImageFiles: Array<Promise<string>> = []

if (!imageFiles) {
Expand All @@ -51,23 +78,37 @@ const ThumbnavSlideshow = ({ images, onChange }: Props) => {
}

return (
<div>
<div className="uk-position-relative" data-uk-slideshow="animation: fade">
<div ref={dropRef}>
<div className={`uk-position-relative ${dropStyle}`} data-uk-slideshow="animation: fade">
<ul className="uk-slideshow-items">
{photos.length === 0 ? (
<DropArea
placeholder={dropArea.placeholder}
selectLabel={dropArea.selectLabel}
onImageOpen={onImageOpen}
/>
<div className="uk-placeholder uk-flex uk-flex-middle uk-flex-center">
<span data-uk-icon="icon: cloud-upload" />
<span className="uk-text-middle">{dropArea.placeholder}</span>
<div className="color-primary" data-uk-form-custom>
<input
id="image-upload"
type="file"
multiple
onChange={evt => {
if (evt.target.files) {
onImageOpen(evt.target.files)
}
}}
accept="image/*"
/>
&nbsp;{dropArea.selectLabel}
</div>
</div>
) : (
photos.map((image: string) => (
<li key={image} className="img-li-cont">
<img src={image} alt="thumbnail" />
<img src={image} alt="thumbnail" className="thumbnav-image-preview" />
</li>
))
)}
</ul>

<div className="uk-position-bottom-center uk-position-small">
<ul className="uk-thumbnav">
{photos.map((image: string, index: number) => (
Expand Down Expand Up @@ -107,7 +148,11 @@ const ThumbnavSlideshow = ({ images, onChange }: Props) => {
id="file-upload"
type="file"
multiple
onChange={onImageOpen}
onChange={evt => {
if (evt.target.files) {
onImageOpen(evt.target.files)
}
}}
accept="image/*"
hidden
/>
Expand All @@ -116,7 +161,9 @@ const ThumbnavSlideshow = ({ images, onChange }: Props) => {
onClick={evt => {
evt.preventDefault()
const fileUploadHandler = document.getElementById('file-upload')
fileUploadHandler!.click()
if (fileUploadHandler) {
fileUploadHandler!.click()
}
}}
>
<span uk-icon="icon: plus" />
Expand Down
28 changes: 0 additions & 28 deletions src/components/Upload/DropArea.tsx

This file was deleted.

3 changes: 0 additions & 3 deletions src/components/Upload/index.ts

This file was deleted.

16 changes: 15 additions & 1 deletion src/pages/CreateListing.tsx
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import Axios from 'axios'
import React, { Component, ReactNode } from 'react'
import { Prompt, RouteComponentProps } from 'react-router'
import { SideMenuWithContentCard } from '../components/Card'
Expand All @@ -21,6 +22,7 @@ import ImageUploaderInstance from '../utils/ImageUploaderInstance'
import NestedJSONUpdater from '../utils/NestedJSONUpdater'

import { Link } from 'react-router-dom'
import config from '../config'
import { localeInstance } from '../i18n'

const cryptoCurrenciesConstants = [...CryptoCurrencies()]
Expand Down Expand Up @@ -374,6 +376,17 @@ class CreateListing extends Component<CreateListingProps, CreateListingState> {
})

const newImages = this.state.tempImages.filter(image => !image.startsWith('http'))

if (listing.item.images.length <= 0 && this.state.tempImages.length <= 0) {
const defaultLogo = await Axios.get(`${config.host}/icon.png`, { responseType: 'blob' })
const defaultLogoBase64 = await ImageUploaderInstance.convertToBase64(defaultLogo.data)
newImages.push(defaultLogoBase64)
}

if (listing.item.tags.length <= 0) {
listing.item.tags.push('Kimitzu')
}

const imageUpload = newImages.map(base64Image => ImageUploaderInstance.uploadImage(base64Image))
const updateOldImages = this.state.listing.item.images.filter(oldElements => {
return this.state.tempImages.find(updatedElements => {
Expand All @@ -385,6 +398,7 @@ class CreateListing extends Component<CreateListingProps, CreateListingState> {
try {
const images = await Promise.all(imageUpload)
listing.item.images = [...updateOldImages, ...images]

if (this.state.isListingNew) {
await listing.save()
window.UIkit.notification(this.locale.listingForm.notifications.addSuccess, {
Expand All @@ -404,7 +418,7 @@ class CreateListing extends Component<CreateListingProps, CreateListingState> {
window.location.hash = '/'
}, 2000)
} catch (e) {
window.UIkit.notification(e.message, {
window.UIkit.notification(e.response.data.reason || e.message, {
status: 'danger',
})
this.setState({
Expand Down

0 comments on commit 79cc932

Please sign in to comment.