Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | 2x 44x 44x 44x 44x 44x 44x 44x 44x 4x 4x 3x 44x 8x 8x 8x 44x 44x 45x | /**
* RegistrationStep2 Component
* Story 1.2.3: Implement Account Creation Flow - Task 7 (GREEN Phase)
* TDD: Implementation after tests written
*/
import React from 'react';
import { useTranslation } from 'react-i18next';
import { useFormContext, Controller } from 'react-hook-form';
import {
Box,
Typography,
Button,
Checkbox,
FormControlLabel,
FormHelperText,
Link,
Alert,
Paper,
Stack,
} from '@mui/material';
import { Info } from '@mui/icons-material';
export interface RegistrationStep2Props {
onBack: () => void;
onSubmit: () => void;
isLoading: boolean;
error: Error | null;
}
export const RegistrationStep2: React.FC<RegistrationStep2Props> = ({
onBack,
onSubmit,
isLoading,
error,
}) => {
const { t } = useTranslation('auth');
const {
watch,
control,
formState: { errors },
trigger,
} = useFormContext();
// Story 12.6a: name now comes from two fields; assemble for the single-line summary.
const firstName = watch('firstName');
const lastName = watch('lastName');
const fullName = [firstName, lastName].filter(Boolean).join(' ');
const email = watch('email');
const agreedToTerms = watch('agreedToTerms');
const handleSubmit = async () => {
// Validate terms agreement
const isValid = await trigger('agreedToTerms');
if (isValid && agreedToTerms) {
onSubmit();
}
};
// Maps error codes to localized messages.
// Handles both application codes (from authService.mapCognitoError) and original Cognito names.
const getErrorMessage = (error: Error): string => {
const errorCode = error.message;
const errorMap: Record<string, string> = {
// Application codes (produced by authService.mapCognitoError)
EMAIL_EXISTS: t('register.errors.usernameExists'),
INVALID_PASSWORD: t('register.errors.passwordWeak'),
TOO_MANY_REQUESTS: t('register.errors.rateLimitExceeded'),
// Original Cognito codes (fallback)
UsernameExistsException: t('register.errors.usernameExists'),
InvalidPasswordException: t('register.errors.passwordWeak'),
InvalidParameterException: t('register.errors.invalidEmail'),
LimitExceededException: t('register.errors.rateLimitExceeded'),
TooManyRequestsException: t('register.errors.rateLimitExceeded'),
CodeDeliveryFailureException: t('register.errors.emailDeliveryFailed'),
NetworkError: t('register.errors.networkError'),
SIGNUP_FAILED: t('register.errors.signupFailed'),
};
return errorMap[errorCode] || t('register.errors.signupFailed');
};
return (
<Box>
<Typography variant="h5" gutterBottom>
{t('register.step2.title')}
</Typography>
{/* Information Summary */}
<Paper variant="outlined" sx={{ p: 3, mb: 3 }}>
<Box sx={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', mb: 2 }}>
<Typography variant="h6" gutterBottom>
{t('register.step2.reviewTitle')}
</Typography>
<Button variant="text" onClick={onBack} size="small">
{t('common:actions.edit')}
</Button>
</Box>
<Stack spacing={1}>
<Box>
<Typography variant="body2" color="text.secondary">
{t('register.step2.nameLabel')}
</Typography>
<Typography variant="body1">{fullName}</Typography>
</Box>
<Box>
<Typography variant="body2" color="text.secondary">
{t('register.step2.emailLabel')}
</Typography>
<Typography variant="body1">{email}</Typography>
</Box>
</Stack>
</Paper>
{/* Terms and Conditions Checkbox */}
<Controller
name="agreedToTerms"
control={control}
rules={{
required: t('register.errors.termsRequired'),
}}
render={({ field }) => (
<FormControlLabel
control={<Checkbox {...field} checked={field.value} />}
label={
<Typography variant="body2">
{t('register.step2.termsLabel').split('Terms of Service')[0]}
<Link href="/terms" target="_blank" rel="noopener noreferrer">
Terms of Service
</Link>
{' and '}
<Link href="/privacy" target="_blank" rel="noopener noreferrer">
Privacy Policy
</Link>
</Typography>
}
sx={{ mb: 2, alignItems: 'flex-start' }}
/>
)}
/>
{errors.agreedToTerms && (
<FormHelperText error sx={{ mt: -1, mb: 2 }}>
{errors.agreedToTerms.message as string}
</FormHelperText>
)}
{/* Newsletter Opt-in Checkbox */}
<Controller
name="newsletterOptIn"
control={control}
render={({ field }) => (
<FormControlLabel
control={<Checkbox {...field} checked={field.value} />}
label={<Typography variant="body2">{t('register.step2.newsletterLabel')}</Typography>}
sx={{ mb: 3 }}
/>
)}
/>
{/* Verification Notice */}
<Alert icon={<Info />} severity="info" sx={{ mb: 3 }}>
{t('register.step2.verificationNotice', { email })}
</Alert>
{/* Error Display */}
{error && (
<Alert severity="error" sx={{ mb: 3 }}>
{getErrorMessage(error)}
</Alert>
)}
{/* Action Buttons */}
<Box sx={{ display: 'flex', gap: 2 }}>
<Button variant="outlined" onClick={onBack} fullWidth disabled={isLoading}>
{t('common:actions.back')}
</Button>
<Button
variant="contained"
onClick={handleSubmit}
fullWidth
disabled={isLoading}
size="large"
>
{isLoading ? t('register.step2.submittingButton') : t('register.step2.submitButton')}
</Button>
</Box>
</Box>
);
};
export default RegistrationStep2;
|