Merge pull request 'feature-upload-photo' (#1) from feature-upload-photo into main
Reviewed-on: http://192.168.0.232:3000/learning/ignitegym/pulls/1 Reviewed-by: dev <dev@viniciussouza.me>
This commit is contained in:
commit
268b5ab43e
3 changed files with 55 additions and 5 deletions
|
|
@ -6,6 +6,8 @@ import { UserPhoto } from './UserPhoto';
|
|||
|
||||
import defaultAvatar from '@assets/userPhotoDefault.png';
|
||||
|
||||
import { api } from '@services/api';
|
||||
|
||||
import { useAuth } from '@hooks/useAuth';
|
||||
|
||||
export function HomeHeader() {
|
||||
|
|
@ -17,7 +19,7 @@ export function HomeHeader() {
|
|||
return (
|
||||
<HStack bg="$gray600" pt="$16" pb="$5" px="$8" alignItems="center" gap="$4">
|
||||
<UserPhoto
|
||||
source={avatar ? { uri: avatar } : defaultAvatar}
|
||||
source={avatar ? { uri: `${api.defaults.baseURL}/avatar/${avatar}` } : defaultAvatar}
|
||||
alt="avatar image"
|
||||
h="$16"
|
||||
w="$16"
|
||||
|
|
|
|||
|
|
@ -7,7 +7,11 @@ import * as yup from 'yup';
|
|||
import { Controller, useForm } from 'react-hook-form';
|
||||
import { yupResolver } from '@hookform/resolvers/yup';
|
||||
|
||||
import defaultAvatar from '@assets/userPhotoDefault.png';
|
||||
|
||||
import { api } from '@services/api';
|
||||
import { AppError } from '@utils/AppError';
|
||||
import { slug } from '@utils/Slug';
|
||||
|
||||
import { useAuth } from '@hooks/useAuth';
|
||||
|
||||
|
|
@ -16,7 +20,6 @@ import { Input } from '@components/Input';
|
|||
import { ScreenHeader } from '@components/ScreenHeader';
|
||||
import { UserPhoto } from '@components/UserPhoto';
|
||||
import { ToastMessage } from '@components/ToastMessage';
|
||||
import { AppError } from '@utils/AppError';
|
||||
|
||||
type ProfileForm = {
|
||||
name: string;
|
||||
|
|
@ -50,7 +53,6 @@ const profileSchema = yup.object({
|
|||
|
||||
export function Profile() {
|
||||
const [isUpdating, setIsUpdating] = useState(false);
|
||||
const [userPhoto, setUserPhoto] = useState('https://i.pravatar.cc/200');
|
||||
|
||||
const toast = useToast();
|
||||
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) {
|
||||
console.error(error);
|
||||
|
|
@ -163,7 +195,13 @@ export function Profile() {
|
|||
|
||||
<ScrollView contentContainerStyle={{ paddingHorizontal: 36 }}>
|
||||
<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}>
|
||||
<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