Skip to content

Commit 9110f3b

Browse files
Merge pull request #1186 from suppermancool/f2f-30069264-issue-1029
fix issue #1029
2 parents 852eaa5 + ebbca7a commit 9110f3b

File tree

5 files changed

+91
-5
lines changed

5 files changed

+91
-5
lines changed

__tests__/shared/components/Settings/Profile/ExternalLinks/__snapshots__/index.jsx.snap

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,39 @@ exports[`renders external links section of profile setting page correctly 1`] =
2626
</div>
2727
<AddWebLink
2828
addWebLink={[Function]}
29+
allLinks={
30+
Array [
31+
Object {
32+
"data": Object {
33+
"handle": "tcscoder",
34+
},
35+
"providerType": "github",
36+
"status": "pending",
37+
},
38+
Object {
39+
"data": Object {
40+
"handle": "1233342",
41+
},
42+
"providerType": "stackoverflow",
43+
"status": "pending",
44+
},
45+
Object {
46+
"URL": "https://www.google.com",
47+
"description": "Search the world's information, including webpages, images, videos and more. Google has many special features to help you find exactly what you're looking for.",
48+
"entities": null,
49+
"handle": "tcscoder",
50+
"images": "https://www.google.com/images/branding/googlelogo/1x/googlelogo_white_background_color_272x92dp.png",
51+
"key": "4c7136cdbbf227274e49c475e7541abf",
52+
"keywords": null,
53+
"providerType": "weblink",
54+
"source": "embed.ly",
55+
"status": "linked",
56+
"synchronizedAt": 1525560141959,
57+
"title": "Google",
58+
"userId": 22655076,
59+
},
60+
]
61+
}
2962
deleteWebLink={[Function]}
3063
handle=""
3164
linkExternalAccount={[Function]}

src/shared/components/Settings/Account/LinkedAccount/AddWebLink.jsx

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
*/
44
/* eslint-disable jsx-a11y/label-has-for */
55
import React from 'react';
6+
import _ from 'lodash';
67
import PT from 'prop-types';
78

89
import './styles.scss';
@@ -17,6 +18,7 @@ export default class AddWebLink extends React.Component {
1718
this.onUpdateWebLink = this.onUpdateWebLink.bind(this);
1819
this.onAddWebLink = this.onAddWebLink.bind(this);
1920
this.isWebLinkValid = this.isWebLinkValid.bind(this);
21+
this.isWebLinkExist = this.isWebLinkExist.bind(this);
2022
}
2123

