|
@@ -1,11 +1,113 @@
|
|
-import { useSelector } from 'react-redux';
|
|
|
|
|
|
+import { useEffect } from 'react';
|
|
|
|
+import hookForm from 'hook-easy-form';
|
|
|
|
+import { useSelector, useDispatch } from 'react-redux';
|
|
|
|
+
|
|
|
|
+import { setData, getUsers } from 'store/counter/actions';
|
|
|
|
+
|
|
|
|
+import classes from './index.module.scss';
|
|
|
|
+
|
|
|
|
+const form = [
|
|
|
|
+ {
|
|
|
|
+ name: 'first_name',
|
|
|
|
+ value: '',
|
|
|
|
+ required: true,
|
|
|
|
+ options: {
|
|
|
|
+ type: 'text',
|
|
|
|
+ label: 'First Name'
|
|
|
|
+ }
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ name: 'last_name',
|
|
|
|
+ value: '',
|
|
|
|
+ required: true,
|
|
|
|
+ options: {
|
|
|
|
+ type: 'text',
|
|
|
|
+ label: 'Last Name'
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ name: 'age',
|
|
|
|
+ value: '',
|
|
|
|
+ required: true,
|
|
|
|
+ options: {
|
|
|
|
+ type: 'number',
|
|
|
|
+ label: 'Your Age'
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ name: 'gender',
|
|
|
|
+ value: '',
|
|
|
|
+ required: true,
|
|
|
|
+ options: {
|
|
|
|
+ type: 'select',
|
|
|
|
+ label: 'Pick your gender',
|
|
|
|
+ options: [
|
|
|
|
+ { id: 'none', text: '' },
|
|
|
|
+ { id: 'male', text: 'Male' },
|
|
|
|
+ { id: 'female', text: 'Female' }
|
|
|
|
+ ]
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+ {
|
|
|
|
+ name: 'bio',
|
|
|
|
+ value: '',
|
|
|
|
+ required: true,
|
|
|
|
+ options: {
|
|
|
|
+ type: 'text-area',
|
|
|
|
+ label: 'Tell us about yourself'
|
|
|
|
+ },
|
|
|
|
+ },
|
|
|
|
+]
|
|
|
|
+
|
|
|
|
|
|
const Main = () => {
|
|
const Main = () => {
|
|
- const user = useSelector((s) => s.auth.user)
|
|
|
|
- return (<div>
|
|
|
|
|
|
+ const dispatch = useDispatch();
|
|
|
|
+ const { formArray, updateEvent, submitEvent, setValueManually } = hookForm({ initialForm: form })
|
|
|
|
+
|
|
|
|
+ const user = useSelector((s) => s.auth.user);
|
|
|
|
+
|
|
|
|
+ const submit = submitEvent((d) => dispatch(setData(d)));
|
|
|
|
+
|
|
|
|
+ useEffect(() => {
|
|
|
|
+ dispatch(getUsers())
|
|
|
|
+ }, [dispatch])
|
|
|
|
+
|
|
|
|
+ return (
|
|
|
|
+ <div>
|
|
<h1>MAIN PAGE</h1>
|
|
<h1>MAIN PAGE</h1>
|
|
|
|
+
|
|
|
|
+ <form onSubmit={submit} className={classes.form}>
|
|
|
|
+ {formArray.map(el => {
|
|
|
|
+ if (el.options.type === 'text' || el.options.type === 'number') {
|
|
|
|
+ return <label key={el.name} className={classes.label}>
|
|
|
|
+ <span>{el.options.label}</span>
|
|
|
|
+ <input name={el.name} type={el.options.type} value={el.value} onChange={updateEvent} />
|
|
|
|
+ </label>
|
|
|
|
+ }
|
|
|
|
+ if (el.options.type === 'text-area') {
|
|
|
|
+ return <label key={el.name} className={classes.label}>
|
|
|
|
+ <span>{el.options.label}</span>
|
|
|
|
+ <textarea name={el.name} value={el.value} onChange={updateEvent} />
|
|
|
|
+ </label>
|
|
|
|
+ }
|
|
|
|
+ if (el.options.type === 'select') {
|
|
|
|
+ return <label key={el.name} className={classes.label}>
|
|
|
|
+ <span>{el.options.label}</span>
|
|
|
|
+ <select name={el.name} value={el.value} onChange={(e) => setValueManually(el.name, e.target.value)} >
|
|
|
|
+ {el.options.options.map(e => <option key={e.id} value={e.id}>{e.text}</option>)}
|
|
|
|
+ </select>
|
|
|
|
+ </label>
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ return null;
|
|
|
|
+ })}
|
|
|
|
+
|
|
|
|
+ <button type="submit">Submit</button>
|
|
|
|
+ </form>
|
|
|
|
+
|
|
<pre>{JSON.stringify(user, null, 2)}</pre>
|
|
<pre>{JSON.stringify(user, null, 2)}</pre>
|
|
- </div>)
|
|
|
|
|
|
+ </div>
|
|
|
|
+ )
|
|
}
|
|
}
|
|
|
|
|
|
export default Main;
|
|
export default Main;
|