-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3589 from webkom/quote-final-form
- Loading branch information
Showing
6 changed files
with
123 additions
and
97 deletions.
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