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

View File

@@ -61,41 +61,49 @@ const baseAPIUrl =
? "/api"
: Constants.manifest?.extra?.apiUrl;
const requestGet = async (url: string) => {
try {
const response = await fetch(url, {
export default class API {
/**
*
* @param url
* @returns
*/
public static async getRequest(url: string) {
console.log('Fetching on', url)
const res = await fetch(url, {
method: 'GET',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
'Content-Type': 'application/json',
}
});
const json = await response.json();
return json;
} catch (error) {
console.error(error);
})
const data = await res.json()
if (data.error)
throw data.message;
return data;
}
}
const requestPost = async (url: string, params: any[]) => {
try {
const response = await fetch(url, {
method: 'POST',
/**
*
* @param url
* @param body
* @returns
*/
public static async postRequest(url: string, body: {}) {
console.log('Fetching on', url)
const res = await fetch(url, {
method: 'PATCH',
headers: {
Accept: 'application/json',
'Content-Type': 'application/json'
'Content-Type': 'application/json',
},
body : JSON.stringify(params),
});
const json = await response.json();
return json;
} catch (error) {
console.error(error);
body: JSON.stringify(body)
})
const data = await res.json()
if (data.error)
throw data.message;
return data;
}
}
export default class API {
private static async fetch(params: FetchParams) {
public static async fetch(params: FetchParams) {
const jwtToken = store.getState().user.accessToken;
const header = {
"Content-Type": "application/json",
@@ -163,6 +171,26 @@ export default class API {
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
@@ -458,12 +486,35 @@ export default class API {
* @returns new user's credentials
*/
static async updateUserCredentials(dataKey: 'password' | 'email', value: any): Promise<string> {
let validDataKeys: string[] = ['password', 'email'];
static async updateUserCredentials(dataKey: 'password' | 'email', oldValue: string, newValue: string): Promise<string> {
return new Promise<string>((resolve, reject) => {
setTimeout(() => {
if (!validDataKeys.includes(dataKey))
return resolve('giga chad');
try {
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);
});
}

View File

@@ -89,23 +89,23 @@ const ChangeEmailForm = ({ onSubmit }: ChangeEmailFormProps) => {
<Button
style={{ marginTop: 10 }}
// isLoading={submittingForm}
// isDisabled={
// formData.newEmail.error !== null
// }
// onPress={async () => {
// setSubmittingForm(true);
// try {
// const resp = await onSubmit(
// formData.newEmail.value
// );
// toast.show({ description: resp });
// } catch (e) {
// toast.show({ description: e as string });
// } finally {
// setSubmittingForm(false);
// }
// }}
isLoading={submittingForm}
isDisabled={
formData.newEmail.error !== null
}
onPress={async () => {
setSubmittingForm(true);
try {
const resp = await onSubmit(formData.oldEmail.value,
formData.newEmail.value
);
toast.show({ description: resp });
} catch (e) {
toast.show({ description: e as string });
} finally {
setSubmittingForm(false);
}
}}
>
{translate("submitBtn")}
</Button>

View File

@@ -13,9 +13,9 @@ import ChangeEmailForm from '../../components/forms/changeEmailForm';
const SettingsStack = createNativeStackNavigator();
const handleChangeEmail = async (newEmail: string): Promise<string> => {
const handleChangeEmail = async (oldEmail: string, newEmail: string): Promise<string> => {
try {
let response = await API.updateUserCredentials("email", newEmail);
let response = await API.updateUserCredentials("email", oldEmail, newEmail);
return response as string;
} catch (error) {
return ("error: " + error);
@@ -24,7 +24,7 @@ const handleChangeEmail = async (newEmail: string): Promise<string> => {
const handleChangePassword = async (oldPassword: string, newPassword: string): Promise<string> => {
try {
let response = await API.updateUserCredentials("password", newPassword);
let response = await API.updateUserCredentials("password", oldPassword, newPassword);
return response as string;
} catch (error) {
return ("error: " + error);
@@ -81,7 +81,7 @@ const ChangeEmailView = ({navigation}) => {
return (
<Center style={{ flex: 1}}>
<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>
</Center>
)