Skip to content

Commit

Permalink
Add Protocol if needed for URL
Browse files Browse the repository at this point in the history
  • Loading branch information
jedtan committed Sep 15, 2023
1 parent a69ef39 commit 36d5fb8
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 16 deletions.
16 changes: 1 addition & 15 deletions src/components/AddShortcut.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
/* eslint no-useless-escape: 0 */

import React, { useEffect, useState } from 'react'
import { makeStyles } from '@material-ui/core/styles'
import Button from '@material-ui/core/Button'
import PropTypes from 'prop-types'
import { Typography } from '@material-ui/core'
import TextField from '@material-ui/core/TextField'
import { addProtocolToURLIfNeeded } from 'src/utils/urls'
import Notification from './Notification'

const useStyles = makeStyles((theme) => ({
Expand Down Expand Up @@ -37,19 +36,6 @@ const useStyles = makeStyles((theme) => ({
},
}))

const addProtocolToURLIfNeeded = (url) => {
const hasProtocol = (s) => {
const regexp =
/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
return regexp.test(s)
}

if (!hasProtocol(url)) {
return `http://${url}`
}
return url
}

const isValidUrl = (urlString) => {
try {
return Boolean(new URL(urlString))
Expand Down
7 changes: 6 additions & 1 deletion src/components/ShortcutIcon.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import EditIcon from '@material-ui/icons/Edit'
import CheckIcon from '@material-ui/icons/Check'
import Link from 'src/components/Link'
import CloseIcon from '@material-ui/icons/Close'
import { addProtocolToURLIfNeeded } from 'src/utils/urls'

const useStyles = makeStyles((theme) => ({
button: {
Expand Down Expand Up @@ -131,7 +132,11 @@ const ShortcutIcon = ({ onEdit, onDelete, text, url, id }) => {
<Typography className={classes.deleteText}>Confirm Delete</Typography>
</div>
) : (
<Link to={url} target="_blank" className={classes.link}>
<Link
to={addProtocolToURLIfNeeded(url)}
target="_blank"
className={classes.link}
>
<Fade in={hover}>
<div className={classes.buttons}>
<IconButton
Expand Down
9 changes: 9 additions & 0 deletions src/components/ShortcutIcon.stories.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,12 @@ basic.args = {
onEdit: () => console.log('onEdit'),
onDelete: () => console.log('onDelete'),
}

export const withoutHttps = Template.bind({})
withoutHttps.args = {
id: 'abcd',
text: 'Google Googledy',
url: 'www.google.com',
onEdit: () => console.log('onEdit'),
onDelete: () => console.log('onDelete'),
}
10 changes: 10 additions & 0 deletions src/components/__tests__/ShortcutIcon.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,16 @@ describe('ShortcutIcon component', () => {
expect(wrapper.find(Link).prop('to')).toEqual(mockProps.url)
})

it('has an https link with url property', () => {
const ShortcutIcon = require('src/components/ShortcutIcon').default
const mockProps = {
...getMockProps(),
url: 'www.google.com',
}
const wrapper = mount(<ShortcutIcon {...mockProps} />)
expect(wrapper.find(Link).prop('to')).toEqual('http://www.google.com')
})

it('displays abbreviated version of link name along with full title', () => {
const ShortcutIcon = require('src/components/ShortcutIcon').default
const mockProps = getMockProps()
Expand Down
7 changes: 7 additions & 0 deletions src/utils/__tests__/urls.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,3 +35,10 @@ describe('Urls: getSquadsLink', () => {
)
})
})

describe('addProtocolToURLIfNeeded', () => {
it('appends https if applicable', () => {
const { addProtocolToURLIfNeeded } = require('src/utils/urls')
expect(addProtocolToURLIfNeeded('test-user')).toBe('http://test-user')
})
})
15 changes: 15 additions & 0 deletions src/utils/urls.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/* eslint no-useless-escape: 0 */

import ensureValuesAreDefined from 'src/utils/ensureValuesAreDefined'
import { withBasePath } from 'src/utils/navigationUtils'
import { MEDIA_ENDPOINT } from 'src/utils/constants'
Expand Down Expand Up @@ -108,3 +110,16 @@ export const shopLandingURL = 'https://shop.gladly.io/'

export const groupImpactLeaderboardFAQ =
'https://gladly.zendesk.com/hc/en-us/articles/17622871939725-Leaderboards'

export const addProtocolToURLIfNeeded = (url) => {
const hasProtocol = (s) => {
const regexp =
/(ftp|http|https):\/\/(\w+:{0,1}\w*@)?(\S+)(:[0-9]+)?(\/|\/([\w#!:.?+=&%@!\-\/]))?/
return regexp.test(s)
}

if (!hasProtocol(url)) {
return `http://${url}`
}
return url
}

0 comments on commit 36d5fb8

Please sign in to comment.