Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: formatting of URL, closes #1256 #1259

Merged
merged 1 commit into from
Dec 20, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 4 additions & 25 deletions src/pages/Settings/content/formSections/Fields/Link.field.tsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Component } from 'react';
import { Component } from 'react'
import { COM_TYPE_MOCKS } from 'src/mocks/Selectors'
import { Field } from 'react-final-form'
import { InputField } from 'src/components/Form/Fields'
Expand All @@ -7,12 +7,8 @@ import { Modal } from 'src/components/Modal/Modal'
import Text from 'src/components/Text'
import Flex from 'src/components/Flex'
import { SelectField } from 'src/components/Form/Select.field'
import {
validateUrl,
validateEmail,
required,
ensureExternalUrl,
} from 'src/utils/validators'
import { validateUrl, validateEmail, required } from 'src/utils/validators'
import { formatLink } from 'src/utils/formatters'

interface IProps {
name: string
Expand Down Expand Up @@ -44,23 +40,6 @@ export class ProfileLinkField extends Component<IProps, IState> {
this.toggleDeleteModal()
this.props.onDelete()
}
// TODO - we might want to add more formatting for cases where,
// e.g. only a username is given for a bazar link
public formatLink(link: string) {
link = link && link.toLowerCase()
Copy link
Collaborator Author

@thisislawatts thisislawatts Nov 10, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

😱 I have inadvertently hidden the functional change as I refactored to introduce tests, but the key change here was removing link.toLowerCase()

switch (this.state.linkType) {
case 'forum':
return ensureExternalUrl(link)
case 'website':
return ensureExternalUrl(link)
case 'social media':
return ensureExternalUrl(link)
case 'bazar':
return ensureExternalUrl(link)
default:
return link
}
}
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

refactor: Move out to a pure function, simplifying the test setup.


public validateDependingOnType(e) {
switch (this.state.linkType) {
Expand Down Expand Up @@ -119,7 +98,7 @@ export class ProfileLinkField extends Component<IProps, IState> {
validateFields={[]}
component={InputField}
placeholder="Link"
format={v => this.formatLink(v)}
format={v => formatLink(v, this.state.linkType)}
formatOnBlur={true}
/>
<DeleteButton
Expand Down
19 changes: 19 additions & 0 deletions src/utils/formatters.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import { formatLink } from './formatters'

describe('formatLink', () => {
it('preserves casing of original string', () => {
expect(formatLink('ABC')).toBe('ABC')
})

describe.each([
['forum', 'https://example.com'],
['website', 'https://example.com'],
['social media', 'https://example.com'],
['bazar', 'https://example.com'],
[undefined, 'example.com'],
])('adds protocal to an external link', (scene, expectation) => {
it(`${scene}`, () => {
expect(formatLink('example.com', scene)).toBe(expectation)
})
})
})
13 changes: 13 additions & 0 deletions src/utils/formatters.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { ensureExternalUrl } from './validators'

// TODO - we might want to add more formatting for cases where,
// e.g. only a username is given for a bazar link
export function formatLink(unformattedLinkString: string, linkType?: string) {
const link = unformattedLinkString

if (['forum', 'website', 'social media', 'bazar'].includes(linkType || '')) {
return ensureExternalUrl(link)
}

return link
}