94 - functionnal screens

This commit is contained in:
danis
2023-01-25 17:37:10 +03:00
committed by Clément Le Bihan
parent 47629e3938
commit 1fa43555df
3 changed files with 102 additions and 51 deletions
+81 -30
View File
@@ -61,41 +61,49 @@ const baseAPIUrl =
? "/api" ? "/api"
: Constants.manifest?.extra?.apiUrl; : Constants.manifest?.extra?.apiUrl;
const requestGet = async (url: string) => { export default class API {
try {
const response = await fetch(url, { /**
*
* @param url
* @returns
*/
public static async getRequest(url: string) {
console.log('Fetching on', url)
const res = await fetch(url, {
method: 'GET', method: 'GET',
headers: { headers: {
Accept: 'application/json', 'Content-Type': 'application/json',
'Content-Type': 'application/json'
} }
}); })
const json = await response.json(); const data = await res.json()
return json; if (data.error)
} catch (error) { throw data.message;
console.error(error); return data;
} }
}
const requestPost = async (url: string, params: any[]) => { /**
try { *
const response = await fetch(url, { * @param url
method: 'POST', * @param body
* @returns
*/
public static async postRequest(url: string, body: {}) {
console.log('Fetching on', url)
const res = await fetch(url, {
method: 'PATCH',
headers: { headers: {
Accept: 'application/json', 'Content-Type': 'application/json',
'Content-Type': 'application/json'
}, },
body : JSON.stringify(params), body: JSON.stringify(body)
}); })
const json = await response.json(); const data = await res.json()
return json; if (data.error)
} catch (error) { throw data.message;
console.error(error); return data;
} }
}
export default class API { public static async fetch(params: FetchParams) {
private static async fetch(params: FetchParams) {
const jwtToken = store.getState().user.accessToken; const jwtToken = store.getState().user.accessToken;
const header = { const header = {
"Content-Type": "application/json", "Content-Type": "application/json",
@@ -163,6 +171,26 @@ export default class API {
password: registrationInput.password, password: registrationInput.password,
}); });
} }
/**
*
* @param url
* @param body
* @returns
*/
public static async patchRequest(url: string, body: {}) {
console.log('Fetching on', url)
const res = await fetch(url, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(body)
})
const data = await res.json()
if (data.error)
throw data.message;
return data;
}
/*** /***
* Retrieve information of the currently authentified user * Retrieve information of the currently authentified user
@@ -458,12 +486,35 @@ export default class API {
* @returns new user's credentials * @returns new user's credentials
*/ */
static async updateUserCredentials(dataKey: 'password' | 'email', value: any): Promise<string> { static async updateUserCredentials(dataKey: 'password' | 'email', oldValue: string, newValue: string): Promise<string> {
let validDataKeys: string[] = ['password', 'email'];
return new Promise<string>((resolve, reject) => { return new Promise<string>((resolve, reject) => {
setTimeout(() => { setTimeout(() => {
if (!validDataKeys.includes(dataKey)) try {
return resolve('giga chad'); if (dataKey == 'email') {
this.getRequest('http://localhost:3000/users/24').then((res) => {
console.table(res);
console.log(oldValue, newValue);
if (res.email === oldValue) {
this.patchRequest('http://localhost:3000/users/24', {'email': newValue}).finally(() => {
return resolve('email successfully changed');
});
} else return reject("wrong email");
});
} else if (dataKey == 'password') {
this.getRequest('http://localhost:3000/users/24').then((res) => {
console.table(res);
if (res.password == oldValue) {
this.patchRequest('http://localhost:3000/users/24', {'password': newValue}).finally(() => {
return resolve('password successfully changed');
});
} else return reject("wrong password");
});
}
} catch (error) {
console.error(error);
return reject("something went wrong: unable to update");
}
}, 1000); }, 1000);
}); });
} }
+17 -17
View File
@@ -89,23 +89,23 @@ const ChangeEmailForm = ({ onSubmit }: ChangeEmailFormProps) => {
<Button <Button
style={{ marginTop: 10 }} style={{ marginTop: 10 }}
// isLoading={submittingForm} isLoading={submittingForm}
// isDisabled={ isDisabled={
// formData.newEmail.error !== null formData.newEmail.error !== null
// } }
// onPress={async () => { onPress={async () => {
// setSubmittingForm(true); setSubmittingForm(true);
// try { try {
// const resp = await onSubmit( const resp = await onSubmit(formData.oldEmail.value,
// formData.newEmail.value formData.newEmail.value
// ); );
// toast.show({ description: resp }); toast.show({ description: resp });
// } catch (e) { } catch (e) {
// toast.show({ description: e as string }); toast.show({ description: e as string });
// } finally { } finally {
// setSubmittingForm(false); setSubmittingForm(false);
// } }
// }} }}
> >
{translate("submitBtn")} {translate("submitBtn")}
</Button> </Button>
+4 -4
View File
@@ -13,9 +13,9 @@ import ChangeEmailForm from '../../components/forms/changeEmailForm';
const SettingsStack = createNativeStackNavigator(); const SettingsStack = createNativeStackNavigator();
const handleChangeEmail = async (newEmail: string): Promise<string> => { const handleChangeEmail = async (oldEmail: string, newEmail: string): Promise<string> => {
try { try {
let response = await API.updateUserCredentials("email", newEmail); let response = await API.updateUserCredentials("email", oldEmail, newEmail);
return response as string; return response as string;
} catch (error) { } catch (error) {
return ("error: " + error); return ("error: " + error);
@@ -24,7 +24,7 @@ const handleChangeEmail = async (newEmail: string): Promise<string> => {
const handleChangePassword = async (oldPassword: string, newPassword: string): Promise<string> => { const handleChangePassword = async (oldPassword: string, newPassword: string): Promise<string> => {
try { try {
let response = await API.updateUserCredentials("password", newPassword); let response = await API.updateUserCredentials("password", oldPassword, newPassword);
return response as string; return response as string;
} catch (error) { } catch (error) {
return ("error: " + error); return ("error: " + error);
@@ -81,7 +81,7 @@ const ChangeEmailView = ({navigation}) => {
return ( return (
<Center style={{ flex: 1}}> <Center style={{ flex: 1}}>
<Heading paddingBottom={'2%'}>{translate('changeEmail')}</Heading> <Heading paddingBottom={'2%'}>{translate('changeEmail')}</Heading>
<ChangeEmailForm onSubmit={(oldEmail, newEmail) => handleChangeEmail(newEmail)}/> <ChangeEmailForm onSubmit={(oldEmail, newEmail) => handleChangeEmail(oldEmail, newEmail)}/>
<Button marginTop={'2%'} variant='outline' onPress={() => navigation.navigate('Settings')}>Back</Button> <Button marginTop={'2%'} variant='outline' onPress={() => navigation.navigate('Settings')}>Back</Button>
</Center> </Center>
) )