```
add authentication routes, components, and pages including AuthHome, Login, and SignUp, and implement form fields utility functions ```
This commit is contained in:
71
002_source/ionic_mobile/src/data/fields.tsx
Normal file
71
002_source/ionic_mobile/src/data/fields.tsx
Normal file
@@ -0,0 +1,71 @@
|
||||
import { useFormInput } from './utils';
|
||||
|
||||
export const useSignupFields = () => {
|
||||
return [
|
||||
{
|
||||
id: 'name',
|
||||
label: 'Name',
|
||||
required: true,
|
||||
input: {
|
||||
props: {
|
||||
type: 'text',
|
||||
placeholder: 'Joe Bloggs',
|
||||
},
|
||||
state: useFormInput(''),
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'email',
|
||||
label: 'Email',
|
||||
required: true,
|
||||
input: {
|
||||
props: {
|
||||
type: 'email',
|
||||
placeholder: 'joe@bloggs.com',
|
||||
},
|
||||
state: useFormInput(''),
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'password',
|
||||
label: 'Password',
|
||||
required: true,
|
||||
input: {
|
||||
props: {
|
||||
type: 'password',
|
||||
placeholder: '*********',
|
||||
},
|
||||
state: useFormInput(''),
|
||||
},
|
||||
},
|
||||
];
|
||||
};
|
||||
|
||||
export const useLoginFields = () => {
|
||||
return [
|
||||
{
|
||||
id: 'email',
|
||||
label: 'Email',
|
||||
required: true,
|
||||
input: {
|
||||
props: {
|
||||
type: 'email',
|
||||
placeholder: 'joe@bloggs.com',
|
||||
},
|
||||
state: useFormInput(''),
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'password',
|
||||
label: 'Password',
|
||||
required: true,
|
||||
input: {
|
||||
props: {
|
||||
type: 'password',
|
||||
placeholder: '*******',
|
||||
},
|
||||
state: useFormInput(''),
|
||||
},
|
||||
},
|
||||
];
|
||||
};
|
38
002_source/ionic_mobile/src/data/utils.tsx
Normal file
38
002_source/ionic_mobile/src/data/utils.tsx
Normal file
@@ -0,0 +1,38 @@
|
||||
import { useState } from 'react';
|
||||
|
||||
export const useFormInput = (initialValue = '') => {
|
||||
const [value, setValue] = useState(initialValue);
|
||||
|
||||
const handleChange = async (e) => {
|
||||
const tempValue = await e.currentTarget.value;
|
||||
setValue(tempValue);
|
||||
};
|
||||
|
||||
return {
|
||||
value,
|
||||
reset: (newValue) => setValue(newValue),
|
||||
onIonChange: handleChange,
|
||||
onKeyUp: handleChange,
|
||||
};
|
||||
};
|
||||
|
||||
export const validateForm = (fields) => {
|
||||
let errors = [];
|
||||
|
||||
fields.forEach((field) => {
|
||||
if (field.required) {
|
||||
const fieldValue = field.input.state.value;
|
||||
|
||||
if (fieldValue === '') {
|
||||
const error = {
|
||||
id: field.id,
|
||||
message: `Please check your ${field.id}`,
|
||||
};
|
||||
|
||||
errors.push(error);
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
return errors;
|
||||
};
|
Reference in New Issue
Block a user