chore: project structure

keep-around/af30b88d367751c9e05a735e4a0467a96238ef47
mirlan.maksitaliev 6 years ago
parent 6e9e121f93
commit 3fb655dd13
  1. 8
      src/api/operations/User/operation.graphql
  2. 3
      src/api/operations/User/operation.tsx
  3. 1
      src/api/requests/index.tsx
  4. 1
      src/api/requests/login.tsx
  5. 1
      src/config/index.tsx
  6. 1
      src/config/routes.tsx
  7. 7
      src/features/Login/index.tsx
  8. 7
      src/helpers/callApi/checkStatus.tsx
  9. 4
      src/helpers/callApi/clearUserAuthInfo.tsx
  10. 35
      src/helpers/callApi/getRequestConfig.tsx
  11. 3
      src/helpers/callApi/getResponseData.tsx
  12. 42
      src/helpers/callApi/index.tsx
  13. 1
      src/helpers/callApi/loadIdToken.tsx
  14. 1
      src/helpers/callApi/parseJSON.tsx
  15. 11
      src/helpers/callApi/removeCookie.tsx
  16. 14
      src/helpers/callApi/types.tsx
  17. 1
      src/helpers/index.tsx
  18. 1
      src/hooks/index.tsx
  19. 1
      src/hooks/usePageId.tsx
  20. 3
      src/types/index.tsx

@ -0,0 +1,8 @@
query User {
__schema {
types {
name
kind
}
}
}

@ -0,0 +1,3 @@
// autgenerated using https://graphql-code-generator.com/
export {}

@ -0,0 +1 @@
export * from './login'

@ -0,0 +1 @@
export const login = () => {}

@ -0,0 +1 @@
export * from './routes'

@ -0,0 +1 @@
export const API_ROOT = ''

@ -0,0 +1,7 @@
import React from 'react'
export const Login = () => (
<div>
Login page
</div>
)

@ -0,0 +1,7 @@
export const checkStatus = (response: Response) => {
if (!response.ok) {
return Promise.reject(new Error(response.statusText))
}
return Promise.resolve(response)
}

@ -0,0 +1,4 @@
export const clearUserAuthInfo = () => {
localStorage.removeItem('id_token')
localStorage.removeItem('AuthUser')
}

@ -0,0 +1,35 @@
import isString from 'lodash/isString'
import { loadIdToken } from './loadIdToken'
import { TRequestAbortController, TRequestConfig } from './types'
export const getRequestConfig = (
config: TRequestConfig,
abortController?: TRequestAbortController,
) => {
const requestConfig = {
method: 'POST',
...config,
headers: new Headers(),
}
if (config.body && !isString(config.body)) {
requestConfig.body = JSON.stringify(config.body)
}
if (config.body) {
requestConfig.headers.set('Content-Type', 'application/json')
}
if (abortController) {
requestConfig.signal = abortController.signal
}
const token = loadIdToken()
if (token) {
requestConfig.headers.set('x-auth-token', token)
}
return requestConfig
}

@ -0,0 +1,3 @@
export const getResponseData = (proc: string) => (response: any) => (
response?.data?.[0]?.[proc]
)

@ -0,0 +1,42 @@
import { TCallApi } from './types'
import { parseJSON } from './parseJSON'
import { checkStatus } from './checkStatus'
import { removeCookie } from './removeCookie'
import { clearUserAuthInfo } from './clearUserAuthInfo'
import { getRequestConfig } from './getRequestConfig'
export const callApi = ({
abortController,
config,
url,
}: TCallApi) => {
const requestConfig = getRequestConfig(config, abortController)
// eslint-disable-next-line no-console
console.log(
'%c callApi from module - config ',
'color: white; background-color: #95B46A',
config,
)
return fetch(url, requestConfig)
.then(checkStatus)
.then(parseJSON)
.catch((error) => {
if (error.message === 'Unauthorized') {
clearUserAuthInfo()
removeCookie('token')
if (window.location.pathname !== '/login') {
localStorage.setItem('backLocation', window.location.pathname)
window.location.pathname = '/login'
}
}
// eslint-disable-next-line no-console
console.error(error)
return Promise.reject(error)
})
}
export { getResponseData } from './getResponseData'

@ -0,0 +1 @@
export const loadIdToken = () => localStorage.getItem('id_token')

@ -0,0 +1 @@
export const parseJSON = (response: Response) => response.json()

@ -0,0 +1,11 @@
export const removeCookie = (
name: string,
domain: string = '.instatscout.com',
) => {
document.cookie = `
${name}=;
expires='Thu, 01 Jan 1970 00:00:00 UTC';
path=/;
domain=${domain}
`
}

@ -0,0 +1,14 @@
export type TRequestConfig = {
[key: string]: any,
body?: any,
headers?: Headers,
signal?: any,
}
export type TRequestAbortController = AbortController | null
export type TCallApi = {
abortController?: TRequestAbortController,
config: TRequestConfig,
url: string,
}

@ -0,0 +1 @@
export * from './callApi'

@ -0,0 +1 @@
export * from './usePageId'

@ -0,0 +1 @@
export const usePageId = () => {}

@ -0,0 +1,3 @@
// autgenerated using https://graphql-code-generator.com/
export {}
Loading…
Cancel
Save