2224
componentWillReceiveProps(nextProps) {
@@ -43,7 +45,7 @@ export default class AddWebLink extends React.Component {
4345
tokenV3,
4446
} = this.props;
4547
const { webLink } = this.state;
46-
if (webLink && this.isWebLinkValid()) {
48+
if (webLink && this.isWebLinkValid() && !this.isWebLinkExist()) {
4749
addWebLink(handle, tokenV3, webLink);
4850
}
4951
}
@@ -54,10 +56,19 @@ export default class AddWebLink extends React.Component {
5456
return !webLink || /^(http(s?):\/\/)?(www\.)?[a-zA-Z0-9\.\-\_]+(\.[a-zA-Z]{2,15})+(\/[a-zA-Z0-9\_\-\s\.\/\?\%\#\&\=]*)?$/.test(webLink); /* eslint-disable-line no-useless-escape */
5557
}
5658

59+
isWebLinkExist() {
60+
const { webLink } = this.state;
61+
const {
62+
allLinks,
63+
} = this.props;
64+
return _.some(allLinks, link => link.URL.toLowerCase() === webLink.toLowerCase());
65+
}
66+
5767
render() {
5868
const { webLink } = this.state;
5969

6070
const webLinkValid = this.isWebLinkValid();
71+
const isWebLinkExist = this.isWebLinkExist();
6172

6273
return (
6374
<div styleName="external-web-link">
@@ -83,7 +94,7 @@ export default class AddWebLink extends React.Component {
8394
required
8495
/>
8596
{
86-
!webLinkValid
97+
!webLinkValid && !isWebLinkExist
8798
&& (
8899
<div styleName="form-input-error">
89100
<p>
@@ -92,6 +103,16 @@ Please enter a valid URL
92103
</div>
93104
)
94105
}
106+
{
107+
isWebLinkExist
108+
&& (
109+
<div styleName="form-input-error">
110+
<p>
111+
{`The URL ${webLink} already exists`}
112+
</p>
113+
</div>
114+
)
115+
}
95116
</div>
96117
</form>
97118
</div>
@@ -100,9 +121,14 @@ Please enter a valid URL
100121
}
101122
}
102123

124+
AddWebLink.defaultProps = {
125+
allLinks: [],
126+
};
127+
103128
AddWebLink.propTypes = {
104129
handle: PT.string.isRequired,
105130
tokenV3: PT.string.isRequired,
106131
profileState: PT.shape().isRequired,
107132
addWebLink: PT.func.isRequired,
133+
allLinks: PT.arrayOf(PT.shape),
108134
};

src/shared/components/Settings/Account/LinkedAccount/index.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,7 @@ export default function LinkedAccount(props) {
154154
</h1>
155155
<AddWebLink
156156
{...props}
157+
allLinks={allLinks}
157158
/>
158159
<LinkAccounts
159160
{...props}

src/shared/components/Settings/Profile/ExternalLinks/AddWebLink.jsx

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
* Renders 'Add a Web Link' section.
33
*/
44
import React from 'react';
5+
import _ from 'lodash';
56
import PT from 'prop-types';
67

78
import { PrimaryButton } from 'topcoder-react-ui-kit';
@@ -17,6 +18,7 @@ export default class AddWebLink extends React.Component {
1718
this.onUpdateWebLink = this.onUpdateWebLink.bind(this);
1819
this.onAddWebLink = this.onAddWebLink.bind(this);
1920
this.isWebLinkValid = this.isWebLinkValid.bind(this);
21+
this.webLinkExists = this.webLinkExists.bind(this);
2022
}
2123

2224
componentWillReceiveProps(nextProps) {
@@ -41,7 +43,7 @@ export default class AddWebLink extends React.Component {
4143
tokenV3,
4244
} = this.props;
4345
const { webLink } = this.state;
44-
if (webLink && this.isWebLinkValid()) {
46+
if (webLink && this.isWebLinkValid() && !this.webLinkExists()) {
4547
addWebLink(handle, tokenV3, webLink);
4648
}
4749
}
@@ -51,6 +53,14 @@ export default class AddWebLink extends React.Component {
5153
return !webLink || /^(http(s?):\/\/)?(www\.)?[a-zA-Z0-9\.\-\_]+(\.[a-zA-Z]{2,15})+(\/[a-zA-Z0-9\_\-\s\.\/\?\%\#\&\=]*)?$/.test(webLink); /* eslint-disable-line no-useless-escape */
5254
}
5355

56+
webLinkExists() {
57+
const { webLink } = this.state;
58+
const {
59+
allLinks,
60+
} = this.props;
61+
return _.some(allLinks, link => link.URL.toLowerCase() === webLink.toLowerCase());
62+
}
63+
5464
render() {
5565
const {
5666
profileState,
@@ -60,15 +70,15 @@ export default class AddWebLink extends React.Component {
6070
const { webLink } = this.state;
6171

6272
const webLinkValid = this.isWebLinkValid();
63-
73+
const webLinkExists = this.webLinkExists();
6474
return (
6575
<div styleName="external-web-link">
6676
<div styleName="web-link">
6777
<form name="addWebLinkFrm" onSubmit={this.onAddWebLink} autoComplete="off">
6878
<div styleName={webLinkValid ? 'validation-bar url' : 'validation-bar url error-bar'}>
6979
<input id="web-link-input" name="url" type="text" styleName="url" value={webLink} onChange={this.onUpdateWebLink} placeholder="http://www.yourlink.com" required />
7080
{
71-
!webLinkValid
81+
!webLinkValid && !webLinkExists
7282
&& (
7383
<div styleName="form-input-error">
7484
<p>
@@ -77,6 +87,16 @@ Please enter a valid URL
7787
</div>
7888
)
7989
}
90+
{
91+
webLinkExists
92+
&& (
93+
<div styleName="form-input-error">
94+
<p>
95+
{`The URL ${webLink} already exists`}
96+
</p>
97+
</div>
98+
)
99+
}
80100
</div>
81101
<PrimaryButton
82102
disabled={!webLink || !webLinkValid || addingWebLink}
@@ -97,9 +117,14 @@ Please enter a valid URL
97117
}
98118
}
99119

120+
AddWebLink.defaultProps = {
121+
allLinks: [],
122+
};
123+
100124
AddWebLink.propTypes = {
101125
handle: PT.string.isRequired,
102126
tokenV3: PT.string.isRequired,
103127
profileState: PT.shape().isRequired,
104128
addWebLink: PT.func.isRequired,
129+
allLinks: PT.arrayOf(PT.shape),
105130
};

src/shared/components/Settings/Profile/ExternalLinks/index.jsx

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,7 @@ Add a web link
159159
</div>
160160
<AddWebLink
161161
{...props}
162+
allLinks={allLinks}
162163
/>
163164
<div className="form-label">
164165
Link Your Accounts

0 commit comments

Comments
 (0)