diff --git a/src/components/User/UserRegistration.component.tsx b/src/components/User/UserRegistration.component.tsx new file mode 100644 index 000000000..45ecb7437 --- /dev/null +++ b/src/components/User/UserRegistration.component.tsx @@ -0,0 +1,108 @@ +import { useState } from 'react'; +import { useMutation, ApolloError } from '@apollo/client'; +import { useForm, FormProvider } from 'react-hook-form'; +import { CREATE_USER } from '../../utils/gql/GQL_MUTATIONS'; +import { InputField } from '../Input/InputField.component'; +import LoadingSpinner from '../LoadingSpinner/LoadingSpinner.component'; +import Button from '../UI/Button.component'; + +interface IRegistrationData { + username: string; + email: string; + password: string; + firstName: string; + lastName: string; +} + +const UserRegistration = () => { + const methods = useForm(); + const [registerUser, { loading, error }] = useMutation(CREATE_USER); + const [registrationCompleted, setRegistrationCompleted] = useState(false); + + const onSubmit = async (data: IRegistrationData) => { + try { + const response = await registerUser({ + variables: data, + }); + + const customer = response.data?.registerCustomer?.customer; + if (customer) { + console.log('Customer registration successful:', customer); + setRegistrationCompleted(true); + } else { + throw new Error('Failed to register customer'); + } + } catch (err: unknown) { + const error = err as ApolloError; + console.error('Registration error:', error); + } + }; + + if (registrationCompleted) { + return ( +
+

+ Registrering vellykket! +

+

Du kan nĂ¥ logge inn med din konto.

+
+ ); + } + + return ( +
+ +
+
+ + + + + + + {error && ( +
+ {error.message} +
+ )} + +
+
+ +
+
+
+
+
+
+ ); +}; + +export default UserRegistration;