Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2020
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from "react";
  2. import { URL_BASE } from "./constants";
  3.  
  4. export const useDoc = doctype => {
  5.   const [doc, setDoc] = React.useState({});
  6.  
  7.   const getFields = async () => {
  8.     try {
  9.       const { document } = await fetch(`${URL_BASE}/document/${doctype}`, {
  10.         method: "GET",
  11.         headers: {
  12.           "Content-Type": "application/json",
  13.           Authorization: `Bearer ${localStorage.getItem("jwt")}`
  14.         }
  15.       })
  16.         .then(res => res.json())
  17.         .then(res => res)
  18.         .catch(err => err);
  19.  
  20.       setDoc(
  21.         (() => {
  22.           const dict = {};
  23.           for (let i = 0; i < document.fields.length; i++) {
  24.             const field = document.fields[i];
  25.             if (field.annot_field_key) {
  26.               dict[field.annot_field_key] = field;
  27.             }
  28.           }
  29.           return dict;
  30.         })()
  31.       );
  32.     } catch (error) {
  33.       console.log("No se seteo el doc quizas, no haya, o error en el server");
  34.     }
  35.   };
  36.  
  37.   React.useEffect(() => {
  38.     getFields();
  39.   }, []);
  40.  
  41.   return doc;
  42. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement