feat: fixes and formatter
This commit is contained in:
+114
-121
@@ -1,230 +1,223 @@
|
||||
import * as ImagePicker from "expo-image-picker";
|
||||
import { Pressable, Text } from "react-native";
|
||||
import useMinuit from "react-native-minuit/src/hooks/useMinuit.js";
|
||||
import React, { useGlobal } from "reactn";
|
||||
import * as ImagePicker from 'expo-image-picker'
|
||||
import { Pressable, Text } from 'react-native'
|
||||
import useMinuit from 'react-native-minuit/src/hooks/useMinuit.js'
|
||||
import React, { useGlobal } from 'reactn'
|
||||
|
||||
import { responsiveWidth } from "../actions/responsiveSizes.js";
|
||||
import Page from "../layouts/Page";
|
||||
import { responsiveWidth } from '../actions/responsiveSizes.js'
|
||||
import Page from '../layouts/Page'
|
||||
|
||||
import alert from "../components/Alert.js";
|
||||
import Avatar from "../components/Avatar";
|
||||
import InputRow from "../components/InputRow";
|
||||
import ItemRowList from "../components/ItemRowList.js";
|
||||
import alert from '../components/Alert.js'
|
||||
import Avatar from '../components/Avatar'
|
||||
import InputRow from '../components/InputRow'
|
||||
import ItemRowList from '../components/ItemRowList.js'
|
||||
|
||||
import firebase, { usersRef } from "../config/firebase";
|
||||
import firebase, { usersRef } from '../config/firebase'
|
||||
|
||||
import { uploadFileToFirebase } from "../helpers/uploadToFirebase";
|
||||
import { Routes } from "../navigation/Routes.js";
|
||||
import { Fonts, gutters, Palette, Style } from "../styles";
|
||||
import { uploadFileToFirebase } from '../helpers/uploadToFirebase'
|
||||
import { Routes } from '../navigation/Routes.js'
|
||||
import { Fonts, gutters, Palette, Style } from '../styles'
|
||||
|
||||
import { useUserData } from "../providers/UserDataProvider.js";
|
||||
import { useUserData } from '../providers/UserDataProvider.js'
|
||||
|
||||
export default ({ navigation }) => {
|
||||
const { setIsLoading, setTooltip } = useMinuit();
|
||||
const { onSignOut } = useUserData();
|
||||
const { setIsLoading, setTooltip } = useMinuit()
|
||||
const { onSignOut } = useUserData()
|
||||
|
||||
const [currentUID] = useGlobal("currentUID");
|
||||
const [currentUserData] = useGlobal("currentUserData");
|
||||
const [currentUID] = useGlobal('currentUID')
|
||||
const [currentUserData] = useGlobal('currentUserData')
|
||||
|
||||
const settingsList = [
|
||||
{
|
||||
title: "Nom du compte",
|
||||
key: "name",
|
||||
value: currentUserData?.name || "",
|
||||
type: "string",
|
||||
title: 'Nom du compte',
|
||||
key: 'name',
|
||||
value: currentUserData?.name || '',
|
||||
type: 'string',
|
||||
},
|
||||
{
|
||||
title: "Adresse email",
|
||||
key: "email",
|
||||
value: currentUserData?.email || "",
|
||||
type: "string",
|
||||
title: 'Adresse email',
|
||||
key: 'email',
|
||||
value: currentUserData?.email || '',
|
||||
type: 'string',
|
||||
},
|
||||
{
|
||||
title: "Mot de passe",
|
||||
key: "password",
|
||||
value: "",
|
||||
type: "string",
|
||||
title: 'Mot de passe',
|
||||
key: 'password',
|
||||
value: '',
|
||||
type: 'string',
|
||||
},
|
||||
{
|
||||
title: "Notifications push",
|
||||
key: "notifications",
|
||||
title: 'Notifications push',
|
||||
key: 'notifications',
|
||||
value: !!currentUserData?.notifications,
|
||||
type: "boolean",
|
||||
type: 'boolean',
|
||||
},
|
||||
{
|
||||
title: "Notifications par email",
|
||||
key: "emailNotifications",
|
||||
title: 'Notifications par email',
|
||||
key: 'emailNotifications',
|
||||
value: !!currentUserData?.emailNotifications,
|
||||
type: "boolean",
|
||||
type: 'boolean',
|
||||
},
|
||||
{
|
||||
title: "Langue",
|
||||
key: "language",
|
||||
value: currentUserData?.language || "",
|
||||
type: "string",
|
||||
title: 'Langue',
|
||||
key: 'language',
|
||||
value: currentUserData?.language || '',
|
||||
type: 'string',
|
||||
},
|
||||
];
|
||||
]
|
||||
|
||||
const destructiveSettingsList = [
|
||||
{
|
||||
title: "Supprimer mon compte",
|
||||
title: 'Supprimer mon compte',
|
||||
action: () => onDeleteUserAccount(),
|
||||
textStyle: {
|
||||
color: Palette.red,
|
||||
},
|
||||
},
|
||||
].filter(({ condition = true }) => condition);
|
||||
].filter(({ condition = true }) => condition)
|
||||
|
||||
const onChangePicture = async () => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
setIsLoading(true)
|
||||
|
||||
let result = await ImagePicker.launchImageLibraryAsync({
|
||||
mediaTypes: ImagePicker.MediaTypeOptions.Images,
|
||||
allowsEditing: true,
|
||||
aspect: [4, 4],
|
||||
quality: 1,
|
||||
});
|
||||
})
|
||||
|
||||
if (result?.assets?.[0]?.uri) {
|
||||
const { resultURI = null } = await uploadFileToFirebase({
|
||||
uri: result?.assets?.[0]?.uri,
|
||||
path: `users/${currentUID}/profilePicture.png`,
|
||||
});
|
||||
})
|
||||
|
||||
if (resultURI) {
|
||||
await usersRef.doc(currentUID).update({
|
||||
profilePictureURL: resultURI,
|
||||
});
|
||||
})
|
||||
|
||||
await firebase.auth().currentUser.updateProfile({
|
||||
photoURL: resultURI,
|
||||
});
|
||||
})
|
||||
|
||||
setTooltip({
|
||||
type: "success",
|
||||
text: "Photo de profil mise à jour",
|
||||
});
|
||||
type: 'success',
|
||||
text: 'Photo de profil mise à jour',
|
||||
})
|
||||
} else {
|
||||
throw new Error("Erreur changement photo de profil");
|
||||
throw new Error('Erreur changement photo de profil')
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
console.log(error)
|
||||
|
||||
setTooltip({
|
||||
type: "error",
|
||||
type: 'error',
|
||||
text: error.message,
|
||||
});
|
||||
})
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
setIsLoading(false)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const onUpdateValue = async ({
|
||||
key,
|
||||
value = null,
|
||||
currentPassword = null,
|
||||
}) => {
|
||||
const onUpdateValue = async ({ key, value = null, currentPassword = null }) => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
const user = firebase.auth().currentUser;
|
||||
setIsLoading(true)
|
||||
const user = firebase.auth().currentUser
|
||||
|
||||
const reauthenticateUser = async () => {
|
||||
try {
|
||||
const credential = firebase.auth.EmailAuthProvider.credential(
|
||||
user?.email,
|
||||
currentPassword
|
||||
);
|
||||
)
|
||||
|
||||
await user.reauthenticateWithCredential(credential);
|
||||
await user.reauthenticateWithCredential(credential)
|
||||
} catch (error) {
|
||||
throw new Error("Erreur authentification");
|
||||
throw new Error('Erreur authentification')
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
// Validate input except for booleans where false is allowed
|
||||
const isInvalidString =
|
||||
typeof value === "string" && value.trim().length === 0;
|
||||
const isNullish = value === null || value === undefined;
|
||||
const isInvalidString = typeof value === 'string' && value.trim().length === 0
|
||||
const isNullish = value === null || value === undefined
|
||||
if (isNullish || isInvalidString) {
|
||||
throw new Error("Vérifiez les informations saisies");
|
||||
throw new Error('Vérifiez les informations saisies')
|
||||
}
|
||||
|
||||
if (key === "name") {
|
||||
if (key === 'name') {
|
||||
await user.updateProfile({
|
||||
displayName: value,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
if (["email", "password"].includes(key)) {
|
||||
await reauthenticateUser();
|
||||
if (['email', 'password'].includes(key)) {
|
||||
await reauthenticateUser()
|
||||
|
||||
if (key === "email") {
|
||||
if (key === 'email') {
|
||||
try {
|
||||
await user.updateEmail(value);
|
||||
await user.updateEmail(value)
|
||||
} catch (e) {
|
||||
throw new Error("Erreur changement email");
|
||||
throw new Error('Erreur changement email')
|
||||
}
|
||||
}
|
||||
|
||||
if (key === "password") {
|
||||
if (key === 'password') {
|
||||
try {
|
||||
await user.updatePassword(value);
|
||||
await user.updatePassword(value)
|
||||
} catch (e) {
|
||||
throw new Error("Erreur changement mot de passe");
|
||||
throw new Error('Erreur changement mot de passe')
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (key !== "password") {
|
||||
if (key !== 'password') {
|
||||
await usersRef.doc(currentUID).update({
|
||||
[key]: value,
|
||||
});
|
||||
})
|
||||
}
|
||||
|
||||
setTooltip({
|
||||
type: "success",
|
||||
text: "Modifications enregistrées",
|
||||
});
|
||||
type: 'success',
|
||||
text: 'Modifications enregistrées',
|
||||
})
|
||||
} catch (error) {
|
||||
console.log(error);
|
||||
console.log(error)
|
||||
|
||||
setTooltip({
|
||||
type: "error",
|
||||
type: 'error',
|
||||
text: error.message,
|
||||
});
|
||||
})
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
setIsLoading(false)
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
const onDeleteUserAccount = async () => {
|
||||
alert(
|
||||
"Êtes-vous sûr?",
|
||||
"La suppression de votre compte entraînera la suppression de toutes les données liées.",
|
||||
'Êtes-vous sûr?',
|
||||
'La suppression de votre compte entraînera la suppression de toutes les données liées.',
|
||||
[
|
||||
{
|
||||
text: "Annuler",
|
||||
text: 'Annuler',
|
||||
onPress: () => {},
|
||||
style: "cancel",
|
||||
style: 'cancel',
|
||||
},
|
||||
{
|
||||
text: "Confirmer",
|
||||
text: 'Confirmer',
|
||||
onPress: async () => {
|
||||
try {
|
||||
setIsLoading(true);
|
||||
setIsLoading(true)
|
||||
|
||||
await firebase
|
||||
.functions()
|
||||
.httpsCallable("users-deleteUserAccount")();
|
||||
await firebase.functions().httpsCallable('users-deleteUserAccount')()
|
||||
|
||||
await onSignOut();
|
||||
await onSignOut()
|
||||
|
||||
setTooltip({
|
||||
type: "success",
|
||||
text: "Compte supprimé avec succès!",
|
||||
});
|
||||
type: 'success',
|
||||
text: 'Compte supprimé avec succès!',
|
||||
})
|
||||
|
||||
navigation.reset({
|
||||
index: 0,
|
||||
@@ -233,25 +226,25 @@ export default ({ navigation }) => {
|
||||
name: Routes.Login,
|
||||
},
|
||||
],
|
||||
});
|
||||
})
|
||||
} catch (error) {
|
||||
setTooltip({
|
||||
type: "error",
|
||||
type: 'error',
|
||||
text: error.message,
|
||||
});
|
||||
})
|
||||
} finally {
|
||||
setIsLoading(false);
|
||||
setIsLoading(false)
|
||||
}
|
||||
},
|
||||
style: "confirm",
|
||||
style: 'confirm',
|
||||
},
|
||||
],
|
||||
{ cancelable: false }
|
||||
);
|
||||
};
|
||||
)
|
||||
}
|
||||
|
||||
return (
|
||||
<Page headerType="NAVIGATE" title={"Paramètres du compte"} scrollEnabled>
|
||||
<Page headerType="NAVIGATE" title={'Paramètres du compte'} scrollEnabled>
|
||||
<Pressable
|
||||
onPress={onChangePicture}
|
||||
style={[Style.containerCenter, { marginBottom: gutters }]}
|
||||
@@ -268,8 +261,8 @@ export default ({ navigation }) => {
|
||||
/>
|
||||
<Text
|
||||
style={Fonts({
|
||||
type: "section",
|
||||
style: { color: Palette.primary, textDecorationLine: "underline" },
|
||||
type: 'section',
|
||||
style: { color: Palette.primary, textDecorationLine: 'underline' },
|
||||
})}
|
||||
>
|
||||
Modifier
|
||||
@@ -277,7 +270,7 @@ export default ({ navigation }) => {
|
||||
</Pressable>
|
||||
|
||||
{settingsList.map((setting) => {
|
||||
const { title, key, value, type } = setting;
|
||||
const { title, key, value, type } = setting
|
||||
|
||||
return (
|
||||
<InputRow
|
||||
@@ -288,11 +281,11 @@ export default ({ navigation }) => {
|
||||
title={title}
|
||||
onUpdateValue={onUpdateValue}
|
||||
/>
|
||||
);
|
||||
)
|
||||
})}
|
||||
|
||||
<ItemRowList
|
||||
title={"Gérer mon abonnement"}
|
||||
title={'Gérer mon abonnement'}
|
||||
action={() => navigation.navigate(Routes.ManageSubscription)}
|
||||
containerStyle={{}}
|
||||
/>
|
||||
@@ -301,5 +294,5 @@ export default ({ navigation }) => {
|
||||
<ItemRowList key={index} {...setting} containerStyle={{}} />
|
||||
))}
|
||||
</Page>
|
||||
);
|
||||
};
|
||||
)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user