Skip to content

Commit

Permalink
feat: add hooks
Browse files Browse the repository at this point in the history
  • Loading branch information
ialexanderbrito committed Mar 30, 2022
1 parent a89dd59 commit f37a505
Show file tree
Hide file tree
Showing 4 changed files with 223 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/hooks/useAuth.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { useContext } from 'react';

import { AuthContext } from 'contexts/Auth';

export function useAuth() {
const context = useContext(AuthContext);

return context;
}
61 changes: 61 additions & 0 deletions src/hooks/useForgetPassword.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import { useNavigate } from 'react-router-dom';

import { useFormik } from 'formik';
import { forgetPasswordSchema, resetPasswordSchema } from 'validations/ForgetPassword';

import { useToast } from 'contexts/Toast';

import { supabase } from 'services/supabase';

export function useForgetPassword() {
const navigate = useNavigate();
const { toast } = useToast();

const formikForgetPassword = useFormik({
initialValues: {
email: '',
},
validationSchema: forgetPasswordSchema,
onSubmit: async (values) => {
toast.loading('Enviando email...', { id: 'toast' });
const { error } = await supabase.auth.api.resetPasswordForEmail(values.email, {
redirectTo: 'http://localhost:3000/reset-password',
});

if (error) {
toast.error(error.message, { id: 'toast' });
return;
}

toast.success('Email enviado com sucesso!', { id: 'toast' });
navigate('/');
},
});

const formikResetPassword = useFormik({
initialValues: {
senha: '',
confirmarSenha: '',
},
validationSchema: resetPasswordSchema,
onSubmit: async (values) => {
toast.loading('Alterando senha...', { id: 'toast' });
const { error } = await supabase.auth.update({
password: values.senha,
});

if (error) {
toast.error(error.message, { id: 'toast' });
return;
}

toast.success('Senha alterada com sucesso!', { id: 'toast' });
navigate('/');
},
});

return {
formikForgetPassword,
formikResetPassword,
};
}
58 changes: 58 additions & 0 deletions src/hooks/useProfile.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { useEffect, useState } from 'react';

import { useFormik } from 'formik';
import { profileSchema } from 'validations/Profile';

import { useToast } from 'contexts/Toast';

import { useAuth } from 'hooks/useAuth';

import { supabase } from 'services/supabase';

export function useProfile() {
const { toast } = useToast();

const { user, setUser } = useAuth();
const [loading, setLoading] = useState(false);

const formikProfile = useFormik({
initialValues: {
avatar: '',
nome: '',
email: '',
},
validationSchema: profileSchema,
onSubmit: async (values) => {
setLoading(true);
const { user: userData, error } = await supabase.auth.update({
email: values.email,
data: { name: values.nome },
});

const { error: errorAvatar } = await supabase.rpc('upsert_profile_photo', {
p_user_id: user?.id,
p_src: values.avatar,
});

if (errorAvatar || error) {
toast.error('Não foi possível atualizar perfil', { id: 'toast' });
setLoading(false);
return;
}

toast.success('Perfil atualizado com sucesso', { id: 'toast' });
setLoading(false);
setUser(userData);
},
});

useEffect(() => {
formikProfile.setFieldValue('nome', user?.user_metadata.name);
formikProfile.setFieldValue('email', user?.email);
}, [user]);

return {
formikProfile,
loading,
};
}
95 changes: 95 additions & 0 deletions src/hooks/useRegister.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
import { useState } from 'react';
import { useNavigate } from 'react-router-dom';

import { useFormik } from 'formik';
import { registerSchema } from 'validations/Register';

import { useToast } from 'contexts/Toast';

import { useAuth } from 'hooks/useAuth';

import { supabase } from 'services/supabase';

export function useRegister() {
const navigate = useNavigate();
const { ocupacao } = useAuth();
const { toast } = useToast();

const [loading, setLoading] = useState(false);
const [status, setStatus] = useState<'success' | 'error' | ''>('');

const formikRegister = useFormik({
initialValues: {
nome: '',
email: '',
senha: '',
confirmarSenha: '',
ocupacao: '',
},
validationSchema: registerSchema,
onSubmit: async (values) => {
try {
setLoading(true);

const { error } = await supabase.auth.signUp(
{
email: values.email,
password: values.senha,
},
{
data: {
name: values.nome,
ocupacao: ocupacao,
},
},
);

if (error) {
toast.error(error.message, { id: 'toast' });
setStatus('error');
setLoading(false);
return;
}

setStatus('success');
setLoading(false);
} catch (error: any) {
toast.error(error?.message, { id: 'toast' });
setStatus('error');
setLoading(false);
} finally {
setTimeout(function () {
navigate('/');
}, 4000);
}
},
});

const formikLoginGoogle = useFormik({
initialValues: {
ocupacao: '',
},
validationSchema: ocupacao ? '' : registerSchema,
onSubmit: async (values) => {
const { error } = await supabase.auth.update({
data: { ocupacao: values.ocupacao },
});

if (error) {
toast.error(error.message, { id: 'toast' });
return;
}

toast.success('Função adicionada com sucesso!', { id: 'toast' });

window.location.reload();
},
});

return {
formikRegister,
formikLoginGoogle,
loading,
status,
};
}

0 comments on commit f37a505

Please sign in to comment.