aboutsummaryrefslogtreecommitdiffstats
path: root/webui
diff options
context:
space:
mode:
authorQuentin Gliech <quentingliech@gmail.com>2020-02-13 00:19:22 +0100
committerQuentin Gliech <quentingliech@gmail.com>2020-02-13 00:19:22 +0100
commit680dd91c0c0200bd4948173df0b601e16f511e6e (patch)
tree4d4cd8fb1fa42c3d17fa507880bd7e2b470e0d4c /webui
parent0066f3d8c278558eeac70d3cd7ca21c360014346 (diff)
downloadgit-bug-680dd91c0c0200bd4948173df0b601e16f511e6e.tar.gz
webui: create comment form
Diffstat (limited to 'webui')
-rw-r--r--webui/package-lock.json5
-rw-r--r--webui/package.json1
-rw-r--r--webui/src/Date.tsx7
-rw-r--r--webui/src/bug/Bug.tsx3
-rw-r--r--webui/src/bug/CommentForm.graphql5
-rw-r--r--webui/src/bug/CommentForm.tsx145
6 files changed, 165 insertions, 1 deletions
diff --git a/webui/package-lock.json b/webui/package-lock.json
index 9bc9576c..d0c7d6f4 100644
--- a/webui/package-lock.json
+++ b/webui/package-lock.json
@@ -14608,6 +14608,11 @@
"resolved": "https://registry.npmjs.org/react-is/-/react-is-16.12.0.tgz",
"integrity": "sha512-rPCkf/mWBtKc97aLL9/txD8DZdemK0vkA3JMLShjlJB3Pj3s+lpf1KaBzMfQrAmhMQB0n1cU/SUGgKKBCe837Q=="
},
+ "react-moment": {
+ "version": "0.9.7",
+ "resolved": "https://registry.npmjs.org/react-moment/-/react-moment-0.9.7.tgz",
+ "integrity": "sha512-ifzUrUGF6KRsUN2pRG5k56kO0mJBr8kRkWb0wNvtFIsBIxOuPxhUpL1YlXwpbQCbHq23hUu6A0VEk64HsFxk9g=="
+ },
"react-router": {
"version": "5.1.2",
"resolved": "https://registry.npmjs.org/react-router/-/react-router-5.1.2.tgz",
diff --git a/webui/package.json b/webui/package.json
index cf61b883..a6fd4a58 100644
--- a/webui/package.json
+++ b/webui/package.json
@@ -21,6 +21,7 @@
"react": "^16.8.6",
"react-apollo": "^3.1.3",
"react-dom": "^16.8.6",
+ "react-moment": "^0.9.7",
"react-router": "^5.0.0",
"react-router-dom": "^5.0.0",
"react-scripts": "^3.3.1",
diff --git a/webui/src/Date.tsx b/webui/src/Date.tsx
index 9380d2fc..a830546c 100644
--- a/webui/src/Date.tsx
+++ b/webui/src/Date.tsx
@@ -1,11 +1,16 @@
import Tooltip from '@material-ui/core/Tooltip/Tooltip';
import moment from 'moment';
import React from 'react';
+import Moment from 'react-moment';
+
+const HOUR = 1000 * 3600;
+const DAY = 24 * HOUR;
+const WEEK = 7 * DAY;
type Props = { date: string };
const Date = ({ date }: Props) => (
<Tooltip title={moment(date).format('MMMM D, YYYY, h:mm a')}>
- <span> {moment(date).fromNow()} </span>
+ <Moment date={date} fromNowDuring={WEEK} />
</Tooltip>
);
diff --git a/webui/src/bug/Bug.tsx b/webui/src/bug/Bug.tsx
index f4029a5f..114cb8e0 100644
--- a/webui/src/bug/Bug.tsx
+++ b/webui/src/bug/Bug.tsx
@@ -7,6 +7,7 @@ import Date from '../Date';
import Label from '../Label';
import { BugFragment } from './Bug.generated';
+import CommentForm from './CommentForm';
import TimelineQuery from './TimelineQuery';
const useStyles = makeStyles(theme => ({
@@ -87,6 +88,8 @@ function Bug({ bug }: Props) {
</ul>
</div>
</div>
+
+ <CommentForm bugId={bug.id} />
</main>
);
}
diff --git a/webui/src/bug/CommentForm.graphql b/webui/src/bug/CommentForm.graphql
new file mode 100644
index 00000000..33d21193
--- /dev/null
+++ b/webui/src/bug/CommentForm.graphql
@@ -0,0 +1,5 @@
+mutation AddComment($input: AddCommentInput!) {
+ addComment(input: $input) {
+ operation { id }
+ }
+}
diff --git a/webui/src/bug/CommentForm.tsx b/webui/src/bug/CommentForm.tsx
new file mode 100644
index 00000000..3aa52b19
--- /dev/null
+++ b/webui/src/bug/CommentForm.tsx
@@ -0,0 +1,145 @@
+import Button from '@material-ui/core/Button';
+import Paper from '@material-ui/core/Paper';
+import Tab from '@material-ui/core/Tab';
+import Tabs from '@material-ui/core/Tabs';
+import TextField from '@material-ui/core/TextField';
+import { makeStyles, Theme } from '@material-ui/core/styles';
+import React, { useState, useRef } from 'react';
+
+import Content from '../Content';
+
+import { useAddCommentMutation } from './CommentForm.generated';
+import { TimelineDocument } from './TimelineQuery.generated';
+
+type StyleProps = { loading: boolean };
+const useStyles = makeStyles<Theme, StyleProps>(theme => ({
+ container: {
+ margin: theme.spacing(2, 0),
+ padding: theme.spacing(0, 2, 2, 2),
+ },
+ textarea: {},
+ tabContent: {
+ margin: theme.spacing(2, 0),
+ },
+ preview: {
+ borderBottom: `solid 3px ${theme.palette.grey['200']}`,
+ minHeight: '5rem',
+ },
+ actions: {
+ display: 'flex',
+ justifyContent: 'flex-end',
+ },
+}));
+
+type TabPanelProps = {
+ children: React.ReactNode;
+ value: number;
+ index: number;
+} & React.HTMLProps<HTMLDivElement>;
+function TabPanel({ children, value, index, ...props }: TabPanelProps) {
+ return (
+ <div
+ role="tabpanel"
+ hidden={value !== index}
+ id={`editor-tabpanel-${index}`}
+ aria-labelledby={`editor-tab-${index}`}
+ {...props}
+ >
+ {value === index && children}
+ </div>
+ );
+}
+
+const a11yProps = (index: number) => ({
+ id: `editor-tab-${index}`,
+ 'aria-controls': `editor-tabpanel-${index}`,
+});
+
+type Props = {
+ bugId: string;
+};
+
+function CommentForm({ bugId }: Props) {
+ const [addComment, { loading }] = useAddCommentMutation();
+ const [input, setInput] = useState<string>('');
+ const [tab, setTab] = useState(0);
+ const classes = useStyles({ loading });
+ const form = useRef<HTMLFormElement>(null);
+
+ const submit = () => {
+ addComment({
+ variables: {
+ input: {
+ prefix: bugId,
+ message: input,
+ },
+ },
+ refetchQueries: [
+ // TODO: update the cache instead of refetching
+ {
+ query: TimelineDocument,
+ variables: {
+ id: bugId,
+ first: 100,
+ },
+ },
+ ],
+ awaitRefetchQueries: true,
+ }).then(() => setInput(''));
+ };
+
+ const handleSubmit = (e: React.FormEvent<HTMLFormElement>) => {
+ e.preventDefault();
+ submit();
+ };
+
+ const handleKeyDown = (e: React.KeyboardEvent<HTMLElement>) => {
+ // Submit on cmd/ctrl+enter
+ if ((e.metaKey || e.altKey) && e.keyCode === 13) {
+ submit();
+ }
+ };
+
+ return (
+ <Paper className={classes.container}>
+ <form onSubmit={handleSubmit} ref={form}>
+ <Tabs value={tab} onChange={(_, t) => setTab(t)}>
+ <Tab label="Write" {...a11yProps(0)} />
+ <Tab label="Preview" {...a11yProps(1)} />
+ </Tabs>
+ <div className={classes.tabContent}>
+ <TabPanel value={tab} index={0}>
+ <TextField
+ onKeyDown={handleKeyDown}
+ fullWidth
+ label="Comment"
+ placeholder="Leave a comment"
+ className={classes.textarea}
+ multiline
+ value={input}
+ variant="filled"
+ rows="4" // TODO: rowsMin support
+ onChange={(e: any) => setInput(e.target.value)}
+ disabled={loading}
+ />
+ </TabPanel>
+ <TabPanel value={tab} index={1} className={classes.preview}>
+ <Content markdown={input} />
+ </TabPanel>
+ </div>
+ <div className={classes.actions}>
+ <Button
+ variant="contained"
+ color="primary"
+ type="submit"
+ disabled={loading}
+ >
+ Comment
+ </Button>
+ </div>
+ </form>
+ </Paper>
+ );
+}
+
+export default CommentForm;