Skip to content

Instantly share code, notes, and snippets.

@enaluz
Last active April 29, 2020 22:59
Show Gist options
  • Save enaluz/8620a395ffdd52485a2671b28ba4a949 to your computer and use it in GitHub Desktop.
Save enaluz/8620a395ffdd52485a2671b28ba4a949 to your computer and use it in GitHub Desktop.
const openTakePhoto = async () => {
onCloseUploadImage()
try {
const image = await ImagePicker.openCamera(imageUploadOptions) as ImagePickerResult;
if (!image) return showBanner({ message: "Image upload failed. Please try again.", type: "danger" })
setState({
...state,
avatar: image.path,
fileType: image.mime
});
} catch (err) { ... }
}
const save = async () => {
const { firstName, lastName, avatar, fileType } = state;
const data = new FormData();
data.append("avatar", {
name: "avatar",
type: fileType,
uri: Platform.OS === "ios" ? avatar.replace("file://", "") : avatar
})
data.append("firstName", firstName)
data.append("lastName", lastName)
const response = await request({
authToken,
url: "/user/settings/account",
method: "PUT",
body: data
})
setLoading(false);
if (response.ok) {
const { firstName, lastName, avatar } = response.data;
global.setState({ firstName, lastName, avatar });
await writeCacheMulti([
[firstNameType, firstName],
[lastNameType, lastName],
[avatarType, avatar],
]);
return props.navigation.goBack();
} else {
if (response.status === 409) {
showBanner({
message: "Please sign in.",
type: "danger",
});
await deleteCacheAll();
return global.setState({ isLoggedIn: false });
}
return showBanner({
message: "An error occurred. Please try saving again.",
type: "danger",
});
}
};
import axios, {
AxiosResponse as BaseAxiosResponse,
Method,
} from "axios";
import ENV from "../../env";
import { bugsnag } from "../utils/bugsnag";
import { checkInternetConnection } from 'react-native-offline';
import { showBanner } from './banner';
export interface AxiosResponse extends BaseAxiosResponse {
ok: boolean;
}
export interface RequestArgs {
authToken: string;
url: string;
method: Method;
body?: any;
optionalHeaders?: any;
params?: any;
onUploadProgress?: (progressEvent: any) => void;
}
const request = async ({
authToken,
url,
method,
body,
params,
optionalHeaders,
}: RequestArgs): Promise<AxiosResponse> => {
let response: AxiosResponse;
try {
bugsnag.leaveBreadcrumb("request", {
type: "request",
endpoint: url,
method,
});
const internetActive = await checkInternetConnection()
if (!internetActive) {
showBanner({
message: "Your internet connection is off 📡",
description: "Please attempt to reconnect to the Internet.",
type: "danger"
})
}
response = {
ok: true,
...await axios({
url,
method: method,
data: body,
params,
baseURL: ENV.API_BASE_URL,
headers: authToken ? { Authorization: `Bearer ${authToken}` } : {},
timeout: 6000,
...optionalHeaders,
})
}
return response;
} catch (err) {
if (err.response) {
err.response.ok = false;
return err.response;
} else {
err.ok = false
return err
}
}
};
export { request };
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment