Skip to content
This repository has been archived by the owner on Oct 17, 2020. It is now read-only.

Commit

Permalink
Add query parameter to autofill long link (#485)
Browse files Browse the repository at this point in the history
* Add query parameter to autofill long link

* Add query parameter to autofill long link
  • Loading branch information
JackieYJC authored and coderworld10 committed Jan 20, 2020
1 parent bd78e93 commit 44d1f50
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
9 changes: 8 additions & 1 deletion frontend/src/component/form/TextField.tsx
@@ -1,4 +1,4 @@
import React, { ChangeEvent, Component } from 'react';
import React, { ChangeEvent, Component, createRef } from 'react';
import './TextField.scss';

interface Props {
Expand All @@ -13,6 +13,8 @@ interface State {
}

export class TextField extends Component<Props, State> {
textInput = createRef<HTMLInputElement>();

handleChange = (event: ChangeEvent<HTMLInputElement>) => {
if (!this.props.onChange) {
return;
Expand All @@ -27,9 +29,14 @@ export class TextField extends Component<Props, State> {
this.props.onBlur();
};

focus = () => {
this.textInput.current!.focus();
};

render = () => {
return (
<input
ref={this.textInput}
className={'text-field'}
type={'text'}
value={this.props.text}
Expand Down
19 changes: 19 additions & 0 deletions frontend/src/component/pages/Home.tsx
Expand Up @@ -57,6 +57,7 @@ interface State {
export class Home extends Component<Props, State> {
errModal = React.createRef<Modal>();
signInModal = React.createRef<SignInModal>();
shortLinkTextField = React.createRef<TextField>();

constructor(props: Props) {
super(props);
Expand All @@ -69,7 +70,19 @@ export class Home extends Component<Props, State> {
this.showSignInModal();
return;
}
this.handleStateChange();
this.autoFillLongLink();
}

autoFillLongLink() {
const longLink = this.getLongLinkFromQueryParams();
if (validateLongLinkFormat(longLink) == null) {
this.props.store.dispatch(updateLongLink(longLink));
this.shortLinkTextField.current!.focus();
}
}

handleStateChange() {
this.props.store.subscribe(async () => {
const state = this.props.store.getState();

Expand Down Expand Up @@ -150,6 +163,11 @@ export class Home extends Component<Props, State> {
});
};

getLongLinkFromQueryParams(): string {
let urlParams = new URLSearchParams(window.location.search);
return urlParams.get('long_link')!;
}

showError(error?: IErr) {
if (!error) {
return;
Expand All @@ -175,6 +193,7 @@ export class Home extends Component<Props, State> {
</div>
<div className={'text-field-wrapper'}>
<TextField
ref={this.shortLinkTextField}
text={this.state.alias}
placeHolder={'Custom Short Link ( Optional )'}
onBlur={this.handlerCustomAliasTextFieldBlur}
Expand Down

0 comments on commit 44d1f50

Please sign in to comment.