From 8a87ddf0957e9b56ac2a085d2f10efaa90e58dbd Mon Sep 17 00:00:00 2001 From: Kurt Hutten Date: Wed, 30 Dec 2020 11:14:00 +1100 Subject: [PATCH] Make title save able from draft --- web/src/components/IdeToolbar/IdeToolbar.js | 77 +++++++++++++++------ 1 file changed, 54 insertions(+), 23 deletions(-) diff --git a/web/src/components/IdeToolbar/IdeToolbar.js b/web/src/components/IdeToolbar/IdeToolbar.js index c9fd12c..f923188 100644 --- a/web/src/components/IdeToolbar/IdeToolbar.js +++ b/web/src/components/IdeToolbar/IdeToolbar.js @@ -26,21 +26,10 @@ const IdeToolbar = ({ const [isLoginModalOpen, setIsLoginModalOpen] = useState(false) const { isAuthenticated, currentUser } = useAuth() const showForkButton = !(canEdit || isDraft) + const [title, setTitle] = useState('untitled-part') const { addMessage } = useFlash() - const [forkPart] = useMutation(FORK_PART_MUTATION, { - onCompleted: ({ forkPart }) => { - navigate( - routes.ide({ - userName: forkPart?.user?.userName, - partTitle: forkPart?.title, - }) - ) - addMessage(`Part created with title: ${forkPart?.title}.`, { - classes: 'rw-flash-success', - }) - }, - }) + const [forkPart] = useMutation(FORK_PART_MUTATION) const handleClick = ({ event, whichPopup }) => { setAnchorEl(event.currentTarget) @@ -52,21 +41,42 @@ const IdeToolbar = ({ setWhichPopup(null) } - const handleSave = () => { - if (isDraft && isAuthenticated) - forkPart({ - variables: { - input: { - userId: currentUser.sub, - title: 'draft', - code, - }, + const saveFork = () => + forkPart({ + variables: { + input: { + userId: currentUser.sub, + title, + code, }, + }, + }) + + const handleSave = async () => { + if (isDraft && isAuthenticated) { + const { data } = await saveFork() + navigate( + routes.ide({ + userName: data?.forkPart?.user?.userName, + partTitle: data?.forkPart?.title, + }) + ) + addMessage(`Part created with title: ${data?.forkPart?.title}.`, { + classes: 'rw-flash-success', }) - else if (isAuthenticated) onSave() + } else if (isAuthenticated) onSave() else recordedLogin() } + const handleSaveAndEdit = async () => { + const { data } = await saveFork() + const { + user: { userName }, + title: partTitle, + } = data?.forkPart || { user: {} } + navigate(routes.part({ userName, partTitle })) + } + const recordedLogin = async () => { ReactGA.event({ category: 'login', @@ -135,6 +145,27 @@ const IdeToolbar = ({ )} + {isDraft && ( +
+ +
title:
+ + setTitle(target?.value.replace(/([^a-zA-Z\d_:])/g, '-')) + } + /> +
+
+ )}