Merge pull request #4 from instat/ott-67-project-configuration
Ott 67 project configurationkeep-around/af30b88d367751c9e05a735e4a0467a96238ef47
commit
64e0609013
@ -1,4 +0,0 @@ |
||||
src/types/* |
||||
src/api/operations/**/*.tsx |
||||
src/react-app-env.d.ts |
||||
src/serviceWorker.ts |
||||
@ -0,0 +1,47 @@ |
||||
<link |
||||
href='https://fonts.googleapis.com/css?family=PT+Sans:400,700&amp;subset=cyrillic,latin' |
||||
rel='stylesheet' type='text/css'/> |
||||
<style> |
||||
*, *:before, *:after { |
||||
box-sizing: border-box; |
||||
} |
||||
|
||||
body { |
||||
min-height: 100vh; |
||||
margin: 20; |
||||
padding: 20; |
||||
font-family: Montserrat, Tahoma, sans-serif; |
||||
font-size: 12px; |
||||
line-height: 12px; |
||||
color: #000; |
||||
} |
||||
|
||||
h1, h2, h3, h4, h5, h6, p, ul, li { |
||||
margin: 0; |
||||
padding: 0; |
||||
font-size: 12px; |
||||
line-height: 12px; |
||||
} |
||||
|
||||
ul, li { |
||||
list-style: none; |
||||
} |
||||
|
||||
a { |
||||
text-decoration: none; |
||||
color: #000; |
||||
} |
||||
|
||||
fieldset { |
||||
margin: 0; |
||||
min-width: 0; |
||||
padding: 0; |
||||
border: 0; |
||||
} |
||||
|
||||
button, input, select, textarea { |
||||
font-family: inherit; |
||||
font-size: inherit; |
||||
line-height: inherit; |
||||
} |
||||
</style> |
||||
@ -0,0 +1,7 @@ |
||||
import { addDecorator } from '@storybook/react' |
||||
import { withThemesProvider } from 'storybook-addon-styled-component-theme' |
||||
|
||||
import { lightTheme, darkTheme } from '../src/features/Theme/config' |
||||
|
||||
const themes = [darkTheme, lightTheme] |
||||
addDecorator(withThemesProvider(themes)) |
||||
@ -1,22 +0,0 @@ |
||||
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 |
||||
@ -1,8 +0,0 @@ |
||||
query User { |
||||
__schema { |
||||
types { |
||||
name |
||||
kind |
||||
} |
||||
} |
||||
} |
||||
@ -1,3 +0,0 @@ |
||||
// autgenerated using https://graphql-code-generator.com/
|
||||
|
||||
export {} |
||||
@ -0,0 +1,7 @@ |
||||
export const AUTH_KEYS = { |
||||
authUser: 'AuthUser', |
||||
backLocation: 'backLocation', |
||||
cookieToken: 'token', |
||||
headerToken: 'x-auth-token', |
||||
idToken: 'id_token', |
||||
} |
||||
@ -1 +1,3 @@ |
||||
export * from './routes' |
||||
export * from './pages' |
||||
export * from './authKeys' |
||||
|
||||
@ -0,0 +1,3 @@ |
||||
export const PAGES = { |
||||
login: '/login', |
||||
} |
||||
@ -1,2 +1,2 @@ |
||||
export const API_ROOT = '' |
||||
export const GRAPHQL = `${API_ROOT}/graphql` |
||||
export const DATA_URL = `${API_ROOT}/data` |
||||
|
||||
@ -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,6 @@ |
||||
import { AUTH_KEYS } from 'config' |
||||
|
||||
export const clearUserAuthInfo = () => { |
||||
localStorage.removeItem(AUTH_KEYS.idToken) |
||||
localStorage.removeItem(AUTH_KEYS.authUser) |
||||
} |
||||
@ -0,0 +1,29 @@ |
||||
import { AUTH_KEYS } from 'config' |
||||
|
||||
import { loadIdToken } from './loadIdToken' |
||||
import { TRequestConfig } from './types' |
||||
|
||||
export const getRequestConfig = ( |
||||
config: TRequestConfig, |
||||
signal?: AbortSignal, |
||||
) => { |
||||
const requestConfig = { |
||||
...config, |
||||
headers: config.headers || new Headers(), |
||||
method: config.method || 'POST', |
||||
signal, |
||||
} |
||||
|
||||
if (config.body) { |
||||
requestConfig.body = JSON.stringify(config.body) |
||||
requestConfig.headers.set('Content-Type', 'application/json') |
||||
} |
||||
|
||||
const token = loadIdToken() |
||||
|
||||
if (token) { |
||||
requestConfig.headers.set(AUTH_KEYS.headerToken, token) |
||||
} |
||||
|
||||
return requestConfig |
||||
} |
||||
@ -0,0 +1,3 @@ |
||||
export const getResponseData = (proc: string) => (response: any) => ( |
||||
response?.data?.[0]?.[proc] |
||||
) |
||||
@ -0,0 +1,36 @@ |
||||
import { TCallApi } from './types' |
||||
import { parseJSON } from './parseJSON' |
||||
import { checkStatus } from './checkStatus' |
||||
import { getRequestConfig } from './getRequestConfig' |
||||
import { logoutIfUnauthorized } from './logoutIfUnauthorized' |
||||
|
||||
export const callApiBase = ({ |
||||
abortSignal, |
||||
config, |
||||
url, |
||||
}: TCallApi) => { |
||||
const requestConfig = getRequestConfig(config, abortSignal) |
||||
|
||||
// eslint-disable-next-line no-console
|
||||
console.log( |
||||
'%c callApi from module - config ', |
||||
'color: white; background-color: #95B46A', |
||||
config, |
||||
) |
||||
|
||||
return fetch(url, requestConfig) |
||||
} |
||||
|
||||
export const callApi = ({ |
||||
abortSignal, |
||||
config, |
||||
url, |
||||
}: TCallApi) => ( |
||||
callApiBase({ |
||||
abortSignal, |
||||
config, |
||||
url, |
||||
}).then(checkStatus) |
||||
.then(parseJSON) |
||||
.catch(logoutIfUnauthorized) |
||||
) |
||||
@ -0,0 +1 @@ |
||||
export const loadIdToken = () => localStorage.getItem('id_token') |
||||
@ -0,0 +1,22 @@ |
||||
import { AUTH_KEYS, PAGES } from 'config' |
||||
|
||||
import { removeCookie } from './removeCookie' |
||||
import { clearUserAuthInfo } from './clearUserAuthInfo' |
||||
|
||||
export const logoutIfUnauthorized = (error: Error) => { |
||||
if (error.message === 'Unauthorized') { |
||||
clearUserAuthInfo() |
||||
removeCookie(AUTH_KEYS.cookieToken) |
||||
|
||||
const { pathname } = window.location |
||||
|
||||
if (pathname !== PAGES.login) { |
||||
localStorage.setItem(AUTH_KEYS.backLocation, pathname) |
||||
window.location.pathname = PAGES.login |
||||
} |
||||
} |
||||
|
||||
// eslint-disable-next-line no-console
|
||||
console.error(error) |
||||
return Promise.reject(error) |
||||
} |
||||
@ -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,11 @@ |
||||
export type TRequestConfig = { |
||||
body?: any, |
||||
headers?: Headers, |
||||
method?: string, |
||||
} |
||||
|
||||
export type TCallApi = { |
||||
abortSignal?: AbortSignal, |
||||
config: TRequestConfig, |
||||
url: string, |
||||
} |
||||
@ -1 +1 @@ |
||||
export {} |
||||
export * from './callApi' |
||||
|
||||
@ -1 +1,2 @@ |
||||
// eslint-disable-next-line spaced-comment
|
||||
/// <reference types="react-scripts" />
|
||||
|
||||
@ -1,3 +0,0 @@ |
||||
// autgenerated using https://graphql-code-generator.com/
|
||||
|
||||
export {} |
||||
Loading…
Reference in new issue