This commit is contained in:
Vut Pov 2020-07-15 10:40:48 +07:00
parent 5034f5924b
commit 47f80670cf
24 changed files with 306 additions and 10970 deletions

8
.idea/.gitignore vendored Normal file
View File

@ -0,0 +1,8 @@
# Default ignored files
/shelf/
/workspace.xml
# Datasource local storage ignored files
/dataSources/
/dataSources.local.xml
# Editor-based HTTP Client requests
/httpRequests/

View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$" />
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
</component>
</module>

View File

@ -0,0 +1,40 @@
<component name="ProjectCodeStyleConfiguration">
<code_scheme name="Project" version="173">
<JSCodeStyleSettings version="0">
<option name="USE_SEMICOLON_AFTER_STATEMENT" value="false" />
<option name="FORCE_SEMICOLON_STYLE" value="true" />
<option name="SPACE_BEFORE_FUNCTION_LEFT_PARENTH" value="false" />
<option name="USE_DOUBLE_QUOTES" value="false" />
<option name="FORCE_QUOTE_STYlE" value="true" />
<option name="ENFORCE_TRAILING_COMMA" value="Remove" />
<option name="SPACES_WITHIN_OBJECT_LITERAL_BRACES" value="true" />
<option name="SPACES_WITHIN_IMPORTS" value="true" />
</JSCodeStyleSettings>
<TypeScriptCodeStyleSettings version="0">
<option name="USE_SEMICOLON_AFTER_STATEMENT" value="false" />
<option name="FORCE_SEMICOLON_STYLE" value="true" />
<option name="SPACE_BEFORE_FUNCTION_LEFT_PARENTH" value="false" />
<option name="USE_DOUBLE_QUOTES" value="false" />
<option name="FORCE_QUOTE_STYlE" value="true" />
<option name="ENFORCE_TRAILING_COMMA" value="Remove" />
<option name="SPACES_WITHIN_OBJECT_LITERAL_BRACES" value="true" />
<option name="SPACES_WITHIN_IMPORTS" value="true" />
</TypeScriptCodeStyleSettings>
<codeStyleSettings language="JavaScript">
<option name="SOFT_MARGINS" value="80" />
<indentOptions>
<option name="INDENT_SIZE" value="2" />
<option name="CONTINUATION_INDENT_SIZE" value="2" />
<option name="TAB_SIZE" value="2" />
</indentOptions>
</codeStyleSettings>
<codeStyleSettings language="TypeScript">
<option name="SOFT_MARGINS" value="80" />
<indentOptions>
<option name="INDENT_SIZE" value="2" />
<option name="CONTINUATION_INDENT_SIZE" value="2" />
<option name="TAB_SIZE" value="2" />
</indentOptions>
</codeStyleSettings>
</code_scheme>
</component>

View File

@ -0,0 +1,5 @@
<component name="ProjectCodeStyleConfiguration">
<state>
<option name="USE_PER_PROJECT_SETTINGS" value="true" />
</state>
</component>

View File

@ -0,0 +1,6 @@
<component name="InspectionProjectProfileManager">
<profile version="1.0">
<option name="myName" value="Project Default" />
<inspection_tool class="Eslint" enabled="true" level="WARNING" enabled_by_default="true" />
</profile>
</component>

9
.idea/misc.xml Normal file
View File

@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="JavaScriptSettings">
<option name="languageLevel" value="JSX" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_1_8" project-jdk-name="1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>

8
.idea/modules.xml Normal file
View File

@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/.idea/auth-context-provider.iml" filepath="$PROJECT_DIR$/.idea/auth-context-provider.iml" />
</modules>
</component>
</project>

6
.idea/vcs.xml Normal file
View File

@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="" vcs="Git" />
</component>
</project>

View File

