Skip to content

Commit

Permalink
Merge pull request #136 from swiftyapp/otp-fix
Browse files Browse the repository at this point in the history
Update hook dependencies to make OTP refresh
  • Loading branch information
alchaplinsky authored Jan 29, 2023
2 parents dc0cc77 + 4a8647e commit e73fc4e
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 12 deletions.
3 changes: 2 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,12 @@
"ecmaVersion": 2018,
"sourceType": "module"
},
"plugins": ["react", "prettier", "jest"],
"plugins": ["react", "prettier", "jest", "react-hooks"],
"rules": {
"prettier/prettier": ["error", { "singlQuote": true, "parser": "flow" }],
"react/jsx-uses-react": 1,
"react/jsx-uses-vars": 1,
"react-hooks/exhaustive-deps": "warn",
"no-console": "warn"
}
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@
"eslint-plugin-jsx-a11y": "^6.7.1",
"eslint-plugin-prettier": "^4.2.1",
"eslint-plugin-react": "^7.32.2",
"eslint-plugin-react-hooks": "^4.6.0",
"file-loader": "^6.2.0",
"jest": "^29.4.1",
"prettier": "^2.8.3",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import React, { useState, useEffect } from 'react'
import React, { useState, useEffect, useCallback } from 'react'
import Copy from 'copy.svg'
import { copy } from 'services/copy'

Expand All @@ -10,24 +10,22 @@ export default ({ name, entry }) => {
const [code, setCode] = useState('')

useEffect(() => {
setOTPData()
const interval = setInterval(() => {
setOTPData(entry.otp)
const interval = setTimeout(() => {
if (time > 0) {
setTime(time - 1)
} else {
setOTPData()
setOTPData(entry.otp)
}
}, 1000)
return () => clearInterval(interval)
}, [])
return () => clearTimeout(interval)
}, [entry.otp, setOTPData, time])

const setOTPData = () => {
const otp = window.GeneratorAPI.generateOTP(
window.CryptorAPI.decrypt(entry.otp)
)
const setOTPData = useCallback(code => {
const otp = window.GeneratorAPI.generateOTP(window.CryptorAPI.decrypt(code))
setTime(otp.time)
setCode(otp.code)
}
}, [])

const formattedValue = () => `${code.substr(0, 3)} ${code.substr(3)}`

Expand Down
5 changes: 5 additions & 0 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3952,6 +3952,11 @@ eslint-plugin-prettier@^4.2.1:
dependencies:
prettier-linter-helpers "^1.0.0"

eslint-plugin-react-hooks@^4.6.0:
version "4.6.0"
resolved "https://registry.yarnpkg.com/eslint-plugin-react-hooks/-/eslint-plugin-react-hooks-4.6.0.tgz#4c3e697ad95b77e93f8646aaa1630c1ba607edd3"
integrity sha512-oFc7Itz9Qxh2x4gNHStv3BqJq54ExXmfC+a1NjAta66IAN87Wu0R/QArgIS9qKzX3dXKPI9H5crl9QchNMY9+g==

eslint-plugin-react@^7.32.2:
version "7.32.2"
resolved "https://registry.yarnpkg.com/eslint-plugin-react/-/eslint-plugin-react-7.32.2.tgz#e71f21c7c265ebce01bcbc9d0955170c55571f10"
Expand Down

0 comments on commit e73fc4e

Please sign in to comment.