1
1
mirror of https://github.com/aelve/guide.git synced 2024-12-23 04:42:24 +03:00

[GD-11] add a component for displaying categories data

This commit is contained in:
Juan Bono 2017-05-04 04:51:49 -03:00
parent 26cf0f4cfd
commit 52fc6624ad
10 changed files with 128 additions and 44 deletions

View File

@ -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 (
<div>
<h1>Not Found</h1>
<p><Link to="/">Go home</Link></p>
</div>
)
}
}
import { CategoryChild, Home, NoMatch } from './components/index';
import { BrowserRouter as Router, Route, Switch } from 'react-router-dom';
class App extends Component {
render() {

View File

@ -0,0 +1,5 @@
import React, { Component } from 'react';
class Category extends Component {
}

View File

@ -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 (
<h1>hi</h1>
)
return (
<h1>{ this.state.cat.title }</h1>)
}
}

View File

@ -10,8 +10,8 @@ class GrandCategory extends Component {
render() {
const renderBigCatLink = cat => {
return(
<span>
<a key={cat.uid} href={cat.link}>{cat.title}</a>
<span key={cat.uid}>
<a href={cat.link}>{cat.title}</a>
<style jsx>{`
a {
display: block;
@ -25,8 +25,8 @@ class GrandCategory extends Component {
const renderSmallCatLink = cat => {
return(
<span>
<a key={cat.uid} href={cat.link}>{cat.title}</a>
<span key={cat.uid}>
<a href={cat.link}>{cat.title}</a>
<style jsx>{`
a {white-space: nowrap;}
`}</style>
@ -37,7 +37,7 @@ class GrandCategory extends Component {
const grand = this.props.val;
return(
<div className="GrandCategory" key={grand.title}>
<div className="GrandCategory">
<h1>{grand.title}</h1>
<div className="finished">

View File

@ -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 (
<Tiles space="1em">
{this.state.categories.map(grand => {
return <GrandCategory val={grand} />;
return <GrandCategory key={grand.title} val={grand} />;
})
}
</Tiles>

View File

@ -0,0 +1,17 @@
import React, { Component } from 'react';
import { Link } from 'react-router-dom';
class NoMatch extends Component {
render() {
return (
<div>
<h1>Not Found</h1>
<p><Link to="/">Go home</Link></p>
</div>
)
}
}
module.exports = {
NoMatch
}

View File

@ -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
}

View File

@ -3,11 +3,50 @@ export type Category = {
uid : string,
link : string,
title : string
};
};
export type GrandCategoryT = {
title : string,
finished : Array<Category>,
wip : Array<Category>,
stubs : Array<Category>
}
};
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<Valoration>,
cons : Array<Valoration>
};
export type Item = {
ecosystem : Description,
group_ : string,
kind : Kind,
created : string,
link : string,
uid : string,
consDeleted : Array<String>,
name : string,
prosDeleted : Array<String>,
notes : Note,
};
export type Cat = {
title : string,
items : Array<Item>
};

View File

@ -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<any> {
return fetch(url)
.then(checkStatus)
.then(parseJSON)
.catch(function(error) {
console.log('request failed', error);
});
}
module.exports = {
fetchData
}

5
front/src/utils/index.js Normal file
View File

@ -0,0 +1,5 @@
import { fetchData } from './fetchData';
module.exports = {
fetchData
}