From 62952b80dd1cecaa28ff9032ed9cc587828dfd17 Mon Sep 17 00:00:00 2001 From: Matchu Date: Tue, 23 Mar 2021 17:48:11 -0700 Subject: [PATCH] Can edit closet list text, incl as Support I wanted the ability to clear out closet list text for Support users, and figured I should just build the UI for end users too, and grant Support users the same access! --- scripts/setup-mysql.sql | 2 +- src/app/UserItemsPage.js | 165 ++++++++++++++++++++++++++++--- src/app/components/WIPCallout.js | 14 ++- src/server/types/ClosetList.js | 84 ++++++++++++++++ 4 files changed, 246 insertions(+), 19 deletions(-) diff --git a/scripts/setup-mysql.sql b/scripts/setup-mysql.sql index 8d550c98..8a6251ec 100644 --- a/scripts/setup-mysql.sql +++ b/scripts/setup-mysql.sql @@ -26,7 +26,7 @@ GRANT INSERT ON modeling_logs TO impress2020; -- User data tables GRANT SELECT, INSERT, DELETE ON closet_hangers TO impress2020; -GRANT SELECT ON closet_lists TO impress2020; +GRANT SELECT, UPDATE ON closet_lists TO impress2020; GRANT SELECT ON item_outfit_relationships TO impress2020; GRANT SELECT ON neopets_connections TO impress2020; GRANT SELECT ON outfits TO impress2020; diff --git a/src/app/UserItemsPage.js b/src/app/UserItemsPage.js index e0dc9d11..7c4f8b35 100644 --- a/src/app/UserItemsPage.js +++ b/src/app/UserItemsPage.js @@ -19,6 +19,9 @@ import { WrapItem, VStack, useToast, + Button, + Textarea, + HStack, } from "@chakra-ui/react"; import { ArrowForwardIcon, @@ -239,11 +242,6 @@ function UserItemsPage() { - {isCurrentUser && ( - - - - )} {isCurrentUser ? "Items you own" @@ -417,6 +415,9 @@ function UserSearchForm() { } function ClosetList({ closetList, isCurrentUser, showHeading }) { + const { isSupportUser, supportSecret } = useSupport(); + const toast = useToast(); + const hasYouWantThisBadge = (item) => !isCurrentUser && closetList.ownsOrWantsItems === "OWNS" && @@ -446,19 +447,155 @@ function ClosetList({ closetList, isCurrentUser, showHeading }) { } }, [anchorId]); + const [ + sendSaveChangesMutation, + { loading: loadingSaveChanges }, + ] = useMutation( + gql` + mutation ClosetList_Edit( + $closetListId: ID! + $name: String! + $description: String! + # Support users can edit any list, if they provide the secret. If you're + # editing your own list, this will be empty, and that's okay. + $supportSecret: String + ) { + editClosetList( + closetListId: $closetListId + name: $name + description: $description + supportSecret: $supportSecret + ) { + id + name + description + } + } + `, + { context: { sendAuth: true } } + ); + + const [isEditing, setIsEditing] = React.useState(false); + const [editableName, setEditableName] = React.useState(closetList.name); + const [editableDescription, setEditableDescription] = React.useState( + closetList.description + ); + const hasChanges = + editableName !== closetList.name || + editableDescription !== closetList.description; + const onSaveChanges = () => { + if (!hasChanges) { + setIsEditing(false); + return; + } + + sendSaveChangesMutation({ + variables: { + closetListId: closetList.id, + name: editableName, + description: editableDescription, + supportSecret, + }, + }) + .then(() => { + setIsEditing(false); + toast({ + status: "success", + title: "Changes saved!", + }); + }) + .catch((err) => { + console.error(err); + toast({ + status: "error", + title: "Sorry, we couldn't save this list 😖", + description: "Check your connection and try again.", + }); + }); + }; + return ( - {showHeading && ( - - {closetList.name} - - )} + + {showHeading && + (isEditing ? ( + setEditableName(e.target.value)} + maxWidth="20ch" + // Shift left by our own padding/border, for alignment with the + // original title + paddingX="0.75rem" + marginLeft="calc(-0.75rem - 1px)" + boxShadow="sm" + /> + ) : ( + + {closetList.name} + + ))} + + {(isCurrentUser || isSupportUser) && + !closetList.isDefaultList && + (isEditing ? ( + <> + + WIP: Can only edit text for now! + + + + + + + + ) : ( + + ))} + {closetList.description && ( - {closetList.description} + {isEditing ? ( +