import React, { useRef, useState } from 'react' import Router from 'next/router' import Layout from '../components/layout' import { withAuthSync } from '../utils/auth' import { callAPI } from '../utils/api' const Profile = props => { const { users } = props const check = useRef(undefined) const [photo, setPhoto] = useState(undefined) const [price, setPrice] = useState('') const [description, setDescription] = useState('') const [debtors, setDebtors] = useState(users.reduce((acc, users) => { acc[users.name] = true return acc }, {})) const removeUpload = () => { setPhoto(false) } const handlePhoto = e => { const reader = new FileReader() reader.onload = e => { const converted = Buffer.from(e.target.result).toString('base64') check.current.src = `data:image/jpeg;base64,${converted}` setPhoto(converted) } reader.readAsArrayBuffer(e.target.files[0]) } const submit = async (e) => { e.preventDefault() console.log(e.files) const selectedDebtors = Object.entries(debtors) .filter(([name, selected]) => selected) .map(([name, selected]) => name) await callAPI(null, { url: `/api/purchases`, method: 'post', data: { price: parseFloat(price), debtors: selectedDebtors, description, }, }) Router.push('/') } return

Add New

setPrice(e.target.value)} type={'number'} min={0} step={0.01} />
setDescription(e.target.value)} />
{photo ? :
Upload a Photo
}
{users.map(({ name }, i) => )}
file upload check {/* language=CSS */}
} Profile.getInitialProps = async ctx => ({ users: await callAPI(ctx, { url: `/api/users/` }), }) export default withAuthSync(Profile)