import React, { useState } from 'react' import { Link, useHistory } from 'react-router-dom' import useAuth from '@wasp/auth/useAuth.js' import logout from '@wasp/auth/logout.js' import createArticle from '@wasp/actions/createArticle' import updateArticle from '@wasp/actions/updateArticle' import { useQuery } from '@wasp/queries' import getArticle from '@wasp/queries/getArticle' import Navbar from './Navbar' const ArticleEditorPage = (props) => { const { data: user, isError } = useAuth({ keepPreviousData: true }) // TODO: Here, as in some other places, it feels tricky to figure out what is happening regarding the state. // When is article null, when not, should I look into combination of article and articleSlug, then // there is this 'enabled' which I need on the other hand -> uff. And what if I get error? humpf! const articleSlug = props.match.params.articleSlug const { data: article, error: articleError } = useQuery(getArticle, { slug: articleSlug }, { enabled: articleSlug }) // TODO: Instead of this logic here, I wish I could use ACL via Wasp and just // receive user via props instead of useAuth(). if (!user || isError) { return Please log in. } return articleError ? articleError.message || articleError : (
) } const ArticleEditor = (props) => { const user = props.user const article = props.article console.log(article) const history = useHistory() const [title, setTitle] = useState(article?.title || '') const [description, setDescription] = useState(article?.description || '') const [markdownContent, setMarkdownContent] = useState(article?.markdownContent || '') const [submitError, setSubmitError] = useState(null) const handleSubmit = async (event) => { event.preventDefault() setSubmitError(null) try { let articleSlug if (article?.id) { await updateArticle({ id: article.id, title, description, markdownContent }) articleSlug = article.slug } else { const newArticle = await createArticle({ title, description, markdownContent }) articleSlug = newArticle.slug } history.push(`/article/${articleSlug}`) } catch (err) { setSubmitError(err) } } return (
{ submitError && (

{ submitError.message || submitError }

) }

Article title

setTitle(e.target.value)} />

What's this article about?

setDescription(e.target.value)} />

Markdown content