'use client'; import { yupResolver } from '@hookform/resolvers/yup'; import clsx from 'clsx'; import { signIn } from 'next-auth/react'; import { useState } from 'react'; import { SubmitHandler, useForm } from 'react-hook-form'; import { useAppDispatch, useAppSelector } from '@akinon/next/redux/hooks'; import { RegisterFormType, FormType, PzSignInOptions } from '@theme/types'; import { Button, Checkbox, Icon, Input, Modal } from '@theme/components'; import * as yup from 'yup'; import { useCaptcha, useLocalization, useRouter } from '@akinon/next/hooks'; import { AuthError } from '@akinon/next/types'; import PluginModule, { Component } from '@akinon/next/components/plugin-module'; import PasswordRulesFeedback from '@theme/components/password-rules-feedback'; import { showPopup as showOtpPopup } from '@akinon/pz-otp/src/redux/reducer'; const registerFormSchema = (t) => yup.object().shape({ email: yup .string() .email(t('auth.register.form.error.email_valid')) .required(t('auth.register.form.error.required')), first_name: yup .string() .required(t('auth.register.form.error.required')) .min(2, t('auth.register.form.error.name_min')) .max(50, t('auth.register.form.error.name_max')) .matches( /^[a-zA-ZğüşöçıİĞÜŞÖÇ\s]+$/, t('auth.register.form.error.name_match') ), last_name: yup .string() .required(t('auth.register.form.error.required')) .min(2, t('auth.register.form.error.surname_min')) .max(50, t('auth.register.form.error.surname_max')) .matches( /^[a-zA-ZğüşöçıİĞÜŞÖÇ\s]+$/, t('auth.register.form.error.surname_match') ), password: yup .string() .required(t('auth.register.form.error.required')) .matches( /^(?=.*[a-z])(?=.*[A-Z])(?=.*\d)(?=.*[^a-zA-Z\d]).{6,}$/, t('auth.register.form.error.password_rule') ) .max(50, t('auth.register.form.error.password_max')), phone: yup .string() .transform((value: string) => value.replace(/_/g, '').replace(/ /g, '')) .length(11, t('auth.register.form.error.phone_length')) .required(t('auth.register.form.error.required')), confirm: yup .boolean() .oneOf([true], t('auth.register.form.error.required')), kvkk_confirm: yup .boolean() .oneOf([true], t('auth.register.form.error.required')) }); export const Register = () => { const { t, locale } = useLocalization(); const dispatch = useAppDispatch(); const [openModal, setOpenModal] = useState(false); const otpPopupVisible = useAppSelector((state) => state.otp?.isPopupVisible); const [contentModal, setContentModal] = useState({ title: undefined, description: undefined }); const STATIC_CONTENT = { content_1: { title: t('auth.register.form.agreements.membership.modal_title'), description: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum" }, content_2: { title: t('auth.register.form.agreements.kvkk.modal_title'), description: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum. Contrary to popular belief, Lorem Ipsum is not simply random text. It has roots in a piece of classical Latin literature from 45 BC, making it over 2000 years old. Richard McClintock, a Latin professor at Hampden-Sydney College in Virginia, looked up one of the more obscure Latin words, consectetur, from a Lorem Ipsum passage, and going through the cites of the word in classical literature, discovered the undoubtable source. Lorem Ipsum comes from sections 1.10.32 and 1.10.33 of 'de Finibus Bonorum et Malorum' (The Extremes of Good and Evil) by Cicero, written in 45 BC. This book is a treatise on the theory of ethics, very popular during the Renaissance. The first line of Lorem Ipsum, 'Lorem ipsum dolor sit amet..', comes from a line in section 1.10.32. The standard chunk of Lorem Ipsum used since the 1500s is reproduced below for those interested. Sections 1.10.32 and 1.10.33 from 'de Finibus Bonorum et Malorum' by Cicero are also reproduced in their exact original form, accompanied by English versions from the 1914 translation by H. Rackham." }, content_3: { title: t('auth.register.form.agreements.email_communication.modal_title'), description: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." }, content_4: { title: t('auth.register.form.agreements.sms_communication.modal_title'), description: "Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry's standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum." } }; const { register, handleSubmit, control, formState: { errors }, setError, getValues, watch } = useForm({ resolver: yupResolver(registerFormSchema(t)) }); const router = useRouter(); const passwordValue = watch('password', ''); const { CaptchaView, validated: captchaValidated, isVisible: isCaptchaVisible, validate: validateCaptcha } = useCaptcha(); const [formError, setFormError] = useState(null); const [showPassword, setShowPassword] = useState(false); const { user_phone_format } = useAppSelector((state) => state.config); const registerHandler = async (data: RegisterFormType) => { return await signIn('default', { redirect: false, callbackUrl: '/', captchaValidated, ...data, formType: FormType.register } as PzSignInOptions & { redirect: false }); }; const onSubmit: SubmitHandler = async (data) => { const registerResponse = await registerHandler(data); if (registerResponse.error) { const errors: AuthError[] = JSON.parse(registerResponse.code); if (errors.find((error) => error.type === 'captcha')) { if (await validateCaptcha()) { onSubmit(data); } return; } else if (errors.find((error) => error.type === 'otp')) { if (showOtpPopup) { dispatch(showOtpPopup()); } return; } const fieldErrors = errors.find((error) => error.type === 'field_errors') ?.data as { name: string; value: string[] }[]; const nonFieldErrors = errors.find( (error) => error.type === 'non_field_errors' )?.data as string[]; fieldErrors?.forEach((item) => { let parsedValue: Record | string[] = []; if (typeof item.value === 'string') { try { parsedValue = JSON.parse(item.value); } catch { parsedValue = [item.value]; } } else { parsedValue = item.value; } if (Array.isArray(parsedValue)) { setError(item.name as keyof RegisterFormType, { type: 'custom', message: parsedValue.join(', ') }); } else { Object.keys(parsedValue).forEach((key) => { const fieldName = key as keyof RegisterFormType; const errorMessages = parsedValue[key] as string[]; setError(fieldName, { type: 'custom', message: errorMessages.join(', ') }); }); } }); if (nonFieldErrors?.length) { setFormError(nonFieldErrors.join(', ')); } return; } if (registerResponse.url) { router.replace(registerResponse.url); return; } router.push('/'); }; const modalChange = (e, content) => { e.preventDefault(e); setContentModal(content); setOpenModal(true); }; return (
{contentModal.description}
{otpPopupVisible && ( )}
setShowPassword(!showPassword)} />
{errors.password && ( {errors.password.message} )}

{t('auth.register.form.agreements.title')}:

{t('auth.register.form.agreements.membership.label')}
{ modalChange(e, STATIC_CONTENT.content_1); }} > {t('auth.register.form.agreements.click_contract_text')}
{t('auth.register.form.agreements.kvkk.label')}
{ modalChange(e, STATIC_CONTENT.content_2); }} > {t('auth.register.form.agreements.read_approve')}
{t('auth.register.form.agreements.email_communication.label')}
{ modalChange(e, STATIC_CONTENT.content_3); }} > {t('auth.register.form.agreements.read_approve')}
{t('auth.register.form.agreements.sms_communication.label')}
{ modalChange(e, STATIC_CONTENT.content_4); }} > {t('auth.register.form.agreements.read_approve')}
{formError &&

{formError}

}

{t('auth.register.form.mandatory_fields')}

); };