From 23b9774a85d7dbfc27805325be330006cff9afa4 Mon Sep 17 00:00:00 2001 From: Dessalines Date: Tue, 30 Apr 2019 07:45:50 -0700 Subject: [PATCH] Adding simple suggested post title from textance. - Fixes #112 --- ui/src/components/post-form.tsx | 20 ++++++++++++++++++-- ui/src/utils.ts | 6 ++++++ 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/ui/src/components/post-form.tsx b/ui/src/components/post-form.tsx index 0154a923f..849797132 100644 --- a/ui/src/components/post-form.tsx +++ b/ui/src/components/post-form.tsx @@ -3,7 +3,7 @@ import { Subscription } from "rxjs"; import { retryWhen, delay, take } from 'rxjs/operators'; import { PostForm as PostFormI, Post, PostResponse, UserOperation, Community, ListCommunitiesResponse, ListCommunitiesForm, SortType } from '../interfaces'; import { WebSocketService, UserService } from '../services'; -import { msgOp } from '../utils'; +import { msgOp, getPageTitle } from '../utils'; import * as autosize from 'autosize'; interface PostFormProps { @@ -18,6 +18,7 @@ interface PostFormState { postForm: PostFormI; communities: Array; loading: boolean; + suggestedTitle: string; } export class PostForm extends Component { @@ -31,7 +32,8 @@ export class PostForm extends Component { creator_id: (UserService.Instance.user) ? UserService.Instance.user.id : null, }, communities: [], - loading: false + loading: false, + suggestedTitle: undefined, } constructor(props: any, context: any) { @@ -83,6 +85,10 @@ export class PostForm extends Component {
+ {this.state.suggestedTitle && + copy suggested title: {this.state.suggestedTitle} + + }
@@ -135,8 +141,18 @@ export class PostForm extends Component { i.setState(i.state); } + copySuggestedTitle(i: PostForm) { + i.state.postForm.name = i.state.suggestedTitle; + i.state.suggestedTitle = undefined; + i.setState(i.state); + } + 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); + }); i.setState(i.state); } diff --git a/ui/src/utils.ts b/ui/src/utils.ts index 3baf2367d..d0c7c89a6 100644 --- a/ui/src/utils.ts +++ b/ui/src/utils.ts @@ -111,3 +111,9 @@ export function routeListingTypeToEnum(type: string): ListingType { return ListingType[capitalizeFirstLetter(type)]; } +export async function getPageTitle(url: string) { + let res = await fetch(`https://textance.herokuapp.com/title/${url}`); + let data = await res.text(); + return data; +} +