Advertisement
Guest User

Untitled

a guest
Nov 22nd, 2019
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import axios from "axios";
  2. import { throwUtil } from "./apiUtils";
  3.  
  4. const ProjectAPI = (authContext: any) => {
  5.   const serverUrl = process.env.REACT_APP_API_ENDPOINT;
  6.  
  7.   const updateNewHeaders = (headers: any) => {
  8.     if (headers["access-token"]) authContext.setHeaders(headers);
  9.   };
  10.  
  11.   const setTokenExpired = (err: any) => {
  12.     if (err.response && err.response.status === 401)
  13.       authContext.setHeadersExpired(true);
  14.   };
  15.  
  16.   const list = async (page: number | string = 1) => {
  17.     return axios
  18.       .get(`${serverUrl}/projects?page=${page}&per_page=${12}`, {
  19.         headers: {
  20.           ...authContext.headers,
  21.           accept: "application/vnd.arpia.pro; version=1,application/json"
  22.         }
  23.       })
  24.       .then((response: any) => {
  25.         console.log(response);
  26.         updateNewHeaders(response.headers);
  27.  
  28.         return response;
  29.       })
  30.       .catch((err: any) => {
  31.         setTokenExpired(err);
  32.         throwUtil(err);
  33.       });
  34.   };
  35.  
  36.   const create = async ({
  37.     name,
  38.     client,
  39.     description,
  40.     visibility,
  41.     reference,
  42.     cover,
  43.     model
  44.   }: {
  45.     name: string;
  46.     client?: string;
  47.     description?: string;
  48.     visibility: string;
  49.     reference?: string;
  50.     cover?: string;
  51.     model?: string;
  52.   }) => {
  53.     console.log(name, visibility);
  54.     return axios
  55.       .post(
  56.         `${serverUrl}/projects`,
  57.         {
  58.           data: {
  59.             attributes: {
  60.               name,
  61.               client_name: client,
  62.               description,
  63.               visibility
  64.             },
  65.             reference,
  66.             cover,
  67.             model
  68.           }
  69.         },
  70.         {
  71.           headers: {
  72.             ...authContext.headers,
  73.             accept: "application/vnd.arpia.pro; version=1,application/json"
  74.           }
  75.         }
  76.       )
  77.       .then((response: any) => {
  78.         updateNewHeaders(response.headers);
  79.  
  80.         return response;
  81.       })
  82.       .catch((err: any) => {
  83.         setTokenExpired(err);
  84.         throwUtil(err);
  85.       });
  86.   };
  87.  
  88.   const get = async (id: string | number) => {
  89.     return axios
  90.       .get(`${serverUrl}/projects/${id}`, {
  91.         headers: {
  92.           ...authContext.headers,
  93.           accept: "application/vnd.arpia.pro; version=1,application/json"
  94.         }
  95.       })
  96.       .then((response: any) => {
  97.         console.log("projeto retornado: ", response);
  98.         updateNewHeaders(response.headers);
  99.  
  100.         return response;
  101.       })
  102.       .catch((err: any) => {
  103.         setTokenExpired(err);
  104.         throwUtil(err);
  105.       });
  106.   };
  107.  
  108.   const update = async ({
  109.     id,
  110.     reference,
  111.     cover,
  112.     model,
  113.     androidBundle,
  114.     iosBundle,
  115.     attributes: { name, client, description, visibility }
  116.   }: {
  117.     id: string | number;
  118.     reference?: string | undefined;
  119.     cover?: string | undefined;
  120.     model?: string | undefined;
  121.     androidBundle?: string;
  122.     iosBundle?: string;
  123.     attributes: {
  124.       name?: string;
  125.       client?: string;
  126.       description?: string;
  127.       visibility?: string;
  128.     };
  129.   }) => {
  130.     return axios
  131.       .put(
  132.         `${serverUrl}/projects/${id}`,
  133.         {
  134.           data: {
  135.             attributes: {
  136.               name,
  137.               client_name: client,
  138.               description,
  139.               visibility
  140.             },
  141.             cover,
  142.             model,
  143.             reference,
  144.             asset_bundle_android: androidBundle,
  145.             asset_bundle_ios: iosBundle
  146.           }
  147.         },
  148.         {
  149.           headers: {
  150.             ...authContext.headers,
  151.             accept: "application/vnd.arpia.pro; version=1,application/json"
  152.           }
  153.         }
  154.       )
  155.       .then((response: any) => {
  156.         updateNewHeaders(response.headers);
  157.  
  158.         return response;
  159.       })
  160.       .catch((err: any) => {
  161.         setTokenExpired(err);
  162.         throwUtil(err);
  163.       });
  164.   };
  165.  
  166.   const _delete = async (id: number | string) => {
  167.     return axios
  168.       .delete(`${serverUrl}/projects/${id}`, {
  169.         headers: {
  170.           ...authContext.headers,
  171.           accept: "application/vnd.arpia.pro; version=1,application/json"
  172.         }
  173.       })
  174.       .then((response: any) => {
  175.         updateNewHeaders(response.headers);
  176.  
  177.         return response;
  178.       })
  179.       .catch((err: any) => {
  180.         setTokenExpired(err);
  181.         throwUtil(err);
  182.       });
  183.   };
  184.  
  185.   return {
  186.     list,
  187.     create,
  188.     get,
  189.     update,
  190.     delete: _delete
  191.   };
  192. };
  193.  
  194. export default ProjectAPI;
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement