From 3fb655dd1328e36910fc89ec0020d1f8e30c9f62 Mon Sep 17 00:00:00 2001 From: "mirlan.maksitaliev" Date: Fri, 29 May 2020 19:21:09 +0600 Subject: [PATCH 1/7] chore: project structure --- src/api/operations/User/operation.graphql | 8 +++++ src/api/operations/User/operation.tsx | 3 ++ src/api/requests/index.tsx | 1 + src/api/requests/login.tsx | 1 + src/config/index.tsx | 1 + src/config/routes.tsx | 1 + src/features/Login/index.tsx | 7 ++++ src/helpers/callApi/checkStatus.tsx | 7 ++++ src/helpers/callApi/clearUserAuthInfo.tsx | 4 +++ src/helpers/callApi/getRequestConfig.tsx | 35 +++++++++++++++++++ src/helpers/callApi/getResponseData.tsx | 3 ++ src/helpers/callApi/index.tsx | 42 +++++++++++++++++++++++ src/helpers/callApi/loadIdToken.tsx | 1 + src/helpers/callApi/parseJSON.tsx | 1 + src/helpers/callApi/removeCookie.tsx | 11 ++++++ src/helpers/callApi/types.tsx | 14 ++++++++ src/helpers/index.tsx | 1 + src/hooks/index.tsx | 1 + src/hooks/usePageId.tsx | 1 + src/types/index.tsx | 3 ++ 20 files changed, 146 insertions(+) create mode 100644 src/api/operations/User/operation.graphql create mode 100644 src/api/operations/User/operation.tsx create mode 100644 src/api/requests/index.tsx create mode 100644 src/api/requests/login.tsx create mode 100644 src/config/index.tsx create mode 100644 src/config/routes.tsx create mode 100644 src/features/Login/index.tsx create mode 100644 src/helpers/callApi/checkStatus.tsx create mode 100644 src/helpers/callApi/clearUserAuthInfo.tsx create mode 100644 src/helpers/callApi/getRequestConfig.tsx create mode 100644 src/helpers/callApi/getResponseData.tsx create mode 100644 src/helpers/callApi/index.tsx create mode 100644 src/helpers/callApi/loadIdToken.tsx create mode 100644 src/helpers/callApi/parseJSON.tsx create mode 100644 src/helpers/callApi/removeCookie.tsx create mode 100644 src/helpers/callApi/types.tsx create mode 100644 src/helpers/index.tsx create mode 100644 src/hooks/index.tsx create mode 100644 src/hooks/usePageId.tsx create mode 100644 src/types/index.tsx diff --git a/src/api/operations/User/operation.graphql b/src/api/operations/User/operation.graphql new file mode 100644 index 00000000..14ec1c2c --- /dev/null +++ b/src/api/operations/User/operation.graphql @@ -0,0 +1,8 @@ +query User { + __schema { + types { + name + kind + } + } +} diff --git a/src/api/operations/User/operation.tsx b/src/api/operations/User/operation.tsx new file mode 100644 index 00000000..bbbcc5b7 --- /dev/null +++ b/src/api/operations/User/operation.tsx @@ -0,0 +1,3 @@ +// autgenerated using https://graphql-code-generator.com/ + +export {} diff --git a/src/api/requests/index.tsx b/src/api/requests/index.tsx new file mode 100644 index 00000000..d4389750 --- /dev/null +++ b/src/api/requests/index.tsx @@ -0,0 +1 @@ +export * from './login' diff --git a/src/api/requests/login.tsx b/src/api/requests/login.tsx new file mode 100644 index 00000000..627dbbf4 --- /dev/null +++ b/src/api/requests/login.tsx @@ -0,0 +1 @@ +export const login = () => {} diff --git a/src/config/index.tsx b/src/config/index.tsx new file mode 100644 index 00000000..49800c7a --- /dev/null +++ b/src/config/index.tsx @@ -0,0 +1 @@ +export * from './routes' diff --git a/src/config/routes.tsx b/src/config/routes.tsx new file mode 100644 index 00000000..ff4452e0 --- /dev/null +++ b/src/config/routes.tsx @@ -0,0 +1 @@ +export const API_ROOT = '' diff --git a/src/features/Login/index.tsx b/src/features/Login/index.tsx new file mode 100644 index 00000000..741eb3d8 --- /dev/null +++ b/src/features/Login/index.tsx @@ -0,0 +1,7 @@ +import React from 'react' + +export const Login = () => ( +
+ Login page +
+) diff --git a/src/helpers/callApi/checkStatus.tsx b/src/helpers/callApi/checkStatus.tsx new file mode 100644 index 00000000..4b3b206a --- /dev/null +++ b/src/helpers/callApi/checkStatus.tsx @@ -0,0 +1,7 @@ +export const checkStatus = (response: Response) => { + if (!response.ok) { + return Promise.reject(new Error(response.statusText)) + } + + return Promise.resolve(response) +} diff --git a/src/helpers/callApi/clearUserAuthInfo.tsx b/src/helpers/callApi/clearUserAuthInfo.tsx new file mode 100644 index 00000000..c48925f4 --- /dev/null +++ b/src/helpers/callApi/clearUserAuthInfo.tsx @@ -0,0 +1,4 @@ +export const clearUserAuthInfo = () => { + localStorage.removeItem('id_token') + localStorage.removeItem('AuthUser') +} diff --git a/src/helpers/callApi/getRequestConfig.tsx b/src/helpers/callApi/getRequestConfig.tsx new file mode 100644 index 00000000..8bbf113e --- /dev/null +++ b/src/helpers/callApi/getRequestConfig.tsx @@ -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 +} diff --git a/src/helpers/callApi/getResponseData.tsx b/src/helpers/callApi/getResponseData.tsx new file mode 100644 index 00000000..149ace17 --- /dev/null +++ b/src/helpers/callApi/getResponseData.tsx @@ -0,0 +1,3 @@ +export const getResponseData = (proc: string) => (response: any) => ( + response?.data?.[0]?.[proc] +) diff --git a/src/helpers/callApi/index.tsx b/src/helpers/callApi/index.tsx new file mode 100644 index 00000000..00a38273 --- /dev/null +++ b/src/helpers/callApi/index.tsx @@ -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' diff --git a/src/helpers/callApi/loadIdToken.tsx b/src/helpers/callApi/loadIdToken.tsx new file mode 100644 index 00000000..c1f70feb --- /dev/null +++ b/src/helpers/callApi/loadIdToken.tsx @@ -0,0 +1 @@ +export const loadIdToken = () => localStorage.getItem('id_token') diff --git a/src/helpers/callApi/parseJSON.tsx b/src/helpers/callApi/parseJSON.tsx new file mode 100644 index 00000000..12709130 --- /dev/null +++ b/src/helpers/callApi/parseJSON.tsx @@ -0,0 +1 @@ +export const parseJSON = (response: Response) => response.json() diff --git a/src/helpers/callApi/removeCookie.tsx b/src/helpers/callApi/removeCookie.tsx new file mode 100644 index 00000000..4118169c --- /dev/null +++ b/src/helpers/callApi/removeCookie.tsx @@ -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} + ` +} diff --git a/src/helpers/callApi/types.tsx b/src/helpers/callApi/types.tsx new file mode 100644 index 00000000..cb2b105a --- /dev/null +++ b/src/helpers/callApi/types.tsx @@ -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, +} diff --git a/src/helpers/index.tsx b/src/helpers/index.tsx new file mode 100644 index 00000000..8842e651 --- /dev/null +++ b/src/helpers/index.tsx @@ -0,0 +1 @@ +export * from './callApi' diff --git a/src/hooks/index.tsx b/src/hooks/index.tsx new file mode 100644 index 00000000..cd448150 --- /dev/null +++ b/src/hooks/index.tsx @@ -0,0 +1 @@ +export * from './usePageId' diff --git a/src/hooks/usePageId.tsx b/src/hooks/usePageId.tsx new file mode 100644 index 00000000..7d6a8899 --- /dev/null +++ b/src/hooks/usePageId.tsx @@ -0,0 +1 @@ +export const usePageId = () => {} diff --git a/src/types/index.tsx b/src/types/index.tsx new file mode 100644 index 00000000..bbbcc5b7 --- /dev/null +++ b/src/types/index.tsx @@ -0,0 +1,3 @@ +// autgenerated using https://graphql-code-generator.com/ + +export {} From 529181c021921e1a6b270f415d024d01b4caeb7a Mon Sep 17 00:00:00 2001 From: "mirlan.maksitaliev" Date: Mon, 1 Jun 2020 20:37:36 +0600 Subject: [PATCH 2/7] chore: added code generation config --- codegen.yml | 22 ++++++++++++++++++++++ package.json | 16 +++++++++++++++- 2 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 codegen.yml diff --git a/codegen.yml b/codegen.yml new file mode 100644 index 00000000..7801e848 --- /dev/null +++ b/codegen.yml @@ -0,0 +1,22 @@ +overwrite: true +schema: '' +documents: src/api/operations/**/*.graphql +generates: + src/types/index.tsx: + plugins: + - typescript + src/: + preset: + near-operation-file + presetConfig: + extension: .tsx + baseTypesPath: types + plugins: + - typescript-operations + - typescript-react-apollo + config: + withComponent: false + withHOC: false + withHooks: true +config: + typesPrefix: T diff --git a/package.json b/package.json index a4d7cab2..f1c2b61c 100644 --- a/package.json +++ b/package.json @@ -3,12 +3,19 @@ "version": "0.1.0", "private": true, "scripts": { + "prestart ": "yarn generate", "start": "react-scripts start", "build": "GENERATE_SOURCEMAP=false react-scripts build && gzipper --verbose ./build", "test": "react-scripts test --passWithNoTests --watchAll=false", - "eject": "react-scripts eject" + "eject": "react-scripts eject", + "generate": "graphql-codegen --config codegen.yml" }, "dependencies": { + "@apollo/react-common": "^3.1.4", + "@apollo/react-hooks": "^3.1.5", + "apollo-boost": "^0.4.9", + "graphql": "^15.0.0", + "lodash": "^4.17.15", "react": "^16.13.1", "react-dom": "^16.13.1", "react-scripts": "3.4.1" @@ -16,10 +23,17 @@ "devDependencies": { "@commitlint/cli": "^8.3.5", "@commitlint/config-conventional": "^8.3.4", + "@graphql-codegen/cli": "1.15.0", + "@graphql-codegen/import-types-preset": "^1.15.0", + "@graphql-codegen/near-operation-file-preset": "^1.15.0", + "@graphql-codegen/typescript": "1.15.0", + "@graphql-codegen/typescript-operations": "1.15.0", + "@graphql-codegen/typescript-react-apollo": "1.15.0", "@testing-library/jest-dom": "^4.2.4", "@testing-library/react": "^9.3.2", "@testing-library/user-event": "^7.1.2", "@types/jest": "^24.0.0", + "@types/lodash": "^4.14.154", "@types/node": "^12.0.0", "@types/react": "^16.9.0", "@types/react-dom": "^16.9.0", From 73f9b545e2629c7bd5d209f60ac5f5ab977018f3 Mon Sep 17 00:00:00 2001 From: "mirlan.maksitaliev" Date: Mon, 1 Jun 2020 20:39:32 +0600 Subject: [PATCH 3/7] feat: apollo client config --- src/config/routes.tsx | 1 + src/index.tsx | 13 +++++++++++-- 2 files changed, 12 insertions(+), 2 deletions(-) diff --git a/src/config/routes.tsx b/src/config/routes.tsx index ff4452e0..2f03b423 100644 --- a/src/config/routes.tsx +++ b/src/config/routes.tsx @@ -1 +1,2 @@ export const API_ROOT = '' +export const GRAPHQL = `${API_ROOT}/graphql` diff --git a/src/index.tsx b/src/index.tsx index 98389c5f..93c34645 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -1,15 +1,24 @@ import React from 'react' import ReactDOM from 'react-dom' +import ApolloClient from 'apollo-boost' +import { ApolloProvider } from '@apollo/react-hooks' +import { GRAPHQL } from 'config' import { App } from 'features/App' import * as serviceWorker from './serviceWorker' +const client = new ApolloClient({ + uri: GRAPHQL, +}) + ReactDOM.render( - + + + , - document.getElementById('root') + document.getElementById('root'), ) // If you want your app to work offline and load faster, you can change From 11008d279d4897b2f130b4ec96ad08192d40bb66 Mon Sep 17 00:00:00 2001 From: "mirlan.maksitaliev" Date: Tue, 2 Jun 2020 16:05:41 +0600 Subject: [PATCH 4/7] chore: eslint configuration --- .eslintignore | 4 +++ .eslintrc | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++ package.json | 15 ++++++--- 3 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 .eslintignore create mode 100644 .eslintrc diff --git a/.eslintignore b/.eslintignore new file mode 100644 index 00000000..e56874cc --- /dev/null +++ b/.eslintignore @@ -0,0 +1,4 @@ +src/types/* +src/api/operations/*/operation.tsx +src/react-app-env.d.ts +src/serviceWorker.ts diff --git a/.eslintrc b/.eslintrc new file mode 100644 index 00000000..d821bc4b --- /dev/null +++ b/.eslintrc @@ -0,0 +1,84 @@ +{ + "extends": [ + "eslint:recommended", + "plugin:react/recommended", + "airbnb", + "react-app" + ], + "plugins": [ + "sort-destructure-keys", + "typescript-sort-keys", + "postro4no" + ], + "rules": { + "@typescript-eslint/array-type": [ + "warn", + { "default" : "generic" } + ], + "@typescript-eslint/indent": ["warn", 2], + "@typescript-eslint/member-delimiter-style": ["warn", { + "multiline": { + "delimiter": "comma", + "requireLast": true + }, + "singleline": { + "delimiter": "comma", + "requireLast": false + } + }], + "import/extensions": [ + "warn", + "never", + { + "svg": "always", + "css": "always", + "scss": "always" + } + ], + "import/no-duplicates": "warn", + "import/no-extraneous-dependencies": [ + "warn", + { "devDependencies": ["src/setupTests.ts"] } + ], + "jsx-quotes": [ + "warn", + "prefer-single" + ], + "no-param-reassign": [ + "warn", + { + "props": true, + "ignorePropertyModificationsFor": ["self", "acc"] + } + ], + "postro4no/function-args": "warn", + "postro4no/import": "warn", + "postro4no/object-props": "warn", + "react/jsx-filename-extension": [ + "warn", + { "extensions": [".tsx"] } + ], + "react/jsx-indent": ["warn", 2], + "sort-destructure-keys/sort-destructure-keys": [ + 1, + { "caseSensitive": false } + ], + "sort-keys": [ + "warn", + "asc", { + "caseSensitive": true, + "natural": true + } + ], + "typescript-sort-keys/interface": 1, + "typescript-sort-keys/string-enum": 1, + + "camelcase": "off", + "import/no-unresolved": "off", + "import/prefer-default-export": "off", + "indent": "off", + "no-unused-vars": "off", + "react/jsx-one-expression-per-line": "off", + "semi": "off" + } +} diff --git a/package.json b/package.json index f1c2b61c..3dc0a6bc 100644 --- a/package.json +++ b/package.json @@ -8,7 +8,8 @@ "build": "GENERATE_SOURCEMAP=false react-scripts build && gzipper --verbose ./build", "test": "react-scripts test --passWithNoTests --watchAll=false", "eject": "react-scripts eject", - "generate": "graphql-codegen --config codegen.yml" + "generate": "graphql-codegen --config codegen.yml", + "lint": "eslint 'src/**/*.{ts,tsx}'" }, "dependencies": { "@apollo/react-common": "^3.1.4", @@ -38,13 +39,19 @@ "@types/react": "^16.9.0", "@types/react-dom": "^16.9.0", "commitizen": "^4.1.2", + "eslint": "6.8.0", + "eslint-config-airbnb": "18.1.0", + "eslint-plugin-import": "^2.20.1", + "eslint-plugin-jsx-a11y": "^6.2.3", + "eslint-plugin-postro4no": "^0.0.7", + "eslint-plugin-react": "^7.19.0", + "eslint-plugin-react-hooks": "2.5.0", + "eslint-plugin-sort-destructure-keys": "^1.3.4", + "eslint-plugin-typescript-sort-keys": "^1.2.0", "gzipper": "^3.7.0", "husky": "^4.2.5", "typescript": "^3.9.3" }, - "eslintConfig": { - "extends": "react-app" - }, "config": { "commitizen": { "path": "./node_modules/cz-conventional-changelog" From 71886fc3338ec796d24ceb1891c6a9cc55dddf8a Mon Sep 17 00:00:00 2001 From: "mirlan.maksitaliev" Date: Tue, 2 Jun 2020 16:33:55 +0600 Subject: [PATCH 5/7] chore: lint staged configuration --- package.json | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/package.json b/package.json index 3dc0a6bc..e6968d2e 100644 --- a/package.json +++ b/package.json @@ -6,7 +6,8 @@ "prestart ": "yarn generate", "start": "react-scripts start", "build": "GENERATE_SOURCEMAP=false react-scripts build && gzipper --verbose ./build", - "test": "react-scripts test --passWithNoTests --watchAll=false", + "test": "react-scripts test --testMatch '**/__tests__/*' --passWithNoTests --watchAll=false", + "test:watch": "react-scripts test --testMatch '**/__tests__/*'", "eject": "react-scripts eject", "generate": "graphql-codegen --config codegen.yml", "lint": "eslint 'src/**/*.{ts,tsx}'" @@ -50,6 +51,7 @@ "eslint-plugin-typescript-sort-keys": "^1.2.0", "gzipper": "^3.7.0", "husky": "^4.2.5", + "lint-staged": "^10.2.7", "typescript": "^3.9.3" }, "config": { @@ -59,10 +61,16 @@ }, "husky": { "hooks": { - "pre-commit": "npm test", + "pre-commit": "lint-staged", "commit-msg": "commitlint -E HUSKY_GIT_PARAMS" } }, + "lint-staged": { + "src/**/*.{ts,tsx}": [ + "npm run lint -- --max-warnings 0", + "npm run test -- --findRelatedTests" + ] + }, "browserslist": { "production": [ ">0.2%", From 446c41a37a5f9446a19d7e0832d59662283163ac Mon Sep 17 00:00:00 2001 From: "mirlan.maksitaliev" Date: Tue, 2 Jun 2020 19:55:14 +0600 Subject: [PATCH 6/7] refactor: removed callApi --- src/helpers/callApi/checkStatus.tsx | 7 ---- src/helpers/callApi/clearUserAuthInfo.tsx | 4 --- src/helpers/callApi/getRequestConfig.tsx | 35 ------------------- src/helpers/callApi/getResponseData.tsx | 3 -- src/helpers/callApi/index.tsx | 42 ----------------------- src/helpers/callApi/loadIdToken.tsx | 1 - src/helpers/callApi/parseJSON.tsx | 1 - src/helpers/callApi/removeCookie.tsx | 11 ------ src/helpers/callApi/types.tsx | 14 -------- src/helpers/index.tsx | 2 +- 10 files changed, 1 insertion(+), 119 deletions(-) delete mode 100644 src/helpers/callApi/checkStatus.tsx delete mode 100644 src/helpers/callApi/clearUserAuthInfo.tsx delete mode 100644 src/helpers/callApi/getRequestConfig.tsx delete mode 100644 src/helpers/callApi/getResponseData.tsx delete mode 100644 src/helpers/callApi/index.tsx delete mode 100644 src/helpers/callApi/loadIdToken.tsx delete mode 100644 src/helpers/callApi/parseJSON.tsx delete mode 100644 src/helpers/callApi/removeCookie.tsx delete mode 100644 src/helpers/callApi/types.tsx diff --git a/src/helpers/callApi/checkStatus.tsx b/src/helpers/callApi/checkStatus.tsx deleted file mode 100644 index 4b3b206a..00000000 --- a/src/helpers/callApi/checkStatus.tsx +++ /dev/null @@ -1,7 +0,0 @@ -export const checkStatus = (response: Response) => { - if (!response.ok) { - return Promise.reject(new Error(response.statusText)) - } - - return Promise.resolve(response) -} diff --git a/src/helpers/callApi/clearUserAuthInfo.tsx b/src/helpers/callApi/clearUserAuthInfo.tsx deleted file mode 100644 index c48925f4..00000000 --- a/src/helpers/callApi/clearUserAuthInfo.tsx +++ /dev/null @@ -1,4 +0,0 @@ -export const clearUserAuthInfo = () => { - localStorage.removeItem('id_token') - localStorage.removeItem('AuthUser') -} diff --git a/src/helpers/callApi/getRequestConfig.tsx b/src/helpers/callApi/getRequestConfig.tsx deleted file mode 100644 index 8bbf113e..00000000 --- a/src/helpers/callApi/getRequestConfig.tsx +++ /dev/null @@ -1,35 +0,0 @@ -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 -} diff --git a/src/helpers/callApi/getResponseData.tsx b/src/helpers/callApi/getResponseData.tsx deleted file mode 100644 index 149ace17..00000000 --- a/src/helpers/callApi/getResponseData.tsx +++ /dev/null @@ -1,3 +0,0 @@ -export const getResponseData = (proc: string) => (response: any) => ( - response?.data?.[0]?.[proc] -) diff --git a/src/helpers/callApi/index.tsx b/src/helpers/callApi/index.tsx deleted file mode 100644 index 00a38273..00000000 --- a/src/helpers/callApi/index.tsx +++ /dev/null @@ -1,42 +0,0 @@ -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' diff --git a/src/helpers/callApi/loadIdToken.tsx b/src/helpers/callApi/loadIdToken.tsx deleted file mode 100644 index c1f70feb..00000000 --- a/src/helpers/callApi/loadIdToken.tsx +++ /dev/null @@ -1 +0,0 @@ -export const loadIdToken = () => localStorage.getItem('id_token') diff --git a/src/helpers/callApi/parseJSON.tsx b/src/helpers/callApi/parseJSON.tsx deleted file mode 100644 index 12709130..00000000 --- a/src/helpers/callApi/parseJSON.tsx +++ /dev/null @@ -1 +0,0 @@ -export const parseJSON = (response: Response) => response.json() diff --git a/src/helpers/callApi/removeCookie.tsx b/src/helpers/callApi/removeCookie.tsx deleted file mode 100644 index 4118169c..00000000 --- a/src/helpers/callApi/removeCookie.tsx +++ /dev/null @@ -1,11 +0,0 @@ -export const removeCookie = ( - name: string, - domain: string = '.instatscout.com', -) => { - document.cookie = ` - ${name}=; - expires='Thu, 01 Jan 1970 00:00:00 UTC'; - path=/; - domain=${domain} - ` -} diff --git a/src/helpers/callApi/types.tsx b/src/helpers/callApi/types.tsx deleted file mode 100644 index cb2b105a..00000000 --- a/src/helpers/callApi/types.tsx +++ /dev/null @@ -1,14 +0,0 @@ -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, -} diff --git a/src/helpers/index.tsx b/src/helpers/index.tsx index 8842e651..336ce12b 100644 --- a/src/helpers/index.tsx +++ b/src/helpers/index.tsx @@ -1 +1 @@ -export * from './callApi' +export {} From af057e0c4c4917a597800c43d6e95ab1d2bdd2ea Mon Sep 17 00:00:00 2001 From: "mirlan.maksitaliev" Date: Tue, 2 Jun 2020 19:58:41 +0600 Subject: [PATCH 7/7] refactor: renamed operation to queries --- src/api/operations/User/{operation.graphql => queries.graphql} | 0 src/api/operations/User/{operation.tsx => queries.tsx} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename src/api/operations/User/{operation.graphql => queries.graphql} (100%) rename src/api/operations/User/{operation.tsx => queries.tsx} (100%) diff --git a/src/api/operations/User/operation.graphql b/src/api/operations/User/queries.graphql similarity index 100% rename from src/api/operations/User/operation.graphql rename to src/api/operations/User/queries.graphql diff --git a/src/api/operations/User/operation.tsx b/src/api/operations/User/queries.tsx similarity index 100% rename from src/api/operations/User/operation.tsx rename to src/api/operations/User/queries.tsx