Skip to content

Commit

Permalink
feat(user): add registration container
Browse files Browse the repository at this point in the history
  • Loading branch information
RCVZ committed Jul 23, 2021
1 parent a412129 commit c2479a6
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 13 deletions.
11 changes: 3 additions & 8 deletions src/components/Header/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type Props = {
onSearchButtonClick?: () => void;
onCloseSearchButtonClick?: () => void;
onLoginButtonClick?: () => void;
onSignUpButtonClick?: () => void;
toggleUserMenu: (value: boolean) => void;
children?: ReactFragment;
isLoggedIn: boolean;
Expand All @@ -45,6 +46,7 @@ const Header: React.FC<Props> = ({
searchEnabled,
onCloseSearchButtonClick,
onLoginButtonClick,
onSignUpButtonClick,
isLoggedIn,
userMenuOpen,
toggleUserMenu,
Expand Down Expand Up @@ -104,14 +106,7 @@ const Header: React.FC<Props> = ({
) : (
<div className={styles.buttonContainer}>
<Button onClick={onLoginButtonClick} label="Login" />
<Button
variant="contained"
color="primary"
onClick={() => {
'sign up';
}}
label="Sign up"
/>
<Button variant="contained" color="primary" onClick={onSignUpButtonClick} label="Sign up" />
</div>
)
) : null;
Expand Down
5 changes: 5 additions & 0 deletions src/components/Layout/Layout.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,10 @@ const Layout: FC<LayoutProps> = ({ children }) => {
history.push(addQueryParam(history, 'u', 'login'));
};

const signUpButtonClickHandler = () => {
history.push(addQueryParam(history, 'u', 'create_account'));
};

const toggleUserMenu = (value: boolean) =>
UIStore.update((state) => {
state.userMenuOpen = value;
Expand Down Expand Up @@ -111,6 +115,7 @@ const Layout: FC<LayoutProps> = ({ children }) => {
onSearchButtonClick={searchButtonClickHandler}
onCloseSearchButtonClick={closeSearchButtonClickHandler}
onLoginButtonClick={loginButtonClickHandler}
onSignUpButtonClick={signUpButtonClickHandler}
isLoggedIn={isLoggedIn}
userMenuOpen={userMenuOpen}
toggleUserMenu={toggleUserMenu}
Expand Down
55 changes: 55 additions & 0 deletions src/containers/AccountModal/forms/Registration.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import React from 'react';
import { object, string, SchemaOf, boolean } from 'yup';
import type { RegistrationFormData } from 'types/account';
import { useTranslation } from 'react-i18next';
import { useHistory } from 'react-router';

import RegistrationForm from '../../../components/RegistrationForm/RegistrationForm';
import useForm, { UseFormOnSubmitHandler } from '../../../hooks/useForm';
import { addQueryParam } from '../../../utils/history';

//todo add registration logic

const temp = (t: unknown) => {
t;
};

const Registration = () => {
const history = useHistory();
const { t } = useTranslation('account');
const loginSubmitHandler: UseFormOnSubmitHandler<RegistrationFormData> = async (formData, { setErrors, setSubmitting, setValue }) => {
try {
await temp(formData);

history.push(addQueryParam(history, 'u', 'personal_details'));
} catch (error: unknown) {
if (error instanceof Error) {
if (error.message.toLowerCase().includes('invalid param email')) {
setErrors({ email: t('registrations.invalid_email') });
} else if (error.message.toLowerCase().includes('invalid param password')) {
setErrors({ form: t('registrations.invalid_password') });
}
setValue('password', '');
}
}

setSubmitting(false);
};

const validationSchema: SchemaOf<RegistrationFormData> = object().shape({
email: string().email(t('registrations.field_is_not_valid_email')).required(t('registrations.field_required')),
password: string().required(t('registrations.field_required')),
termsConditions: boolean().required(t('registrations.field_required')),
emailUpdates: boolean().required(),
});
const initialValues: RegistrationFormData = { email: '', password: '', termsConditions: true, emailUpdates: true };
const { handleSubmit, handleChange, values, errors, submitting } = useForm<RegistrationFormData>(
initialValues,
loginSubmitHandler,
validationSchema,
);

return <RegistrationForm onSubmit={handleSubmit} onChange={handleChange} values={values} errors={errors} submitting={submitting} />;
};

export default Registration;
4 changes: 2 additions & 2 deletions src/hooks/useForm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export type UseFormReturnValue<T> = {
};

type UseFormMethods<T> = {
setValue: (key: keyof T, value: string) => void;
setValue: (key: keyof T, value: string | boolean) => void;
setErrors: (errors: FormErrors<T>) => void;
setSubmitting: (submitting: boolean) => void;
};
Expand All @@ -32,7 +32,7 @@ export default function useForm<T extends FormValues>(
const [submitting, setSubmitting] = useState(false);
const [errors, setErrors] = useState<FormErrors<T>>({});

const setValue = (name: keyof T, value: string) => {
const setValue = (name: keyof T, value: string | boolean) => {
setValues((current) => ({ ...current, [name]: value }));
};

Expand Down
12 changes: 12 additions & 0 deletions src/i18n/locales/en_US/account.json
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,17 @@
"view_password": "View password",
"wrong_combination": "Incorrect email/password combination",
"wrong_email": "Please check your email and try again."
},
"registration": {
"email": "Email",
"field_required": "This field is required",
"hide_password": "Hide password",
"password": "Password",
"sign_up": "Sign up",
"continue": "Continue",
"view_password": "View password",
"terms_conditions": "I accept the <a href={{link}}>Terms and Conditions</a> of Cleeng.",
"email_updates": "Yes, I want to receive {{siteName}} updates by email.",
"already_account": "Already have an account?"
}
}
8 changes: 6 additions & 2 deletions src/styles/_theme.scss
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ $cookies-color: $dark-color !default;
$cookies-bg: variables.$white !default;

// Form
$form-error-bg-color: #FF0C3E !default;
$form-error-bg-color: #ff0c3e !default;

// Footer

Expand Down Expand Up @@ -151,7 +151,11 @@ $text-field-hover-bg-color: rgba(variables.$white, 0.08) !default;
$text-field-active-color: variables.$white !default;
$text-field-active-border-color: variables.$white !default;
$text-field-placeholder-color: rgba(variables.$white, 0.7) !default;
$text-field-error-color: #FF0C3E !default;
$text-field-error-color: #ff0c3e !default;

// Input
$input-resting-border-color: rgba(variables.$white, 0.34) !default;
$input-hover-border-color: rgba(variables.$white, 0.7) !default;

// Toggle

Expand Down
7 changes: 7 additions & 0 deletions types/account.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,13 @@ export type LoginFormData = {
password: string;
};

export type RegistrationFormData = {
email: string;
password: string;
termsConditions: boolean;
emailUpdates: boolean;
};

export type RegisterPayload = {
email: string;
password: string;
Expand Down
2 changes: 1 addition & 1 deletion types/general.d.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
type FormValues = Record<string, string>;
type FormValues = Record<string, string | boolean>;

0 comments on commit c2479a6

Please sign in to comment.