Skip to content

Commit

Permalink
fix: imports services
Browse files Browse the repository at this point in the history
  • Loading branch information
ialexanderbrito committed Mar 31, 2022
1 parent bb45291 commit f942aed
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 38 deletions.
11 changes: 4 additions & 7 deletions src/hooks/useForgetPassword.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { forgetPasswordSchema, resetPasswordSchema } from 'validations/ForgetPas

import { useToast } from 'contexts/Toast';

import { supabase } from 'services/supabase';
import { resetPassword } from 'services/post/resetPassword';
import { updatePassword } from 'services/update/password';

export function useForgetPassword() {
const navigate = useNavigate();
Expand All @@ -18,9 +19,7 @@ export function useForgetPassword() {
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',
});
const { error } = await resetPassword(values.email);

if (error) {
toast.error(error.message, { id: 'toast' });
Expand All @@ -40,9 +39,7 @@ export function useForgetPassword() {
validationSchema: resetPasswordSchema,
onSubmit: async (values) => {
toast.loading('Alterando senha...', { id: 'toast' });
const { error } = await supabase.auth.update({
password: values.senha,
});
const { error } = await updatePassword(values.senha);

if (error) {
toast.error(error.message, { id: 'toast' });
Expand Down
36 changes: 23 additions & 13 deletions src/hooks/useProfile.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@ import { useToast } from 'contexts/Toast';

import { useAuth } from 'hooks/useAuth';

import { supabase } from 'services/supabase';
import { updateProfile } from 'services/update/profile';
import { updateProfilePhoto } from 'services/update/profileAvatar';

export function useProfile() {
const { toast } = useToast();
Expand All @@ -27,18 +28,27 @@ export function useProfile() {
validationSchema: profileSchema,
onSubmit: async (values) => {
setLoading(true);
const { user: userData, error } = await supabase.auth.update({
email: values.email,
password: values.password === '' ? undefined : values.newPassword,
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) {

const { user: userData, error } = await updateProfile(
values.email,
values.password,
values.newPassword,
values.nome,
);

if (!userData) return;

if (values.avatar !== '') {
const { error } = await updateProfilePhoto(userData.id, values.avatar);

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

if (error) {
toast.error('Não foi possível atualizar perfil', { id: 'toast' });
setLoading(false);
return;
Expand Down
26 changes: 8 additions & 18 deletions src/hooks/useRegister.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ import { useToast } from 'contexts/Toast';

import { useAuth } from 'hooks/useAuth';

import { supabase } from 'services/supabase';
import { registerUser } from 'services/post/register';
import { updateOcupacao } from 'services/update/ocupacao';

export function useRegister() {
const navigate = useNavigate();
Expand All @@ -31,18 +32,7 @@ export function useRegister() {
try {
setLoading(true);

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

if (error) {
toast.error(error.message, { id: 'toast' });
Expand All @@ -53,8 +43,10 @@ export function useRegister() {

setStatus('success');
setLoading(false);
} catch (error: any) {
toast.error(error?.message, { id: 'toast' });
} catch (error) {
const { message } = error as Error;

toast.error(message, { id: 'toast' });
setStatus('error');
setLoading(false);
} finally {
Expand All @@ -71,9 +63,7 @@ export function useRegister() {
},
validationSchema: ocupacao ? '' : registerSchema,
onSubmit: async (values) => {
const { error } = await supabase.auth.update({
data: { ocupacao: values.ocupacao },
});
const { error } = await updateOcupacao(values.ocupacao);

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

0 comments on commit f942aed

Please sign in to comment.