Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import axios from "axios";
- import { throwUtil } from "./apiUtils";
- const ProjectAPI = (authContext: any) => {
- const serverUrl = process.env.REACT_APP_API_ENDPOINT;
- const updateNewHeaders = (headers: any) => {
- if (headers["access-token"]) authContext.setHeaders(headers);
- };
- const setTokenExpired = (err: any) => {
- if (err.response && err.response.status === 401)
- authContext.setHeadersExpired(true);
- };
- const list = async (page: number | string = 1) => {
- return axios
- .get(`${serverUrl}/projects?page=${page}&per_page=${12}`, {
- headers: {
- ...authContext.headers,
- accept: "application/vnd.arpia.pro; version=1,application/json"
- }
- })
- .then((response: any) => {
- console.log(response);
- updateNewHeaders(response.headers);
- return response;
- })
- .catch((err: any) => {
- setTokenExpired(err);
- throwUtil(err);
- });
- };
- const create = async ({
- name,
- client,
- description,
- visibility,
- reference,
- cover,
- model
- }: {
- name: string;
- client?: string;
- description?: string;
- visibility: string;
- reference?: string;
- cover?: string;
- model?: string;
- }) => {
- console.log(name, visibility);
- return axios
- .post(
- `${serverUrl}/projects`,
- {
- data: {
- attributes: {
- name,
- client_name: client,
- description,
- visibility
- },
- reference,
- cover,
- model
- }
- },
- {
- headers: {
- ...authContext.headers,
- accept: "application/vnd.arpia.pro; version=1,application/json"
- }
- }
- )
- .then((response: any) => {
- updateNewHeaders(response.headers);
- return response;
- })
- .catch((err: any) => {
- setTokenExpired(err);
- throwUtil(err);
- });
- };
- const get = async (id: string | number) => {
- return axios
- .get(`${serverUrl}/projects/${id}`, {
- headers: {
- ...authContext.headers,
- accept: "application/vnd.arpia.pro; version=1,application/json"
- }
- })
- .then((response: any) => {
- console.log("projeto retornado: ", response);
- updateNewHeaders(response.headers);
- return response;
- })
- .catch((err: any) => {
- setTokenExpired(err);
- throwUtil(err);
- });
- };
- const update = async ({
- id,
- reference,
- cover,
- model,
- androidBundle,
- iosBundle,
- attributes: { name, client, description, visibility }
- }: {
- id: string | number;
- reference?: string | undefined;
- cover?: string | undefined;
- model?: string | undefined;
- androidBundle?: string;
- iosBundle?: string;
- attributes: {
- name?: string;
- client?: string;
- description?: string;
- visibility?: string;
- };
- }) => {
- return axios
- .put(
- `${serverUrl}/projects/${id}`,
- {
- data: {
- attributes: {
- name,
- client_name: client,
- description,
- visibility
- },
- cover,
- model,
- reference,
- asset_bundle_android: androidBundle,
- asset_bundle_ios: iosBundle
- }
- },
- {
- headers: {
- ...authContext.headers,
- accept: "application/vnd.arpia.pro; version=1,application/json"
- }
- }
- )
- .then((response: any) => {
- updateNewHeaders(response.headers);
- return response;
- })
- .catch((err: any) => {
- setTokenExpired(err);
- throwUtil(err);
- });
- };
- const _delete = async (id: number | string) => {
- return axios
- .delete(`${serverUrl}/projects/${id}`, {
- headers: {
- ...authContext.headers,
- accept: "application/vnd.arpia.pro; version=1,application/json"
- }
- })
- .then((response: any) => {
- updateNewHeaders(response.headers);
- return response;
- })
- .catch((err: any) => {
- setTokenExpired(err);
- throwUtil(err);
- });
- };
- return {
- list,
- create,
- get,
- update,
- delete: _delete
- };
- };
- export default ProjectAPI;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement