Skip to content

Commit

Permalink
#496 One time links should be generated by our url shortener (#615)
Browse files Browse the repository at this point in the history
* #496 One time links should be generated by our url shortener

* For payment we can simply use dapp.gooddollar.org/?paymentCode=
For receive it should go to the .../Send?code=

* double encodeURI

* fix double condition

* editing tests

* editing tests

* fix in PR comments
1 EncodeURI procedure transfer
2 Change default value

* fix by comments
  • Loading branch information
StanislavShevchenko authored and sirpy committed Oct 2, 2019
1 parent d198552 commit 3f2e517
Show file tree
Hide file tree
Showing 6 changed files with 79 additions and 11 deletions.
3 changes: 3 additions & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -40,3 +40,6 @@ REACT_APP_ROLLBAR_API_KEY=

# Should or should not throw errors when saving profile fails
REACT_APP_THROW_SAVE_PROFILE_ERRORS=true

REACT_APP_RECEIVE_URL=
REACT_APP_SEND_URL=
2 changes: 2 additions & 0 deletions .env.test
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,5 @@ REACT_APP_AMPLITUDE_API_KEY=
REACT_APP_ROLLBAR_API_KEY=
REACT_APP_THROW_SAVE_PROFILE_ERRORS=true
REACT_APP_ADMIN_MNEMONIC=drip industry pizza deny pistol stem must device citizen crowd offer now
REACT_APP_RECEIVE_URL=http://localhost:3000/AppNavigation/Dashboard/Send
REACT_APP_SEND_URL=http://localhost:3000/AppNavigation/Dashboard/Home
20 changes: 20 additions & 0 deletions src/components/dashboard/Dashboard.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import userStorage from '../../lib/gundb/UserStorage'
import { FAQ, PrivacyArticle, PrivacyPolicy, Support, TermsOfUse } from '../webView/webViewInstances'
import { withStyles } from '../../lib/styles'
import Mnemonics from '../signin/Mnemonics'
import { extractQueryParams, readCode } from '../../lib/share'

// import goodWallet from '../../lib/wallet/GoodWallet'
import { deleteAccountDialog } from '../sidemenu/SideMenuPanel'
Expand All @@ -50,6 +51,7 @@ import SendConfirmation from './SendConfirmation'
import SendLinkSummary from './SendLinkSummary'
import SendQRSummary from './SendQRSummary'
import { ACTION_SEND } from './utils/sendReceiveFlow'
import { routeAndPathForCode } from './utils/routeAndPathForCode'

// import FaceRecognition from './FaceRecognition/FaceRecognition'
// import FRIntro from './FaceRecognition/FRIntro'
Expand Down Expand Up @@ -103,6 +105,24 @@ const Dashboard = props => {
}
}

//Service redirects Send/Receive
useEffect(() => {
const anyParams = extractQueryParams(window.location.href)

if (anyParams && anyParams.code) {
const { screenProps } = props
const code = readCode(decodeURI(anyParams.code))
routeAndPathForCode('send', code)
.then(({ route, params }) => screenProps.push(route, params))
.catch(e => {
showErrorDialog(null, e, { onDismiss: screenProps.goToRoot })
})
}
if (anyParams && anyParams.paymentCode) {
props.navigation.state.params = anyParams
}
}, [])

const nextFeed = () => {
return getNextFeed(gdstore)
}
Expand Down
6 changes: 5 additions & 1 deletion src/config/config.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
const publicUrl = process.env.REACT_APP_PUBLIC_URL || (window && window.location && window.location.origin)

const Config = {
env: process.env.REACT_APP_ENV || 'development',
version: process.env.VERSION || 'v0',
logLevel: process.env.REACT_APP_LOG_LEVEL || 'debug',
serverUrl: process.env.REACT_APP_SERVER_URL || 'http://localhost:3003',
gunPublicUrl: process.env.REACT_APP_GUN_PUBLIC_URL || 'http://localhost:3003/gun',
web3SiteUrl: process.env.REACT_APP_WEB3_SITE_URL || 'https://w3.gooddollar.org',
publicUrl: process.env.REACT_APP_PUBLIC_URL || (window && window.location && window.location.origin),
publicUrl,
infuraKey: process.env.REACT_APP_INFURA_KEY,
network: process.env.REACT_APP_NETWORK || 'fuse',
zoomLicenseKey: process.env.REACT_APP_ZOOM_LICENSE_KEY,
Expand All @@ -20,6 +22,8 @@ const Config = {
!process.env.REACT_APP_THROW_SAVE_PROFILE_ERRORS || process.env.REACT_APP_THROW_SAVE_PROFILE_ERRORS === 'true',
withMockedFeeds: process.env.REACT_APP_WITH_MOCKED_FEEDS === 'true',
safariMobileKeyboardGuidedSize: process.env.REACT_APP_SAFARI_MOBILE_KEYBOARD_GUIDED_SIZE === 'true',
receiveUrl: process.env.REACT_APP_RECEIVE_URL || `${publicUrl}`,
sendUrl: process.env.REACT_APP_SEND_URL || `${publicUrl}`,
ethereum: {
'42': {
network_id: 42,
Expand Down
29 changes: 24 additions & 5 deletions src/lib/share/__tests__/generateShareLink.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe('generateShareLink', () => {
const link = generateShareLink(action, params)

// Then
expect(link).toEqual(`${Config.publicUrl}/AppNavigation/Dashboard/Home?key=value`)
expect(link).toEqual(`${Config.sendUrl}?key=value`)
})

it(`should return link generated from receive action`, () => {
Expand All @@ -61,7 +61,7 @@ describe('generateShareLink', () => {
const link = generateShareLink(action, params)

// Then
expect(link).toEqual(`${Config.publicUrl}/AppNavigation/Dashboard/Send?key=value`)
expect(link).toEqual(`${Config.receiveUrl}?key=value`)
})

it(`should return link generated from send action, with several query params`, () => {
Expand All @@ -79,10 +79,29 @@ describe('generateShareLink', () => {

// Then
expect(link).toEqual(
`${Config.publicUrl}/AppNavigation/Dashboard/Home?key=value&key2=value2&key3=value3&key4=value4`
`${Config.sendUrl}?key=value&key2=value2&key3=value3&key4=value4`
)
})


it(`should return link generated from send action, with several query params`, () => {
// Given
const action = 'receive'
const params = {
key: 'value',
key2: 'value2',
key3: 'value3',
key4: 'value4',
}

// When
const link = generateShareLink(action, params)

// Then
expect(link).toEqual(
`${Config.receiveUrl}?key=value&key2=value2&key3=value3&key4=value4`
)
})

it(`should return link generated from send action, with encoded query param`, () => {
// Given
const action = 'send'
Expand All @@ -92,6 +111,6 @@ describe('generateShareLink', () => {
const link = generateShareLink(action, params)

// Then
expect(link).toEqual(`${Config.publicUrl}/AppNavigation/Dashboard/Home?key=value%20with%20spaces`)
expect(link).toEqual(`${Config.sendUrl}?key=value%20with%20spaces`)
})
})
30 changes: 25 additions & 5 deletions src/lib/share/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ export function generateShareObject(title: string, text: string, url: string): S
return {
title,
text,
url: encodeURI(url),
url,
}
}

Expand Down Expand Up @@ -178,18 +178,38 @@ type ActionType = 'receive' | 'send'
export function generateShareLink(action: ActionType = 'receive', params: {} = {}): string {
// depending on the action, routes may vary
const destination = {
receive: 'Send',
send: 'Home',
receive: Config.receiveUrl,
send: Config.sendUrl,
}[action]
let queryParams = ''

switch (Config.env) {
case 'production':
if (params.code) {
queryParams = `/${params.code}`
delete params.code
} else if (params.paymentCode) {
queryParams = `/${params.paymentCode}`
delete params.paymentCode
}
break

default:
break
}

// creates query params from params object
const queryParams = toPairs(params)
const additionalParams = toPairs(params)
.map(param => param.join('='))
.join('&')

if (additionalParams.length) {
queryParams += `?${additionalParams}`
}

if (!queryParams || !destination) {
throw new Error(`Link couldn't be generated`)
}

return encodeURI(`${Config.publicUrl}/AppNavigation/Dashboard/${destination}?${queryParams}`)
return encodeURI(`${destination}${queryParams}`)
}

0 comments on commit 3f2e517

Please sign in to comment.