From 68d3c6391c8fbac36ebfae5d6d66fc353291137f Mon Sep 17 00:00:00 2001 From: Adam Munns Date: Thu, 24 Mar 2022 08:55:52 -0700 Subject: [PATCH] feat: add ux for rename --- packages/website/pages/files.js | 102 +++++++++++++++++++++++++++++++- 1 file changed, 100 insertions(+), 2 deletions(-) diff --git a/packages/website/pages/files.js b/packages/website/pages/files.js index dc029c3413..8e1804a673 100644 --- a/packages/website/pages/files.js +++ b/packages/website/pages/files.js @@ -1,6 +1,6 @@ import { API, getNfts, getToken } from '../lib/api.js' import { useQuery, useQueryClient } from 'react-query' -import { VscQuestion } from 'react-icons/vsc' +import { VscQuestion, VscEdit, VscLoading, VscSave } from 'react-icons/vsc' import Button from '../components/button.js' import Tooltip from '../components/tooltip.js' import Loading from '../components/loading' @@ -109,6 +109,10 @@ export default function Files({ user }) { */ const TableItem = ({ nft }) => { // to do, add actual types + const [isRenaming, setRenaming] = useState(false) + const [isLoading, setLoading] = useState(false) + const [renameError, setError] = useState('') + const [renamedValue, setRenamedValue] = useState('') const [showAllDeals, setShowAllDeals] = useState(false) const deals = nft.deals .filter((/** @type {any} */ d) => d.status !== 'queued') @@ -190,13 +194,107 @@ export default function Files({ user }) { ) } + /** @param {import('react').ChangeEvent} ev */ + const handleRename = async (ev) => { + ev.preventDefault() + const data = new FormData(ev.target) + const fileName = data.get('fileName') + + if (!fileName || typeof fileName !== 'string') return + if (fileName === nft.name) return setRenaming(false) + + try { + setLoading(true) + await fetch(`${API}/upload/${nft.cid}`, { + method: 'PATCH', + body: JSON.stringify({ + name: fileName, + }), + headers: { + 'Content-type': 'application/json; charset=UTF-8', + }, + }) + setError('') + } catch (e) { + console.error(e) + // @ts-ignore Catch clause variable type annotation must be 'any' or 'unknown' if specified.ts(1196) + setError(e.message) + } + + setLoading(false) + setRenaming(false) + setRenamedValue(fileName) + } + return ( {nft.created.split('T')[0]} - {nft.name} + {!isRenaming ? ( +
0 && 'text-w3storage-red' + )} + > + {renamedValue || nft.name} + {renameError.length > 0 && ( + + ! + + )} + +
+ ) : ( +
+ + +
+ )}