Compare commits
5 commits
065b7fb1a4
...
268b5ab43e
| Author | SHA1 | Date | |
|---|---|---|---|
| 268b5ab43e | |||
| e3fc71a53f | |||
| 7d90c66b50 | |||
| c87d6b176b | |||
| f0f1567297 |
3 changed files with 55 additions and 5 deletions
|
|
@ -6,6 +6,8 @@ import { UserPhoto } from './UserPhoto';
|
||||||
|
|
||||||
import defaultAvatar from '@assets/userPhotoDefault.png';
|
import defaultAvatar from '@assets/userPhotoDefault.png';
|
||||||
|
|
||||||
|
import { api } from '@services/api';
|
||||||
|
|
||||||
import { useAuth } from '@hooks/useAuth';
|
import { useAuth } from '@hooks/useAuth';
|
||||||
|
|
||||||
export function HomeHeader() {
|
export function HomeHeader() {
|
||||||
|
|
@ -17,7 +19,7 @@ export function HomeHeader() {
|
||||||
return (
|
return (
|
||||||
<HStack bg="$gray600" pt="$16" pb="$5" px="$8" alignItems="center" gap="$4">
|
<HStack bg="$gray600" pt="$16" pb="$5" px="$8" alignItems="center" gap="$4">
|
||||||
<UserPhoto
|
<UserPhoto
|
||||||
source={avatar ? { uri: avatar } : defaultAvatar}
|
source={avatar ? { uri: `${api.defaults.baseURL}/avatar/${avatar}` } : defaultAvatar}
|
||||||
alt="avatar image"
|
alt="avatar image"
|
||||||
h="$16"
|
h="$16"
|
||||||
w="$16"
|
w="$16"
|
||||||
|
|
|
||||||
|
|
@ -7,7 +7,11 @@ import * as yup from 'yup';
|
||||||
import { Controller, useForm } from 'react-hook-form';
|
import { Controller, useForm } from 'react-hook-form';
|
||||||
import { yupResolver } from '@hookform/resolvers/yup';
|
import { yupResolver } from '@hookform/resolvers/yup';
|
||||||
|
|
||||||
|
import defaultAvatar from '@assets/userPhotoDefault.png';
|
||||||
|
|
||||||
import { api } from '@services/api';
|
import { api } from '@services/api';
|
||||||
|
import { AppError } from '@utils/AppError';
|
||||||
|
import { slug } from '@utils/Slug';
|
||||||
|
|
||||||
import { useAuth } from '@hooks/useAuth';
|
import { useAuth } from '@hooks/useAuth';
|
||||||
|
|
||||||
|
|
@ -16,7 +20,6 @@ import { Input } from '@components/Input';
|
||||||
import { ScreenHeader } from '@components/ScreenHeader';
|
import { ScreenHeader } from '@components/ScreenHeader';
|
||||||
import { UserPhoto } from '@components/UserPhoto';
|
import { UserPhoto } from '@components/UserPhoto';
|
||||||
import { ToastMessage } from '@components/ToastMessage';
|
import { ToastMessage } from '@components/ToastMessage';
|
||||||
import { AppError } from '@utils/AppError';
|
|
||||||
|
|
||||||
type ProfileForm = {
|
type ProfileForm = {
|
||||||
name: string;
|
name: string;
|
||||||
|
|
@ -50,7 +53,6 @@ const profileSchema = yup.object({
|
||||||
|
|
||||||
export function Profile() {
|
export function Profile() {
|
||||||
const [isUpdating, setIsUpdating] = useState(false);
|
const [isUpdating, setIsUpdating] = useState(false);
|
||||||
const [userPhoto, setUserPhoto] = useState('https://i.pravatar.cc/200');
|
|
||||||
|
|
||||||
const toast = useToast();
|
const toast = useToast();
|
||||||
const { user, updateUserData } = useAuth();
|
const { user, updateUserData } = useAuth();
|
||||||
|
|
@ -150,7 +152,37 @@ export function Profile() {
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
setUserPhoto(photoURI);
|
const fileExtension = photoSelection.assets[0].uri.split('.').pop();
|
||||||
|
const photoFile = {
|
||||||
|
name: `${slug(user.name)}.${fileExtension}`.toLowerCase(),
|
||||||
|
uri: photoSelection.assets[0].uri,
|
||||||
|
type: `${photoSelection.assets[0].type}/${fileExtension}`,
|
||||||
|
} as any;
|
||||||
|
|
||||||
|
const userPhotoUploadForm = new FormData();
|
||||||
|
userPhotoUploadForm.append('avatar', photoFile);
|
||||||
|
|
||||||
|
const avatarUpdateResponse = await api.patch('/users/avatar', userPhotoUploadForm, {
|
||||||
|
headers: {
|
||||||
|
'Content-Type': 'multipart/form-data',
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
const userUpdated = user;
|
||||||
|
userUpdated.avatar = avatarUpdateResponse.data.avatar;
|
||||||
|
updateUserData(userUpdated);
|
||||||
|
|
||||||
|
toast.show({
|
||||||
|
placement: 'top',
|
||||||
|
render: ({ id }) => (
|
||||||
|
<ToastMessage
|
||||||
|
id={id}
|
||||||
|
title="Foto do perfil atualizada com sucesso!"
|
||||||
|
action="success"
|
||||||
|
onClose={() => toast.close(id)}
|
||||||
|
/>
|
||||||
|
),
|
||||||
|
});
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.error(error);
|
console.error(error);
|
||||||
|
|
@ -163,7 +195,13 @@ export function Profile() {
|
||||||
|
|
||||||
<ScrollView contentContainerStyle={{ paddingHorizontal: 36 }}>
|
<ScrollView contentContainerStyle={{ paddingHorizontal: 36 }}>
|
||||||
<Center mt="$6" px="$10">
|
<Center mt="$6" px="$10">
|
||||||
<UserPhoto source={{ uri: userPhoto }} alt="imagem de perfil" size="xl" />
|
<UserPhoto
|
||||||
|
source={
|
||||||
|
user.avatar ? { uri: `${api.defaults.baseURL}/avatar/${user.avatar}` } : defaultAvatar
|
||||||
|
}
|
||||||
|
alt="imagem de perfil"
|
||||||
|
size="xl"
|
||||||
|
/>
|
||||||
|
|
||||||
<TouchableOpacity onPress={handleUserPhotoSelection}>
|
<TouchableOpacity onPress={handleUserPhotoSelection}>
|
||||||
<Text color="$green500" fontFamily="$heading" fontSize="$md" mt="$2" mb="$8">
|
<Text color="$green500" fontFamily="$heading" fontSize="$md" mt="$2" mb="$8">
|
||||||
|
|
|
||||||
10
src/utils/Slug.ts
Normal file
10
src/utils/Slug.ts
Normal file
|
|
@ -0,0 +1,10 @@
|
||||||
|
export function slug(str: string): string {
|
||||||
|
return str
|
||||||
|
.normalize('NFD') // Normalize accented characters to decomposed form
|
||||||
|
.replace(/[\u0300-\u036f]/g, '') // Remove diacritic marks (accents)
|
||||||
|
.toLowerCase() // Convert to lowercase
|
||||||
|
.replace(/[^a-z0-9\s-]/g, '') // Remove non-alphanumeric characters except spaces and hyphens
|
||||||
|
.trim() // Remove leading and trailing spaces
|
||||||
|
.replace(/\s+/g, '-') // Replace spaces with hyphens
|
||||||
|
.replace(/-+/g, '-'); // Replace multiple hyphens with a single hyphen
|
||||||
|
}
|
||||||
Loading…
Reference in a new issue