Skip to content

Commit

Permalink
Add URL validation and saving functionality to PersonalPreferences co…
Browse files Browse the repository at this point in the history
…mponent
  • Loading branch information
MAX-786 committed Jul 2, 2024
1 parent 3512b76 commit 03b4659
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,8 @@ import languages from '@plone/volto/constants/Languages';
import { changeLanguage } from '@plone/volto/actions';
import { toGettextLang } from '@plone/volto/helpers';
import config from '@plone/volto/registry';

const urls = {
URL1: 'http://localhost:3000',
URL2: 'http://localhost:3001',
URL3: 'http://localhost:3002',
};
import getSavedURLs from '../../../../utils/getSavedURLs';
import isValidUrl from '../../../../utils/isValidUrl';

const messages = defineMessages({
personalPreferences: {
Expand Down Expand Up @@ -98,6 +94,7 @@ class PersonalPreferences extends Component {
super(props);
this.onCancel = this.onCancel.bind(this);
this.onSubmit = this.onSubmit.bind(this);
this.urls = getSavedURLs();
this.state = {
hidden: true,
};
Expand All @@ -118,8 +115,31 @@ class PersonalPreferences extends Component {
});
}
toast.success(
<Toast success title={this.props.intl.formatMessage(messages.saved)} />,
<Toast
success
content={''}
title={this.props.intl.formatMessage(messages.saved)}
/>,
);
if (data.urlCheck) {
if (!isValidUrl(data.url)) {
toast.error(
<Toast
error
content={'Please enter a valid URL or select URL from the options.'}
title={'Invalid Entered URL!'}
/>,
);
return;
}
const urlList = [...new Set([this.urls, data.url])];
this.props.cookies.set('saved_urls', urlList.join(','), {
expires: new Date(Date.now() + 7 * 24 * 60 * 60 * 1000), // 7 Days
});
console.log('data.url', data.url);
} else {
console.log('data.urls', data.urls);
}
this.props.closeMenu();
}

Expand All @@ -130,7 +150,6 @@ class PersonalPreferences extends Component {
*/
onCancel() {
this.props.closeMenu();
toast.error(<Toast error title={'Invalid Entered URL!.'} />);
}

/**
Expand All @@ -140,6 +159,7 @@ class PersonalPreferences extends Component {
*/
render() {
const { cookies } = this.props;
const urls = this.urls;
return (
<Form
formData={{
Expand All @@ -156,7 +176,7 @@ class PersonalPreferences extends Component {
{
id: 'frontend',
title: 'Frontend',
fields: ['urls', 'urlCheck', 'url'],
fields: ['urls', 'url', 'urlCheck'],
},
],
properties: {
Expand All @@ -174,7 +194,7 @@ class PersonalPreferences extends Component {
),
title: this.props.intl.formatMessage(messages.frontendUrls),
type: 'string',
choices: map(keys(urls), (url) => [url, urls[url]]),
choices: map(urls, (url) => [url, url]),
mode: !this.state.hidden ? 'hidden' : '',
},
urlCheck: {
Expand Down
33 changes: 33 additions & 0 deletions packages/volto-hydra/src/utils/getSavedURLs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import Cookies from 'js-cookie';
import isValidUrl from './isValidUrl';

/**
* Get the default URL(s) from the environment
* @returns {Array} URL(s) from the environment
*/
export const getURlsFromEnv = () => {
const presetUrlsString =
process.env['RAZZLE_DEFAULT_IFRAME_URL'] ||
(typeof window !== 'undefined' &&
window.env['RAZZLE_DEFAULT_IFRAME_URL']) ||
'http://localhost:3002'; // fallback if env is not set

const presetUrls = presetUrlsString.split(',');
return presetUrls;
};

/**
* Get the saved URLs from the cookies
* @returns {Array} Saved URLs
*/
const getSavedURLs = () => {
const urls = Cookies.get('saved_urls')
? Cookies.get('saved_urls').split(',')
: [];
const savedUrls = [
...new Set([...urls, ...getURlsFromEnv()]), // Merge saved URLs with default URLs make sure they are unique
];
return savedUrls.filter(isValidUrl);
};

export default getSavedURLs;
9 changes: 7 additions & 2 deletions packages/volto-hydra/src/utils/isValidUrl.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
export default function isValidUrl(string) {
/**
* Check if the URL is valid
* @param {URL} string
* @returns bool
*/
export default function isValidUrl(url) {
try {
new URL(string);
new URL(url);
return true;
} catch (error) {
return false;
Expand Down

0 comments on commit 03b4659

Please sign in to comment.