From 44549d77ed1263579c989782f86c3faf32de2dd2 Mon Sep 17 00:00:00 2001 From: cupcakearmy Date: Fri, 24 May 2019 15:26:22 +0200 Subject: [PATCH] responsive list performance --- www/components/item.jsx | 54 +++++++++++++++++++++++ www/pages/list.jsx | 95 +++++++++-------------------------------- 2 files changed, 73 insertions(+), 76 deletions(-) create mode 100644 www/components/item.jsx diff --git a/www/components/item.jsx b/www/components/item.jsx new file mode 100644 index 0000000..0e07068 --- /dev/null +++ b/www/components/item.jsx @@ -0,0 +1,54 @@ +import { callAPI } from '../utils/api' +import React from 'react' + + +const Item = ({ id, text, done, refresh }) => { + + const _done = (e) => callAPI(null, { + url: `/api/items/${id}`, + method: 'patch', + data: { + done: e.target.checked, + }, + }).then(refresh) + + const _delete = (e) => callAPI(null, { + url: `/api/items/${id}`, + method: 'delete', + }).then(refresh) + + return + +
+ +
+ + {text} + +
+ + + + +
+ + {/* language=CSS */} + + +} + +export default Item \ No newline at end of file diff --git a/www/pages/list.jsx b/www/pages/list.jsx index 64895d9..ff7aef0 100644 --- a/www/pages/list.jsx +++ b/www/pages/list.jsx @@ -2,93 +2,36 @@ import React, { useEffect, useRef, useState } from 'react' import { withAuthSync } from '../utils/auth' import Layout from '../components/layout' -import { callAPI } from '../utils/api' +import { callAPI, useCallAPI } from '../utils/api' +import Item from '../components/item' -const Item = ({ id, text, done }) => { - - const handleDone = async (e) => { - await callAPI(null, { - url: `/api/items/${id}`, - method: 'patch', - data: { - done: e.target.checked, - }, - }) - window.location.reload() - } - - const handleDelete = async (e) => { - await callAPI(null, { - url: `/api/items/${id}`, - method: 'delete', - }) - window.location.reload() - } - - return - -
- -
- - {text} - -
- - - - -
- - {/* language=CSS */} - - -} - -const List = ({ items }) => { +const List = ({ fetched }) => { const input = useRef(undefined) const [text, setText] = useState('') + const [data, refresh] = useCallAPI({ url: `/api/items` }) useEffect(() => { if (!input.current) return input.current.focus() }, [input]) - const submit = async () => { - await callAPI(null, { - url: `/api/items/`, - method: 'post', - data: { text }, - }) - window.location.reload() - } + const _submit = () => callAPI(null, { + url: `/api/items/`, + method: 'post', + data: { text }, + }).then(refresh) - const deleteAll = async () => { - await callAPI(null, { - url: `/api/items/`, - method: 'delete', - }) - window.location.reload() - } + const _clear = () => callAPI(null, { + url: `/api/items/`, + method: 'delete', + }).then(refresh) + + const items = data || fetched return -
+
{
- {items.map((item, i) => )} + {items.map((item, i) => )}

- + } List.getInitialProps = async ctx => ({ - items: await callAPI(ctx, { url: `/api/items` }), + fetched: await callAPI(ctx, { url: `/api/items` }), }) export default withAuthSync(List) \ No newline at end of file