index.js 4.29 KB
Newer Older
d.arizona's avatar
d.arizona committed
1 2
// a library to wrap and simplify api calls
import apisauce from 'apisauce'
d.arizona's avatar
d.arizona committed
3
import Constant from '../library/Constant'
d.arizona's avatar
d.arizona committed
4 5

// our "constructor"
EKSAD's avatar
EKSAD committed
6
const create = (baseURL = 'https://trftia.eksad.com/tia-reporting-dev/public/') => {
d.arizona's avatar
d.arizona committed
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
  // ------
  // STEP 1
  // ------
  //
  // Create and configure an apisauce-based api object.
  //
  const api = apisauce.create({
    // base URL is read from the "constructor"
    baseURL,
    // here are some default headers
    headers: {
      'Cache-Control': 'no-cache',
      Accept: 'application/json',
      'Content-Type': 'application/json',
    },
    // 10 second timeout...
    timeout: 10000
  })

  api.addAsyncRequestTransform(request => async () => {
    var token
    try {
d.arizona's avatar
d.arizona committed
29
      const res = await localStorage.getItem(Constant.TOKEN)
EKSAD's avatar
EKSAD committed
30
      if (token != null) {
d.arizona's avatar
d.arizona committed
31
        token = res
d.arizona's avatar
d.arizona committed
32 33 34
        // alert(url)
        // api.setBaseURL(`${url}/api/`)
      } else {
d.arizona's avatar
d.arizona committed
35
        token = res
d.arizona's avatar
d.arizona committed
36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
        // url = Constant.BASE_URL.MASTER + '/api/'
        // alert(url)
        // api.setBaseURL(`${url}/api/`)
      }
    } catch (error) {
      // console.tron.log(error)
    }
    // console.log(token)
    request.headers['token'] = token
    // console.tron.log(url)
  })

  // ------
  // STEP 2
  // ------
  //
  // Define some functions that call the api.  The goal is to provide
  // a thin wrapper of the api layer providing nicer feeling functions
  // rather than "get", "post" and friends.
  //
  // I generally don't like wrapping the output at this level because
  // sometimes specific actions need to be take on `403` or `401`, etc.
  //
  // Since we can't hide from that, we embrace it by getting out of the
  // way at this level.
  //
  const getRoot = () => api.get('')
EKSAD's avatar
EKSAD committed
63 64

  //Auth
EKSAD's avatar
EKSAD committed
65
  const login = (body) => api.post('auth/login', body)
EKSAD's avatar
EKSAD committed
66
  const resetPassword = (body) => api.post('auth/reset_password', body)
EKSAD's avatar
EKSAD committed
67
  const verification = (body) => api.post('email/reset_password', body)
EKSAD's avatar
EKSAD committed
68
  const isResetPassword = (userId) => api.post(`auth/is_reset_password/${userId}`)
d.arizona's avatar
d.arizona committed
69 70 71
  
  //Role
  const getRole = () => api.get('role/get_all_role')
d.arizona's avatar
d.arizona committed
72
  const getDetailRole = (roleId) => api.get(`role/get_role_by_id/${roleId}`)
d.arizona's avatar
d.arizona committed
73
  const searchRole = (body) => api.post('/role/search_role', body)
d.arizona's avatar
d.arizona committed
74 75 76
  const addRole = (body) => api.post('role/create_role', body)
  const editRole = (body) => api.post('role/update_role', body)
  const deleteRole = (roleId) => api.post(`role/delete_role/${roleId}`)
d.arizona's avatar
d.arizona committed
77 78 79

  //Menu
  const getMenu = () => api.get('menu/get_menu_hierarki')
80 81 82

  //UNIT BISNIS
  const getUnitBisnis = () => api.get('business_unit/get_all_business_unit')
Deni Rinaldi's avatar
Deni Rinaldi committed
83 84 85
  const createUnitBisnis = (body) => api.post('/business_unit/create_business_unit', body)
  const updateUnitBisnis = (body) => api.post('/business_unit/update_business_unit', body)
  const searchUnitBisnis = (body) => api.post('/business_unit/search_business_unit', body) 
86

faisalhamdi's avatar
faisalhamdi committed
87
  // Perusahaan
faisalhamdi's avatar
faisalhamdi committed
88 89 90 91
  const getPerusahaan = () => api.get('company/get_all_company')
  const createPerusahaan = (body) => api.post('/company/create_company', body)
  const updatePerusahaan = (body) => api.post('/company/update_company', body)

92 93
  // APPROVAL MATRIX
  const getAM = () => api.get('approval_matrix/get_all_approval_matrix')
94 95 96 97
  const getApprovedByAM = () => api.get('approval_matrix/get_all_approver')
  const createAM = (body) => api.post('/approval_matrix/create_approval_matrix', body)
  const updateAM = (body) => api.post('/approval_matrix/update_approval_matrix', body)

faisalhamdi's avatar
faisalhamdi committed
98

d.arizona's avatar
d.arizona committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
  // ------
  // STEP 3
  // ------
  //
  // Return back a collection of functions that we would consider our
  // interface.  Most of the time it'll be just the list of all the
  // methods in step 2.
  //
  // Notice we're not returning back the `api` created in step 1?  That's
  // because it is scoped privately.  This is one way to create truly
  // private scoped goodies in JavaScript.
  //
  return {
    // a list of the API functions from step 2
    getRoot,
EKSAD's avatar
EKSAD committed
114
    login,
EKSAD's avatar
EKSAD committed
115
    verification,
EKSAD's avatar
EKSAD committed
116
    resetPassword,
EKSAD's avatar
EKSAD committed
117
    isResetPassword,
d.arizona's avatar
d.arizona committed
118
    getRole,
d.arizona's avatar
d.arizona committed
119
    getDetailRole,
d.arizona's avatar
d.arizona committed
120
    searchRole,
d.arizona's avatar
d.arizona committed
121 122 123
    addRole,
    editRole,
    deleteRole,
124
    getMenu,
Deni Rinaldi's avatar
Deni Rinaldi committed
125 126 127
    getUnitBisnis,
    createUnitBisnis,
    updateUnitBisnis,
faisalhamdi's avatar
faisalhamdi committed
128
    searchUnitBisnis,
faisalhamdi's avatar
faisalhamdi committed
129
    getAM,
faisalhamdi's avatar
faisalhamdi committed
130 131
    getPerusahaan,
    createPerusahaan,
d.arizona's avatar
d.arizona committed
132
    updatePerusahaan,
133 134 135
    getApprovedByAM,
    createAM,
    updateAM
d.arizona's avatar
d.arizona committed
136 137 138 139 140 141 142
  }
}

// let's return back our create method as the default.
export default {
  create
}