-
Notifications
You must be signed in to change notification settings - Fork 180
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Migrate Post Form to React #10408
Open
FinnIckler
wants to merge
11
commits into
thewca:main
Choose a base branch
from
FinnIckler:react/create-post
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Migrate Post Form to React #10408
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
d66201a
add new PostForm component
FinnIckler 9873b85
allow creating a post
FinnIckler fc95740
start of EditPost
FinnIckler 51c56fd
correct edit requests
FinnIckler 0e6b5ba
add deletion
FinnIckler 4ba7101
fix post widget on the homepage
FinnIckler 62533e3
implement date picker
FinnIckler a381050
internationalize Post Form
FinnIckler bb09e6f
Merge branch 'main' into react/create-post
FinnIckler 7208373
handle errors
FinnIckler a0b2cb3
don't allow deleting from post form
FinnIckler File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,11 @@ | ||
<% provide(:title, 'Edit post') %> | ||
|
||
<div class="container"> | ||
<h1><%= yield(:title) %></h1> | ||
|
||
<%= render "posts/post_form" %> | ||
<%= react_component("Posts/EditPost", { | ||
allTags: PostTag.distinct.pluck(:tag).map { |tag| { value: tag, text: tag, key: tag } }.as_json, | ||
post: @post.as_json({ | ||
only: ["id", "title", "sticky", "show_on_homepage", "unstick_at"], | ||
methods: ["tags_array"], | ||
}), | ||
}) %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
<% provide(:title, 'New post') %> | ||
|
||
<div class="container"> | ||
<h1><%= yield(:title) %></h1> | ||
|
||
<%= render "posts/post_form" %> | ||
<%= react_component("Posts/CreatePost", { | ||
allTags: PostTag.distinct.pluck(:tag).map { |tag| { value: tag, text: tag, key: tag } }.as_json, | ||
}) %> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from 'react'; | ||
import WCAQueryClientProvider from '../../lib/providers/WCAQueryClientProvider'; | ||
import PostForm from './PostForm'; | ||
|
||
export default function Wrapper({ | ||
allTags, | ||
}) { | ||
return ( | ||
<WCAQueryClientProvider> | ||
<PostForm allTags={allTags} header="New Post" /> | ||
</WCAQueryClientProvider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import React from 'react'; | ||
import WCAQueryClientProvider from '../../lib/providers/WCAQueryClientProvider'; | ||
import PostForm from './PostForm'; | ||
import ConfirmProvider from '../../lib/providers/ConfirmProvider'; | ||
|
||
export default function Wrapper({ | ||
allTags, post, | ||
}) { | ||
return ( | ||
<WCAQueryClientProvider> | ||
<ConfirmProvider> | ||
<PostForm post={post} allTags={allTags} header="Edit Post" /> | ||
</ConfirmProvider> | ||
</WCAQueryClientProvider> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,141 @@ | ||
import React, { useCallback, useState } from 'react'; | ||
import { | ||
Button, Form, FormField, Header, Message, | ||
} from 'semantic-ui-react'; | ||
import { useMutation } from '@tanstack/react-query'; | ||
import I18n from '../../lib/i18n'; | ||
import useInputState from '../../lib/hooks/useInputState'; | ||
import useCheckboxState from '../../lib/hooks/useCheckboxState'; | ||
import MarkdownEditor from '../wca/FormBuilder/input/MarkdownEditor'; | ||
import { createPost, deletePost, editPost } from './api/posts'; | ||
import { useConfirm } from '../../lib/providers/ConfirmProvider'; | ||
import UtcDatePicker from '../wca/UtcDatePicker'; | ||
import I18nHTMLTranslate from '../I18nHTMLTranslate'; | ||
|
||
export default function PostForm({ | ||
header, allTags, post, | ||
}) { | ||
const [formPost, setFormPost] = useState(post); | ||
const [formTitle, setFormTitle] = useInputState(formPost?.title ?? ''); | ||
const [formBody, setFormBody] = useInputState(formPost?.body ?? ''); | ||
const [formTags, setFormTags] = useState(formPost?.tags_array ?? []); | ||
const [formIsStickied, setFormIsStickied] = useCheckboxState(formPost?.sticky ?? false); | ||
const [formShowOnHomePage, setFormShowOnHomePage] = useCheckboxState(formPost?.show_on_homepage ?? true); | ||
const [unstickAt, setUnstickAt] = useState(formPost?.unstick_at ?? null); | ||
|
||
const { mutate: createMutation, isSuccess: postCreated, error: createError } = useMutation({ | ||
mutationFn: createPost, | ||
onSuccess: ({ data }) => { | ||
setFormPost(data.post); | ||
window.history.replaceState({}, '', `${data.post.url}/edit`); | ||
}, | ||
}); | ||
|
||
const { mutate: editMutation, error: editError, isSuccess: postUpdated } = useMutation({ | ||
mutationFn: editPost, | ||
onSuccess: ({ data }) => { | ||
setFormPost(data.post); | ||
}, | ||
}); | ||
|
||
const { errors } = (createError?.json || editError?.json || {}); | ||
|
||
const onSubmit = useCallback(() => { | ||
if (formPost?.id) { | ||
editMutation({ | ||
id: formPost.id, | ||
title: formTitle, | ||
body: formBody, | ||
tags: formTags, | ||
unstick_at: formIsStickied ? unstickAt : null, | ||
sticky: formIsStickied, | ||
show_on_homepage: formShowOnHomePage, | ||
}); | ||
} else { | ||
createMutation({ | ||
title: formTitle, | ||
body: formBody, | ||
tags: formTags, | ||
sticky: formIsStickied, | ||
unstick_at: formIsStickied ? unstickAt : null, | ||
show_on_homepage: formShowOnHomePage, | ||
}); | ||
} | ||
}, [ | ||
createMutation, | ||
editMutation, | ||
formBody, | ||
formIsStickied, | ||
formShowOnHomePage, | ||
formTags, | ||
formTitle, | ||
unstickAt, | ||
formPost?.id, | ||
]); | ||
|
||
return ( | ||
<> | ||
<Header> | ||
{header} | ||
</Header> | ||
<Form onSubmit={onSubmit}> | ||
<FormField> | ||
<Form.Input label={I18n.t('activerecord.attributes.post.title')} onChange={setFormTitle} value={formTitle} /> | ||
</FormField> | ||
<FormField> | ||
<label>{I18n.t('activerecord.attributes.post.body')}</label> | ||
<MarkdownEditor onChange={setFormBody} value={formBody} /> | ||
{/* i18n-tasks-use t('simple_form.hints.post.body') */} | ||
<I18nHTMLTranslate i18nKey="simple_form.hints.post.body" /> | ||
</FormField> | ||
<FormField> | ||
<Form.Select label={I18n.t('activerecord.attributes.post.tags')} options={allTags} onChange={(_, data) => { setFormTags(data.value); }} value={formTags} multiple /> | ||
</FormField> | ||
<FormField> | ||
<Form.Checkbox label={I18n.t('activerecord.attributes.post.sticky')} onChange={setFormIsStickied} checked={formIsStickied} /> | ||
{ formIsStickied | ||
&& ( | ||
<UtcDatePicker | ||
placeholderText={I18n.t('activerecord.attributes.post.unstick_at')} | ||
isoDate={unstickAt} | ||
onChange={(date) => setUnstickAt(date)} | ||
/> | ||
) } | ||
</FormField> | ||
<FormField> | ||
<Form.Checkbox label={I18n.t('activerecord.attributes.post.show_on_homepage')} onChange={setFormShowOnHomePage} checked={formShowOnHomePage} /> | ||
<p>{I18n.t('simple_form.hints.post.show_on_homepage')}</p> | ||
</FormField> | ||
{ errors && ( | ||
<Message negative> | ||
<Message.Header>Request Failed:</Message.Header> | ||
<Message.List> | ||
{(Object.keys(errors).map((err) => ( | ||
<Message.Item> | ||
{err} | ||
{' '} | ||
{errors[err].join(',')} | ||
</Message.Item> | ||
)))} | ||
</Message.List> | ||
</Message> | ||
)} | ||
{ postCreated && ( | ||
<Message positive> | ||
Post successfully created. View it | ||
{' '} | ||
<a href={formPost.url}>here</a> | ||
</Message> | ||
)} | ||
{ postUpdated && ( | ||
<Message positive> | ||
Post successfully updated. View it | ||
{' '} | ||
<a href={formPost.url}>here</a> | ||
</Message> | ||
)} | ||
Comment on lines
+123
to
+136
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If you create a post then edit it, won't both messages be visible? Side note: Formatting (indentation and spacing) seems to be off here, and I've noticed it in other PRs too. |
||
<Button type="submit" primary>{ formPost ? 'Update Post' : 'Create Post'}</Button> | ||
</Form> | ||
</> | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { fetchJsonOrError } from '../../../lib/requests/fetchWithAuthenticityToken'; | ||
import { postUrl, submitPostUrl } from '../../../lib/requests/routes.js.erb'; | ||
|
||
export const createPost = (post) => fetchJsonOrError(submitPostUrl, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ post }), | ||
}); | ||
|
||
export const editPost = (post) => fetchJsonOrError(postUrl(post.id), { | ||
method: 'PATCH', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
body: JSON.stringify({ post }), | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Feels weird to have states for both the (form)post, and most of the properties of the post. Besides initializing the other states (which happens initially, so doesn't need
formPost
to be a state),formPost
is only used to check whether we're creating or editing, get the id (if we're editing), and show the url at the end.It seems like we could get rid of
formPost
and instead just have 2 more states for the post id and url; then update those 2 states in theonSuccess
functions below.