import React, {useState, useEffect, useMemo, useRef} from 'react' import {DateTime} from 'luxon' import axios from 'axios' import Link from 'next/link' import {Button} from '@rmwc/button' import ActionBar from '~/components/admin/actionBar' import {Icon} from '@rmwc/icon' import styles from '~/styles/imageGallery.module.css' EditImages.getInitialProps = async ({ctx: {axios, query: {slug}}}) => { const {data: item} = await axios.get(`/api/items/by-slug/${slug}`) return {item} } export default function EditImages({item: _item}) { const [item, setItem] = useState(_item) const fileRef = useRef() // Only re-sort images whenever item changes const images = useMemo(() => item.images.slice(0).sort( (a,b) => DateTime.fromISO(a.date_uploaded).diff(DateTime.fromISO(b.date_uploaded)) ), [item]) async function selectImage(ev){ if(ev) ev.preventDefault() fileRef.current.click() } useEffect(()=>{ fileRef.current.addEventListener('change', uploadImage) return ()=>fileRef.current.removeEventListener('change', uploadImage) }, []) async function uploadImage(ev){ if(ev) ev.preventDefault() const image = await readFileAsync(ev.target) const {data: updatedItem} = await axios.post(`/api/items/${item.uuid}/images`, {image}) setItem(updatedItem) } function handleSetFeatured(image){ return async (ev) => { if(ev) ev.preventDefault(); const {data: updatedItem} = await axios.post(`/api/images/${image.uuid}/featured`) setItem(updatedItem) } } function handleRemoveImage(image){ return async (ev) => { if(ev) ev.preventDefault() const {data: updatedItem} = await axios.delete(`/api/images/${image.uuid}`) setItem(updatedItem) } } return ( <>
{images.map(image => (
Uploaded {DateTime.fromISO(image.date_uploaded).toFormat('DD')} by {image.uploader.email}
))} {!images.length ? ( <>

No images added for this item.

) : (

)}
) } function readFileAsync(fileInput){ const reader = new FileReader() return new Promise((resolve, reject) => { reader.addEventListener('load', () => { resolve(reader.result) }) reader.addEventListener('error', reject) reader.addEventListener('abort', reject) reader.readAsDataURL(fileInput.files[0]) }) }