From 52fc6624ad9c76d324cee3891766f7593e1f6615 Mon Sep 17 00:00:00 2001 From: Juan Bono Date: Thu, 4 May 2017 04:51:49 -0300 Subject: [PATCH] [GD-11] add a component for displaying categories data --- front/src/App.js | 15 ++-------- front/src/components/Category.js | 5 ++++ front/src/components/CategoryChild.js | 24 +++++++++++++-- front/src/components/GrandCategory.js | 10 +++---- front/src/components/Home.js | 24 +++------------ front/src/components/NoMatch.js | 17 +++++++++++ front/src/components/index.js | 4 ++- front/src/types.js | 43 +++++++++++++++++++++++++-- front/src/utils/fetchData.js | 25 ++++++++++++++++ front/src/utils/index.js | 5 ++++ 10 files changed, 128 insertions(+), 44 deletions(-) create mode 100644 front/src/components/Category.js create mode 100644 front/src/components/NoMatch.js create mode 100644 front/src/utils/fetchData.js create mode 100644 front/src/utils/index.js diff --git a/front/src/App.js b/front/src/App.js index 31bde5f..dcc11a7 100644 --- a/front/src/App.js +++ b/front/src/App.js @@ -1,19 +1,8 @@ // @flow import React, { Component } from 'react'; -import { CategoryChild, Home } from './components/index'; -import { BrowserRouter as Router, Route, Link, Switch } from 'react-router-dom'; - -class NoMatch extends Component { - render() { - return ( -
-

Not Found

-

Go home

-
- ) - } -} +import { CategoryChild, Home, NoMatch } from './components/index'; +import { BrowserRouter as Router, Route, Switch } from 'react-router-dom'; class App extends Component { render() { diff --git a/front/src/components/Category.js b/front/src/components/Category.js new file mode 100644 index 0000000..d20a89d --- /dev/null +++ b/front/src/components/Category.js @@ -0,0 +1,5 @@ +import React, { Component } from 'react'; + +class Category extends Component { + +} \ No newline at end of file diff --git a/front/src/components/CategoryChild.js b/front/src/components/CategoryChild.js index 9164eb5..ccdebc8 100644 --- a/front/src/components/CategoryChild.js +++ b/front/src/components/CategoryChild.js @@ -1,10 +1,28 @@ import React, { Component } from 'react'; +import * as T from '../types'; +import { fetchData } from '../utils/index'; + +function extractUid(link : string) { + const lastOccur = link.lastIndexOf('-'); + return link.slice(lastOccur + 1); +} class CategoryChild extends Component { + state: { + cat: T.Cat; + }; + componentWillMount() { + this.setState({ cat: [] }); + } + componentDidMount() { + var uid = extractUid(this.props.match.params.uid); + + fetchData('http://localhost:8080/haskell/api/category/'+uid) + .then(data => this.setState({cat: data})) + } render() { - return ( -

hi

- ) + return ( +

{ this.state.cat.title }

) } } diff --git a/front/src/components/GrandCategory.js b/front/src/components/GrandCategory.js index 8e15d4f..fe008e6 100644 --- a/front/src/components/GrandCategory.js +++ b/front/src/components/GrandCategory.js @@ -10,8 +10,8 @@ class GrandCategory extends Component { render() { const renderBigCatLink = cat => { return( - - {cat.title} + + {cat.title} @@ -37,7 +37,7 @@ class GrandCategory extends Component { const grand = this.props.val; return( -
+

{grand.title}

diff --git a/front/src/components/Home.js b/front/src/components/Home.js index e7b0c42..a5f445a 100644 --- a/front/src/components/Home.js +++ b/front/src/components/Home.js @@ -2,18 +2,7 @@ import React, { Component } from 'react'; import { GrandCategory } from './GrandCategory'; import { Tiles } from './Tiles'; import { GrandCategoryT } from '../types'; - -function checkStatus(response) { - if (response.status >= 200 && response.status < 300) { - return response; - } else { - throw new Error(response.statusText); - } -} - -function parseJSON(response) { - return response.json(); -} +import { fetchData } from '../utils/index'; class Home extends Component { state: { @@ -23,19 +12,14 @@ class Home extends Component { this.setState({ categories: [] }); } componentDidMount() { - fetch('http://localhost:8080/haskell/api/all-categories') - .then(checkStatus).then(parseJSON) - .then(data => { - this.setState({ categories: data }); - }).catch(function(error) { - console.log('request failed', error); - }); + fetchData('http://localhost:8080/haskell/api/all-categories') + .then(data => this.setState({categories: data})) } render() { return ( {this.state.categories.map(grand => { - return ; + return ; }) } diff --git a/front/src/components/NoMatch.js b/front/src/components/NoMatch.js new file mode 100644 index 0000000..133aa7a --- /dev/null +++ b/front/src/components/NoMatch.js @@ -0,0 +1,17 @@ +import React, { Component } from 'react'; +import { Link } from 'react-router-dom'; + +class NoMatch extends Component { + render() { + return ( +
+

Not Found

+

Go home

+
+ ) + } +} + +module.exports = { + NoMatch +} \ No newline at end of file diff --git a/front/src/components/index.js b/front/src/components/index.js index b493b5e..98d4aac 100644 --- a/front/src/components/index.js +++ b/front/src/components/index.js @@ -2,10 +2,12 @@ import { GrandCategory } from './GrandCategory'; import { Tiles } from './Tiles'; import { CategoryChild } from './CategoryChild'; import { Home } from './Home'; +import { NoMatch } from './NoMatch'; module.exports = { GrandCategory, Tiles, CategoryChild, - Home + Home, + NoMatch } \ No newline at end of file diff --git a/front/src/types.js b/front/src/types.js index fafa3cd..2a8b96d 100644 --- a/front/src/types.js +++ b/front/src/types.js @@ -3,11 +3,50 @@ export type Category = { uid : string, link : string, title : string - }; +}; export type GrandCategoryT = { title : string, finished : Array, wip : Array, stubs : Array - } +}; + +export type Description = { + text : string, + html : string +}; + +export type Valoration = { + uid : string, + content : Description, +}; + +export type Kind = { + tag : string, + hackageName : string +}; + +export type Note = { + text : string, + pros : Array, + cons : Array +}; + +export type Item = { + ecosystem : Description, + group_ : string, + kind : Kind, + created : string, + link : string, + uid : string, + consDeleted : Array, + name : string, + prosDeleted : Array, + notes : Note, +}; + +export type Cat = { + title : string, + items : Array +}; diff --git a/front/src/utils/fetchData.js b/front/src/utils/fetchData.js new file mode 100644 index 0000000..0682cf7 --- /dev/null +++ b/front/src/utils/fetchData.js @@ -0,0 +1,25 @@ + +function checkStatus(response) { + if (response.status >= 200 && response.status < 300) { + return response; + } else { + throw new Error(response.statusText); + } +} + +function parseJSON(response) { + return response.json(); +} + +function fetchData(url : string) : Promise { + return fetch(url) + .then(checkStatus) + .then(parseJSON) + .catch(function(error) { + console.log('request failed', error); + }); + } + + module.exports = { + fetchData + } \ No newline at end of file diff --git a/front/src/utils/index.js b/front/src/utils/index.js new file mode 100644 index 0000000..3f51f05 --- /dev/null +++ b/front/src/utils/index.js @@ -0,0 +1,5 @@ +import { fetchData } from './fetchData'; + +module.exports = { + fetchData +} \ No newline at end of file