|
| 1 | +import { useState } from 'react'; |
| 2 | +import { useMutation, ApolloError } from '@apollo/client'; |
| 3 | +import { useForm, FormProvider } from 'react-hook-form'; |
| 4 | +import { CREATE_USER } from '../../utils/gql/GQL_MUTATIONS'; |
| 5 | +import { InputField } from '../Input/InputField.component'; |
| 6 | +import LoadingSpinner from '../LoadingSpinner/LoadingSpinner.component'; |
| 7 | +import Button from '../UI/Button.component'; |
| 8 | + |
| 9 | +interface IRegistrationData { |
| 10 | + username: string; |
| 11 | + email: string; |
| 12 | + password: string; |
| 13 | + firstName: string; |
| 14 | + lastName: string; |
| 15 | +} |
| 16 | + |
| 17 | +const UserRegistration = () => { |
| 18 | + const methods = useForm<IRegistrationData>(); |
| 19 | + const [registerUser, { loading, error }] = useMutation(CREATE_USER); |
| 20 | + const [registrationCompleted, setRegistrationCompleted] = useState(false); |
| 21 | + |
| 22 | + const onSubmit = async (data: IRegistrationData) => { |
| 23 | + try { |
| 24 | + const response = await registerUser({ |
| 25 | + variables: data, |
| 26 | + }); |
| 27 | + |
| 28 | + const customer = response.data?.registerCustomer?.customer; |
| 29 | + if (customer) { |
| 30 | + console.log('Customer registration successful:', customer); |
| 31 | + setRegistrationCompleted(true); |
| 32 | + } else { |
| 33 | + throw new Error('Failed to register customer'); |
| 34 | + } |
| 35 | + } catch (err: unknown) { |
| 36 | + const error = err as ApolloError; |
| 37 | + console.error('Registration error:', error); |
| 38 | + } |
| 39 | + }; |
| 40 | + |
| 41 | + if (registrationCompleted) { |
| 42 | + return ( |
| 43 | + <div className="text-center my-8"> |
| 44 | + <h2 className="text-2xl font-bold text-green-600 mb-4"> |
| 45 | + Registrering vellykket! |
| 46 | + </h2> |
| 47 | + <p>Du kan nå logge inn med din konto.</p> |
| 48 | + </div> |
| 49 | + ); |
| 50 | + } |
| 51 | + |
| 52 | + return ( |
| 53 | + <section className="text-gray-700 container p-4 py-2 mx-auto mb-[8rem] md:mb-0"> |
| 54 | + <FormProvider {...methods}> |
| 55 | + <form onSubmit={methods.handleSubmit(onSubmit)}> |
| 56 | + <div className="mx-auto lg:w-1/2 flex flex-wrap"> |
| 57 | + <InputField |
| 58 | + inputName="username" |
| 59 | + inputLabel="Brukernavn" |
| 60 | + type="text" |
| 61 | + customValidation={{ required: true }} |
| 62 | + /> |
| 63 | + <InputField |
| 64 | + inputName="email" |
| 65 | + inputLabel="E-post" |
| 66 | + type="email" |
| 67 | + customValidation={{ required: true }} |
| 68 | + /> |
| 69 | + <InputField |
| 70 | + inputName="password" |
| 71 | + inputLabel="Passord" |
| 72 | + type="password" |
| 73 | + customValidation={{ required: true }} |
| 74 | + /> |
| 75 | + <InputField |
| 76 | + inputName="firstName" |
| 77 | + inputLabel="Fornavn" |
| 78 | + type="text" |
| 79 | + customValidation={{ required: true }} |
| 80 | + /> |
| 81 | + <InputField |
| 82 | + inputName="lastName" |
| 83 | + inputLabel="Etternavn" |
| 84 | + type="text" |
| 85 | + customValidation={{ required: true }} |
| 86 | + /> |
| 87 | + |
| 88 | + {error && ( |
| 89 | + <div className="w-full p-2 text-red-600 text-sm text-center"> |
| 90 | + {error.message} |
| 91 | + </div> |
| 92 | + )} |
| 93 | + |
| 94 | + <div className="w-full p-2"> |
| 95 | + <div className="mt-4 flex justify-center"> |
| 96 | + <Button variant="primary" buttonDisabled={loading}> |
| 97 | + {loading ? <LoadingSpinner /> : 'Registrer'} |
| 98 | + </Button> |
| 99 | + </div> |
| 100 | + </div> |
| 101 | + </div> |
| 102 | + </form> |
| 103 | + </FormProvider> |
| 104 | + </section> |
| 105 | + ); |
| 106 | +}; |
| 107 | + |
| 108 | +export default UserRegistration; |
0 commit comments