diff options
author | Dessalines <happydooby@gmail.com> | 2019-08-16 18:46:39 -0700 |
---|---|---|
committer | Dessalines <happydooby@gmail.com> | 2019-08-16 18:46:39 -0700 |
commit | 778a0a5ff17c3eb6b6b5aa3173eeae8e0e0cc774 (patch) | |
tree | 107f5884e9f62b8befa546104c0329e3ad9b4221 | |
parent | 3c14bf40e3a572e583a98c66f60fc881d3c494a6 (diff) |
Fixing create post bug.
- Fixes #202
-rw-r--r-- | ui/src/components/post-form.tsx | 18 | ||||
-rw-r--r-- | ui/src/utils.ts | 10 |
2 files changed, 21 insertions, 7 deletions
diff --git a/ui/src/components/post-form.tsx b/ui/src/components/post-form.tsx index 704b1cdd..42620c9f 100644 --- a/ui/src/components/post-form.tsx +++ b/ui/src/components/post-form.tsx @@ -4,7 +4,7 @@ import { Subscription } from "rxjs"; import { retryWhen, delay, take } from 'rxjs/operators'; import { PostForm as PostFormI, Post, PostResponse, UserOperation, Community, ListCommunitiesResponse, ListCommunitiesForm, SortType, SearchForm, SearchType, SearchResponse } from '../interfaces'; import { WebSocketService, UserService } from '../services'; -import { msgOp, getPageTitle, debounce, capitalizeFirstLetter } from '../utils'; +import { msgOp, getPageTitle, debounce, validURL, capitalizeFirstLetter } from '../utils'; import * as autosize from 'autosize'; import { i18n } from '../i18next'; import { T } from 'inferno-i18next'; @@ -91,7 +91,7 @@ export class PostForm extends Component<PostFormProps, PostFormState> { <div class="form-group row"> <label class="col-sm-2 col-form-label"><T i18nKey="url">#</T></label> <div class="col-sm-10"> - <input type="url" class="form-control" value={this.state.postForm.url} onInput={linkEvent(this, debounce(this.handlePostUrlChange))} /> + <input type="url" class="form-control" value={this.state.postForm.url} onInput={linkEvent(this, this.handlePostUrlChange)} /> {this.state.suggestedTitle && <div class="mt-1 text-muted small font-weight-bold pointer" onClick={linkEvent(this, this.copySuggestedTitle)}><T i18nKey="copy_suggested_title" interpolation={{title: this.state.suggestedTitle}}>#</T></div> } @@ -100,7 +100,7 @@ export class PostForm extends Component<PostFormProps, PostFormState> { <div class="form-group row"> <label class="col-sm-2 col-form-label"><T i18nKey="title">#</T></label> <div class="col-sm-10"> - <textarea value={this.state.postForm.name} onInput={linkEvent(this, debounce(this.handlePostNameChange))} class="form-control" required rows={2} minLength={3} maxLength={100} /> + <textarea value={this.state.postForm.name} onInput={linkEvent(this, this.handlePostNameChange)} class="form-control" required rows={2} minLength={3} maxLength={100} /> {this.state.suggestedPosts.length > 0 && <> <div class="my-1 text-muted small font-weight-bold"><T i18nKey="related_posts">#</T></div> @@ -169,10 +169,14 @@ export class PostForm extends Component<PostFormProps, PostFormState> { handlePostUrlChange(i: PostForm, event: any) { i.state.postForm.url = event.target.value; - getPageTitle(i.state.postForm.url).then(d => { - i.state.suggestedTitle = d; - i.setState(i.state); - }); + if (validURL(i.state.postForm.url)) { + getPageTitle(i.state.postForm.url).then(d => { + i.state.suggestedTitle = d; + i.setState(i.state); + }); + } else { + i.state.suggestedTitle = undefined; + } i.setState(i.state); } diff --git a/ui/src/utils.ts b/ui/src/utils.ts index c48b00c6..c6f43c94 100644 --- a/ui/src/utils.ts +++ b/ui/src/utils.ts @@ -84,6 +84,16 @@ export function isImage(url: string) { return imageRegex.test(url); } +export function validURL(str: string) { + var pattern = new RegExp('^(https?:\\/\\/)?'+ // protocol + '((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|'+ // domain name + '((\\d{1,3}\\.){3}\\d{1,3}))'+ // OR ip (v4) address + '(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*'+ // port and path + '(\\?[;&a-z\\d%_.~+=-]*)?'+ // query string + '(\\#[-a-z\\d_]*)?$','i'); // fragment locator + return !!pattern.test(str); +} + export let fetchLimit: number = 20; export function capitalizeFirstLetter(str: string): string { |