From 680dd91c0c0200bd4948173df0b601e16f511e6e Mon Sep 17 00:00:00 2001 From: Quentin Gliech Date: Thu, 13 Feb 2020 00:19:22 +0100 Subject: webui: create comment form --- webui/src/Date.tsx | 7 +- webui/src/bug/Bug.tsx | 3 + webui/src/bug/CommentForm.graphql | 5 ++ webui/src/bug/CommentForm.tsx | 145 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 159 insertions(+), 1 deletion(-) create mode 100644 webui/src/bug/CommentForm.graphql create mode 100644 webui/src/bug/CommentForm.tsx (limited to 'webui/src') 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) => ( - {moment(date).fromNow()} + ); 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) { + + ); } 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 => ({ + 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; +function TabPanel({ children, value, index, ...props }: TabPanelProps) { + return ( + + ); +} + +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(''); + const [tab, setTab] = useState(0); + const classes = useStyles({ loading }); + const form = useRef(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) => { + e.preventDefault(); + submit(); + }; + + const handleKeyDown = (e: React.KeyboardEvent) => { + // Submit on cmd/ctrl+enter + if ((e.metaKey || e.altKey) && e.keyCode === 13) { + submit(); + } + }; + + return ( + +
+ setTab(t)}> + + + +
+ + setInput(e.target.value)} + disabled={loading} + /> + + + + +
+
+ +
+
+
+ ); +} + +export default CommentForm; -- cgit