Webui 548 (#549)

Clรกudio Silva created

* Commit for #548

* Commit for #548

Change summary

webui/src/components/ReopenBugButton/OpenBug.graphql     |  7 +
webui/src/components/ReopenBugButton/ReopenBugButton.tsx | 55 ++++++++++
webui/src/pages/bug/CommentForm.tsx                      | 11 +
3 files changed, 72 insertions(+), 1 deletion(-)

Detailed changes

webui/src/components/ReopenBugButton/ReopenBugButton.tsx ๐Ÿ”—

@@ -0,0 +1,55 @@
+import React from 'react';
+
+import Button from '@material-ui/core/Button';
+
+import { BugFragment } from 'src/pages/bug/Bug.generated';
+import { TimelineDocument } from 'src/pages/bug/TimelineQuery.generated';
+
+import { useOpenBugMutation } from './OpenBug.generated';
+
+interface Props {
+  bug: BugFragment;
+  disabled: boolean;
+}
+
+function ReopenBugButton({ bug, disabled }: Props) {
+  const [openBug, { loading, error }] = useOpenBugMutation();
+
+  function openBugAction() {
+    openBug({
+      variables: {
+        input: {
+          prefix: bug.id,
+        },
+      },
+      refetchQueries: [
+        // TODO: update the cache instead of refetching
+        {
+          query: TimelineDocument,
+          variables: {
+            id: bug.id,
+            first: 100,
+          },
+        },
+      ],
+      awaitRefetchQueries: true,
+    });
+  }
+
+  if (loading) return <div>Loading...</div>;
+  if (error) return <div>Error</div>;
+
+  return (
+    <div>
+      <Button
+        variant="contained"
+        onClick={() => openBugAction()}
+        disabled={bug.status === 'OPEN' || disabled}
+      >
+        Reopen issue
+      </Button>
+    </div>
+  );
+}
+
+export default ReopenBugButton;

webui/src/pages/bug/CommentForm.tsx ๐Ÿ”—

@@ -6,6 +6,7 @@ import { makeStyles, Theme } from '@material-ui/core/styles';
 
 import CommentInput from '../../layout/CommentInput/CommentInput';
 import CloseBugButton from 'src/components/CloseBugButton/CloseBugButton';
+import ReopenBugButton from 'src/components/ReopenBugButton/ReopenBugButton';
 
 import { BugFragment } from './Bug.generated';
 import { useAddCommentMutation } from './CommentForm.generated';
@@ -83,6 +84,14 @@ function CommentForm({ bug }: Props) {
     if (issueComment.length > 0) submit();
   };
 
+  function getCloseButton() {
+    return <CloseBugButton bug={bug} disabled={issueComment.length > 0} />;
+  }
+
+  function getReopenButton() {
+    return <ReopenBugButton bug={bug} disabled={issueComment.length > 0} />;
+  }
+
   return (
     <Paper className={classes.container}>
       <form onSubmit={handleSubmit} ref={form}>
@@ -92,7 +101,7 @@ function CommentForm({ bug }: Props) {
           onChange={(comment: string) => setIssueComment(comment)}
         />
         <div className={classes.actions}>
-          <CloseBugButton bug={bug} disabled={issueComment.length > 0} />
+          {bug.status === 'OPEN' ? getCloseButton() : getReopenButton()}
           <Button
             className={classes.greenButton}
             variant="contained"