diff --git a/ui/src/articles/AddArticlePage.tsx b/ui/src/articles/AddArticlePage.tsx index 86d0c08c2..fe48c4338 100644 --- a/ui/src/articles/AddArticlePage.tsx +++ b/ui/src/articles/AddArticlePage.tsx @@ -6,34 +6,44 @@ import ButtonIcon from '../components/ButtonIcon' import { URLRegExp } from '../helpers' import Page from '../layout/Page' import AddArticleForm from './components/AddArticleForm' +import { Article } from './models' +import { Category } from '../categories/models' -type AllProps = RouteComponentProps +interface Props { + category?: Category +} -const Actions = () => +type AllProps = Props & RouteComponentProps -export default ({ location, history }: AllProps) => { - const params = new URLSearchParams(location.search) +const Actions = () => - let value = '' +const extractURLFromParams = (qs: string) => { + const params = new URLSearchParams(qs) if (params.has('url')) { - value = params.get('url')! + return params.get('url')! } else if (params.has('text') || params.has('title')) { const text = params.get('text') || params.get('title')! if (URLRegExp.test(text)) { - value = text + return text } else { const matches = text.match(/\bhttps?:\/\/\S+/gi) if (matches && URLRegExp.test(matches[0])) { - value = matches[0] + return matches[0] } } } + return undefined +} + +export default ({ category, location, history }: AllProps) => { + const url = extractURLFromParams(location.search) - const redirect = () => history.replace('/unread') + const onSuccess = (article: Article) => history.replace(`/unread/${article.id}`) + const onCancel = () => history.replace('/unread') return ( }> - + ) } diff --git a/ui/src/articles/ArticlePage.tsx b/ui/src/articles/ArticlePage.tsx index c6d4b162b..8572a953c 100644 --- a/ui/src/articles/ArticlePage.tsx +++ b/ui/src/articles/ArticlePage.tsx @@ -30,13 +30,13 @@ export default ({ category, match, history }: AllProps) => { let title = 'Articles to read' if (category) { title = category.title - } - if (match.path === '/history/:id') { + } else if (match.path === '/history/:id') { title = 'History' } - // useKeyboard('backspace', () => history.push(redirect)) - useKeyboard('backspace', () => history.goBack()) + const goBack = () => history.goBack() + + useKeyboard('backspace', goBack) const { data, error, loading } = useQuery(GetArticle, { variables: { id } @@ -58,7 +58,7 @@ export default ({ category, match, history }: AllProps) => { - history.goBack()} keyboard /> + ) } @@ -71,7 +71,7 @@ export default ({ category, match, history }: AllProps) => { } + actions={} > {render(data, error, loading)} diff --git a/ui/src/articles/components/AddArticleForm.tsx b/ui/src/articles/components/AddArticleForm.tsx index bb0ea5cac..278670bd0 100644 --- a/ui/src/articles/components/AddArticleForm.tsx +++ b/ui/src/articles/components/AddArticleForm.tsx @@ -9,13 +9,16 @@ import Loader from '../../components/Loader' import Panel from '../../components/Panel' import { MessageContext } from '../../context/MessageContext' import ErrorPanel from '../../error/ErrorPanel' -import { getGQLError, isValidForm } from '../../helpers' +import { getGQLError, isValidForm, isValidInput } from '../../helpers' import useOnMountInputValidator from '../../hooks/useOnMountInputValidator' import { AddNewArticleRequest, AddNewArticleResponse, Article } from '../models' import { AddNewArticle } from '../queries' +import FormSelectField from '../../components/FormSelectField' +import CategoriesOptions from '../../components/CategoriesOptions' interface AddArticleFormFields { url: string + category?: number } interface Props { @@ -29,15 +32,17 @@ export default ({ value, category, onSuccess, onCancel }: Props) => { const [loading, setLoading] = useState(false) const [errorMessage, setErrorMessage] = useState(null) const { showMessage } = useContext(MessageContext) - const [formState, { url }] = useFormState({ url: value }) + const [formState, { url, select }] = useFormState({ + url: value, + category: category ? category.id : undefined + }) const onMountValidator = useOnMountInputValidator(formState.validity) const addArticleMutation = useMutation(AddNewArticle) const addArticle = async (form: AddArticleFormFields) => { setLoading(true) try { - const categoryID = category ? category.id : undefined - const variables = { ...form, category: categoryID } + const variables = { ...form } const res = await addArticleMutation({ variables }) setLoading(false) if (res.data) { @@ -75,11 +80,20 @@ export default ({ value, category, onSuccess, onCancel }: Props) => { + + + +