@ -4,27 +4,6 @@
[![NPM](https://img.shields.io/npm/v/auth-context-provider.svg)](https://www.npmjs.com/package/auth-context-provider) [![JavaScript Style Guide](https://img.shields.io/badge/code_style-standard-brightgreen.svg)](https://standardjs.com)
## Install
```bash
npm install --save auth-context-provider
```
## Usage
```tsx
import React, { Component } from 'react'
import MyComponent from 'auth-context-provider'
import 'auth-context-provider/dist/index.css'
class Example extends Component {
render() {
return <MyComponent />
}
}
```
## License
MIT © [vuthpov](https://github.com/vuthpov)

View File

@ -17,8 +17,8 @@
"@types/node": "link:../node_modules/@types/node",
"@types/react": "link:../node_modules/@types/react",
"@types/react-dom": "link:../node_modules/@types/react-dom",
"react": "link:../node_modules/react",
"react-dom": "link:../node_modules/react-dom",
"react": "^16.0.0",
"react-dom": "^16.0.0",
"react-scripts": "link:../node_modules/react-scripts",
"typescript": "link:../node_modules/typescript",
"auth-context-provider": "link:.."

View File

@ -1,9 +0,0 @@
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'
it('renders without crashing', () => {
const div = document.createElement('div')
ReactDOM.render(<App />, div)
ReactDOM.unmountComponentAtNode(div)
})

View File

@ -1,10 +1,18 @@
import React from 'react'
import { ExampleComponent } from 'auth-context-provider'
import 'auth-context-provider/dist/index.css'
import AuthContextProvider from 'auth-context-provider'
import FormLogin from './FormLogin'
const App = () => {
return <ExampleComponent text="Create React Library Example 😄" />
return <FormLogin/>
}
export default App
const authUrl = 'http://localhost:8080/authenticate'
const EnhancedApp = () => {
return <AuthContextProvider authUrl={authUrl}>
<App />
</AuthContextProvider>
}
export default EnhancedApp

50
example/src/FormLogin.tsx Normal file
View File

@ -0,0 +1,50 @@
import React, { useState, useCallback } from 'react'
import { useAuthContext } from 'auth-context-provider'
const FormLogin = () => {
const [username, setUsername] = useState('')
const [password, setPassword] = useState('')
const auth: any = useAuthContext()
const onSubmit = useCallback(
(authenticationInfo) => {
auth.login(authenticationInfo)
},
[auth]
)
return (
<>
<form
onSubmit={(e) => {
e.preventDefault()
onSubmit({ username, password })
}}
>
<label htmlFor='username'>Username</label>
<input
type='text'
id='username'
value={username}
onChange={(e) => {
setUsername(e.target.value)
}}
/>
<label htmlFor='password'>Password</label>
<input
type='password'
id='password'
value={password}
onChange={(e) => {
setPassword(e.target.value)
}}
/>
<button type={'submit'}>Submit</button>
</form>
<br/>
</>
)
}
export default FormLogin

View File

@ -1,14 +0,0 @@
body {
margin: 0;
padding: 0;
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
sans-serif;
-webkit-font-smoothing: antialiased;
-moz-osx-font-smoothing: grayscale;
}
code {
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
monospace;
}

View File

@ -1,5 +1,3 @@
import './index.css'
import React from 'react'
import ReactDOM from 'react-dom'
import App from './App'

View File

@ -1,5 +0,0 @@
// jest-dom adds custom jest matchers for asserting on DOM nodes.
// allows you to do things like:
// expect(element).toHaveTextContent(/react/i)
// learn more: https://github.com/testing-library/jest-dom
import '@testing-library/jest-dom/extend-expect';

View File

@ -2,7 +2,10 @@
"compilerOptions": {
"outDir": "dist",
"module": "esnext",
"lib": ["dom", "esnext"],
"lib": [
"dom",
"esnext"
],
"moduleResolution": "node",
"jsx": "react",
"sourceMap": true,
@ -15,8 +18,21 @@
"suppressImplicitAnyIndexErrors": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"allowSyntheticDefaultImports": true
"allowSyntheticDefaultImports": true,
"target": "es5",
"allowJs": true,
"skipLibCheck": true,
"strict": true,
"forceConsistentCasingInFileNames": true,
"resolveJsonModule": true,
"isolatedModules": true,
"noEmit": true
},
"include": ["src"],
"exclude": ["node_modules", "build"]
"include": [
"src"
],
"exclude": [
"node_modules",
"build"
]
}

File diff suppressed because it is too large Load Diff

View File

@ -1,6 +1,6 @@
{
"name": "auth-context-provider",
"version": "1.0.0",
"version": "1.0.1",
"description": "Made with create-react-library",
"author": "vuthpov",
"license": "MIT",
@ -24,7 +24,11 @@
"deploy": "gh-pages -d example/build"
},
"peerDependencies": {
"react": "^16.0.0"
"react": "^16.0.0",
"react-dom": "^16.0.0"
},
"dependencies": {
"base-context-provider": "https://git.cubetiqs.com/vutpov/base-context-provider.git"
},
"devDependencies": {
"@testing-library/jest-dom": "^4.2.4",
@ -36,7 +40,6 @@
"@types/react-dom": "^16.9.7",
"@typescript-eslint/eslint-plugin": "^2.26.0",
"@typescript-eslint/parser": "^2.26.0",
"microbundle-crl": "^0.13.10",
"babel-eslint": "^10.0.3",
"cross-env": "^7.0.2",
"eslint": "^6.8.0",
@ -50,10 +53,9 @@
"eslint-plugin-react": "^7.17.0",
"eslint-plugin-standard": "^4.0.1",
"gh-pages": "^2.2.0",
"microbundle-crl": "^0.13.10",
"npm-run-all": "^4.1.5",
"prettier": "^2.0.4",
"react": "^16.13.1",
"react-dom": "^16.13.1",
"react-scripts": "^3.4.1",
"typescript": "^3.7.5"
},

View File

@ -1,7 +0,0 @@
import { ExampleComponent } from '.'
describe('ExampleComponent', () => {
it('is truthy', () => {
expect(ExampleComponent).toBeTruthy()
})
})

View File

@ -1,10 +1,101 @@
import * as React from 'react'
import styles from './styles.module.css'
import React from 'react'
import BaseContextProvider, {
baseUseContext,
baseWithContext
} from 'base-context-provider'
interface Props {
text: string
export interface AuthContextProps {
authUrl: string
storageType?: 'localStorage' | 'cookies'
}
export const ExampleComponent = ({ text }: Props) => {
return <div className={styles.test}>Example Component: {text}</div>
export interface AuthState<T> {
isLogin: boolean
isFetchingUser: boolean
user: T
}
const Context = React.createContext({})
class AuthContextProvider extends BaseContextProvider<
AuthContextProps,
AuthState<any>
> {
state = {
isLogin: false,
isFetchingUser: false,
user: {
username: ''
}
}
getContext() {
return Context
}
fetchUser: (password: string) => void = async (password) => {
this.setState({
isFetchingUser: true
})
const { authUrl } = this.props
const { username } = this.state.user
const response = await fetch(authUrl, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify({ username, password })
})
const data = await response.json()
localStorage.setItem('token', data.token)
}
login = ({ username, password }: { username: string; password: string }) => {
this.setState(
(oldState) => {
return {
...oldState,
user: {
...oldState.user,
username
}
}
},
() => {
this.fetchUser(password)
}
)
}
logout = async () => {
this.setState({
isLogin: false,
user: {
username: ''
}
})
return Promise.resolve(true)
}
getContextReturnValue() {
return {
...this.state,
login: this.login,
logout: this.logout
}
}
}
// @ts-ignore
AuthContextProvider.defaultProps = {
storageType: 'localStorage'
}
export const useAuthContext = baseUseContext(Context)
export const withAuthContext = baseWithContext(Context, 'auth')
export default AuthContextProvider

View File

@ -1,9 +0,0 @@
/* add css module styles here (optional) */
.test {
margin: 2em;
padding: 0.5em;
border: 2px solid #000;
font-size: 2em;
text-align: center;
}

17
src/typings.d.ts vendored
View File

@ -1,17 +0,0 @@
/**
* Default CSS definition for typescript,
* will be overridden with file-specific definitions by rollup
*/
declare module '*.css' {
const content: { [className: string]: string };
export default content;
}
interface SvgrComponent extends React.StatelessComponent<React.SVGAttributes<SVGElement>> {}
declare module '*.svg' {
const svgUrl: string;
const svgComponent: SvgrComponent;
export default svgUrl;
export { svgComponent as ReactComponent }
}

View File

@ -2641,6 +2641,10 @@ balanced-match@^1.0.0:
resolved "https://npm-registry.cubetiqs.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
integrity sha1-ibTRmasr7kneFk6gK4nORi1xt2c=
"base-context-provider@https://git.cubetiqs.com/vutpov/base-context-provider.git":
version "1.0.2"
resolved "https://git.cubetiqs.com/vutpov/base-context-provider.git#a3db95395e9996146f6623469ac52b675a213ce7"
base64-js@^1.0.2:
version "1.3.1"
resolved "https://npm-registry.cubetiqs.com/base64-js/-/base64-js-1.3.1.tgz#58ece8cb75dd07e71ed08c736abc5fac4dbf8df1"
@ -7198,7 +7202,7 @@ loglevel@^1.6.6:
resolved "https://npm-registry.cubetiqs.com/loglevel/-/loglevel-1.6.8.tgz#8a25fb75d092230ecd4457270d80b54e28011171"
integrity sha512-bsU7+gc9AJ2SqpzxwU3+1fedl8zAntbtC5XYlt3s2j1hJcn2PsXSmgN8TaLG/J1/2mod4+cE/3vNL70/c1RNCA==
loose-envify@^1.0.0, loose-envify@^1.1.0, loose-envify@^1.4.0:
loose-envify@^1.0.0, loose-envify@^1.4.0:
version "1.4.0"
resolved "https://npm-registry.cubetiqs.com/loose-envify/-/loose-envify-1.4.0.tgz#71ee51fa7be4caec1a63839f7e682d8132d30caf"
integrity sha512-lyuxPGr/Wfhrlem2CL/UcnUc1zcqKAImBDzukY7Y5F/yQiNdko6+fRLevlw1HgMySw7f611UIY408EtxRSoK3Q==
@ -9263,7 +9267,7 @@ prompts@^2.0.1:
kleur "^3.0.3"
sisteransi "^1.0.4"
prop-types@^15.6.2, prop-types@^15.7.2:
prop-types@^15.7.2:
version "15.7.2"
resolved "https://npm-registry.cubetiqs.com/prop-types/-/prop-types-15.7.2.tgz#52c41e75b8c87e72b9d9360e0206b99dcbffa6c5"
integrity sha512-8QQikdH7//R2vurIJSutZ1smHYTcLpRWEOlHnzcWHmBYrOGUysKwSsrC89BCiFj3CbrfJ/nXFdJepOVrY1GCHQ==
@ -9459,16 +9463,6 @@ react-dev-utils@^10.2.1:
strip-ansi "6.0.0"
text-table "0.2.0"
react-dom@^16.13.1:
version "16.13.1"
resolved "https://npm-registry.cubetiqs.com/react-dom/-/react-dom-16.13.1.tgz#c1bd37331a0486c078ee54c4740720993b2e0e7f"
integrity sha512-81PIMmVLnCNLO/fFOQxdQkvEq/+Hfpv24XNJfpyZhTRfO0QcmQIF/PgCa1zCOj2w1hrn12MFLyaJ/G0+Mxtfag==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"
prop-types "^15.6.2"
scheduler "^0.19.1"
react-error-overlay@^6.0.7:
version "6.0.7"
resolved "https://npm-registry.cubetiqs.com/react-error-overlay/-/react-error-overlay-6.0.7.tgz#1dcfb459ab671d53f660a991513cb2f0a0553108"
@ -9539,15 +9533,6 @@ react-scripts@^3.4.1:
optionalDependencies:
fsevents "2.1.2"
react@^16.13.1:
version "16.13.1"
resolved "https://npm-registry.cubetiqs.com/react/-/react-16.13.1.tgz#2e818822f1a9743122c063d6410d85c1e3afe48e"
integrity sha512-YMZQQq32xHLX0bz5Mnibv1/LHb3Sqzngu7xstSM+vrkE5Kzr9xE0yMByK5kMoTK30YVJE61WfbxIFFvfeDKT1w==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"
prop-types "^15.6.2"
read-pkg-up@^2.0.0:
version "2.0.0"
resolved "https://npm-registry.cubetiqs.com/read-pkg-up/-/read-pkg-up-2.0.0.tgz#6b72a8048984e0c41e79510fd5e9fa99b3b549be"
@ -10151,14 +10136,6 @@ saxes@^3.1.9:
dependencies:
xmlchars "^2.1.1"
scheduler@^0.19.1:
version "0.19.1"
resolved "https://npm-registry.cubetiqs.com/scheduler/-/scheduler-0.19.1.tgz#4f3e2ed2c1a7d65681f4c854fa8c5a1ccb40f196"
integrity sha512-n/zwRWRYSUj0/3g/otKDRPMh6qv2SYMWNq85IEa8iZyAv8od9zDYpGSnpBEjNgcMNq6Scbu5KfIPxNF72R/2EA==
dependencies:
loose-envify "^1.1.0"
object-assign "^4.1.1"
schema-utils@^1.0.0:
version "1.0.0"
resolved "https://npm-registry.cubetiqs.com/schema-utils/-/schema-utils-1.0.0.tgz#0b79a93204d7b600d4b2850d1f66c2a34951c770"