Feature: API endpoint constants (#30)

* Implemented expeced behaviour for text input.

* Added constants for API endpoints. Refactored Footer, Pareto and Search components accordingly.
This commit is contained in:
Honman Yau
2018-12-12 16:54:59 +11:00
committed by Randell Dawson
parent f492176f1d
commit 7131583f65
4 changed files with 67 additions and 38 deletions

View File

@ -1,6 +1,8 @@
import React, { Component } from 'react'; import React, { Component } from 'react';
import styled from 'styled-components'; import styled from 'styled-components';
import { ENDPOINT_INFO } from '../constants';
const Container = styled.div` const Container = styled.div`
margin-top: 5px; margin-top: 5px;
text-align: center; text-align: center;
@ -19,9 +21,7 @@ class Footer extends Component {
} }
componentDidMount() { componentDidMount() {
const baseUrl = 'https://pr-relations.glitch.me/'; fetch(ENDPOINT_INFO)
const fetchUrl = baseUrl + 'info';
fetch(fetchUrl)
.then((response) => response.json()) .then((response) => response.json())
.then(({ ok, numPRs, prRange, lastUpdate }) => { .then(({ ok, numPRs, prRange, lastUpdate }) => {
if (ok) { if (ok) {

View File

@ -1,6 +1,8 @@
import React from 'react'; import React from 'react';
import styled from 'styled-components'; import styled from 'styled-components';
import { ENDPOINT_PARETO } from '../constants';
const Result = styled.div` const Result = styled.div`
word-wrap: break-word; word-wrap: break-word;
margin: 10px 0; margin: 10px 0;
@ -31,7 +33,7 @@ class Pareto extends React.Component {
}; };
componentDidMount() { componentDidMount() {
fetch(`https://pr-relations.glitch.me/pareto`) fetch(ENDPOINT_PARETO)
.then((response) => response.json()) .then((response) => response.json())
.then(({ ok, pareto }) => { .then(({ ok, pareto }) => {
if (ok) { if (ok) {

View File

@ -4,6 +4,9 @@ import Input from './Input';
import PrResults from './PrResults'; import PrResults from './PrResults';
import FilenameResults from './FilenameResults'; import FilenameResults from './FilenameResults';
import SearchOption from './SearchOption'; import SearchOption from './SearchOption';
import { ENDPOINT_PR, ENDPOINT_SEARCH } from '../constants';
class Search extends Component { class Search extends Component {
state = { state = {
searchValue: '', searchValue: '',
@ -39,35 +42,44 @@ class Search extends Component {
if (searchValue) { if (searchValue) {
this.searchPRs(searchValue); this.searchPRs(searchValue);
} }
else {
this.inputRef.current.focus();
}
} }
handleOptionChange = (changeEvent) => { handleOptionChange = (changeEvent) => {
const selectedOption = changeEvent.target.value; const selectedOption = changeEvent.target.value;
this.setState((prevState) => ({ selectedOption, ...this.clearObj })); this.setState((prevState) => ({ selectedOption, ...this.clearObj }));
this.inputRef.current.focus();
} }
searchPRs = (value) => { searchPRs = (value) => {
const { selectedOption } = this.state; const { selectedOption } = this.state;
const baseUrl = 'https://pr-relations.glitch.me/'; const fetchUrl = selectedOption === 'pr' ?
const fetchUrl = baseUrl + (selectedOption === 'pr' ? `pr/${value}` : `search/?value=${value}`); `${ENDPOINT_PR}/${value}` :
`${ENDPOINT_SEARCH}/?value=${value}`;
fetch(fetchUrl) fetch(fetchUrl)
.then((response) => response.json()) .then((response) => response.json())
.then(({ ok, message, results }) => { .then(({ ok, message, results }) => {
if (ok) { if (ok) {
this.setState((prevState) => ({ message, results })); this.setState((prevState) => ({ message, results }));
} }
else {
this.inputRef.current.focus();
}
}) })
.catch(() => { .catch(() => {
this.setState((prevState) => (this.clearObj)); this.setState((prevState) => (this.clearObj));
}); });
} }
componentDidMount() {
this.inputRef.current.focus();
}
render() { render() {
const { handleButtonClick, handleInputEvent, inputRef, handleOptionChange, state } = this; const { handleButtonClick, handleInputEvent, inputRef, handleOptionChange, state } = this;
const { searchValue, message, results, selectedOption } = state; const { searchValue, message, results, selectedOption } = state;
return ( return (
<> <>
<div> <div>

View File

@ -0,0 +1,15 @@
const API_HOST = !!process.env.REACT_APP_DEV ?
'http://localhost:3001/' :
'https://pr-relations.glitch.me';
const ENDPOINT_INFO = API_HOST + '/info';
const ENDPOINT_PARETO = API_HOST + '/pareto';
const ENDPOINT_PR = API_HOST + '/pr';
const ENDPOINT_SEARCH = API_HOST + '/search'
export {
API_HOST,
ENDPOINT_INFO,
ENDPOINT_PARETO,
ENDPOINT_PR,
ENDPOINT_SEARCH
};