-
Notifications
You must be signed in to change notification settings - Fork 48
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
Rewrite quote form to react-final-form #3589
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
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
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,95 +1,108 @@ | ||
import { Field } from 'react-final-form'; | ||
import { Helmet } from 'react-helmet-async'; | ||
import { Field, reduxForm } from 'redux-form'; | ||
import { Button, TextEditor, withSubmissionError } from 'app/components/Form'; | ||
import { Button, TextInput } from 'app/components/Form'; | ||
import LegoFinalForm from 'app/components/Form/LegoFinalForm'; | ||
import { withSubmissionErrorFinalForm } from 'app/components/Form/utils'; | ||
import RandomQuote from 'app/components/RandomQuote/RandomQuote'; | ||
import { spySubmittable, spyValues } from 'app/utils/formSpyUtils'; | ||
import { createValidator, required } from 'app/utils/validation'; | ||
import { navigation } from '../utils'; | ||
import styles from './Quotes.css'; | ||
|
||
type Props = { | ||
addQuotes: (arg0: Record<string, any>) => Promise<any>; | ||
invalid: boolean; | ||
pristine: boolean; | ||
submitting: boolean; | ||
handleSubmit: (arg0: (arg0: Record<string, any>) => Promise<any>) => void; | ||
addQuotes: (quote: { text: string; source: string }) => Promise<unknown>; | ||
actionGrant: Array<string>; | ||
}; | ||
|
||
type FormValues = { | ||
text: string; | ||
source: string; | ||
}; | ||
|
||
const AddQuote = ({ | ||
addQuotes, | ||
invalid, | ||
pristine, | ||
submitting, | ||
handleSubmit, | ||
actionGrant, | ||
text, | ||
source, | ||
}: Props) => { | ||
const disabledButton = invalid || pristine || submitting; | ||
const initialValues: FormValues = { | ||
text: '', | ||
source: '', | ||
}; | ||
|
||
const validate = createValidator({ | ||
text: [required()], | ||
source: [required()], | ||
}); | ||
|
||
const AddQuote = ({ addQuotes, actionGrant }: Props) => { | ||
const onSubmit = withSubmissionErrorFinalForm(addQuotes); | ||
|
||
return ( | ||
<div className={styles.root}> | ||
<Helmet title="Nytt sitat" /> | ||
{navigation('Legg til sitat', actionGrant)} | ||
|
||
<div className={styles.addQuote}> | ||
<form onSubmit={handleSubmit(withSubmissionError(addQuotes))}> | ||
<Field | ||
placeholder="Eks: Det er bare å gjøre det" | ||
label="Selve sitatet" | ||
name="text" | ||
component={TextEditor.Field} | ||
/> | ||
<LegoFinalForm | ||
onSubmit={onSubmit} | ||
validate={validate} | ||
initialValues={initialValues} | ||
subscription={{}} | ||
> | ||
{({ handleSubmit }) => ( | ||
<> | ||
<div className={styles.addQuote}> | ||
<form onSubmit={handleSubmit}> | ||
<Field | ||
placeholder="Eks: Det er bare å gjøre det" | ||
label="Selve sitatet" | ||
name="text" | ||
component={TextInput.Field} | ||
/> | ||
|
||
<Field | ||
placeholder="Eks: Esso" | ||
label="Hvor sitatet kommer fra (sleng gjerne med noe snaks!)" | ||
name="source" | ||
component={TextEditor.Field} | ||
type="text" | ||
/> | ||
<Field | ||
placeholder="Eks: Esso" | ||
label="Hvor sitatet kommer fra (sleng gjerne med noe snaks!)" | ||
name="source" | ||
component={TextInput.Field} | ||
type="text" | ||
/> | ||
|
||
<div className={styles.clear} /> | ||
<div className={styles.clear} /> | ||
|
||
<Button type="submit" disabled={disabledButton}> | ||
Send inn sitat | ||
</Button> | ||
</form> | ||
</div> | ||
{spySubmittable((submittable) => ( | ||
<Button type="submit" disabled={!submittable}> | ||
Send inn sitat | ||
</Button> | ||
))} | ||
</form> | ||
</div> | ||
|
||
<h2>Forhåndsvisning</h2> | ||
<h3 className="u-ui-heading">Overhørt</h3> | ||
<div className={styles.innerPreview}> | ||
<RandomQuote | ||
fetchRandomQuote={() => Promise.resolve()} | ||
addReaction={() => Promise.resolve()} | ||
deleteReaction={() => Promise.resolve()} | ||
fetchEmojis={() => Promise.resolve()} | ||
fetchingEmojis={false} | ||
emojis={[]} | ||
currentQuote={{ | ||
id: 1, | ||
text: text || 'Det er bare å gjøre det', | ||
source: source || 'Esso', | ||
approved: true, | ||
contentTarget: '', | ||
reactionsGrouped: [], | ||
reactions: [], | ||
}} | ||
loggedIn={true} | ||
useReactions={false} | ||
/> | ||
</div> | ||
<h2>Forhåndsvisning</h2> | ||
<h3 className="u-ui-heading">Overhørt</h3> | ||
<div className={styles.innerPreview}> | ||
{spyValues<FormValues>((values) => ( | ||
<RandomQuote | ||
fetchRandomQuote={() => Promise.resolve()} | ||
addReaction={() => Promise.resolve()} | ||
deleteReaction={() => Promise.resolve()} | ||
fetchEmojis={() => Promise.resolve()} | ||
fetchingEmojis={false} | ||
emojis={[]} | ||
currentQuote={{ | ||
id: 1, | ||
text: values.text || 'Det er bare å gjøre det', | ||
source: values.source || 'Esso', | ||
approved: true, | ||
contentTarget: '', | ||
reactionsGrouped: [], | ||
reactions: [], | ||
reactionCount: 0, | ||
}} | ||
loggedIn={true} | ||
useReactions={false} | ||
/> | ||
))} | ||
</div> | ||
</> | ||
)} | ||
</LegoFinalForm> | ||
</div> | ||
); | ||
}; | ||
|
||
const validate = createValidator({ | ||
text: [required()], | ||
source: [required()], | ||
}); | ||
export default reduxForm({ | ||
form: 'addQuote', | ||
validate, | ||
})(AddQuote); | ||
export default AddQuote; |
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
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.
Could you put the actual return type here? Or void if you're unsure. It's better then unknown when you know you don't use it IMO.