Skip to content

Commit

Permalink
working
Browse files Browse the repository at this point in the history
  • Loading branch information
redmannequin committed Apr 13, 2019
1 parent e0e330d commit c99921d
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 59 deletions.
87 changes: 29 additions & 58 deletions client/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,60 +41,12 @@ class App extends Component<any, State> {
this.state = {
tags: [],
suggestions: [],
recipes: [
{
title: 'Test',
key: 'Test',
directions: ['Test','Test'],
ingredients: ['Test', 'Test'],
tags: [
{ name: 'Test'},
{ name: 'Test'}
],
categories: ['Test', 'Test'],
calories: 100,
protein: 100,
fat: 100,
sodium: 100,
rating: 100,
date: new Date(),
},{
title: 'Test',
key: 'Test',
directions: ['Test','Test'],
ingredients: ['Test', 'Test'],
tags: [
{ name: 'Test'},
{ name: 'Test'}
],
categories: ['Test', 'Test'],
calories: 100,
protein: 100,
fat: 100,
sodium: 100,
rating: 100,
date: new Date(),
},{
title: 'Test',
key: 'Test',
directions: ['Test','Test'],
ingredients: ['Test', 'Test'],
tags: [
{ name: 'Test'},
{ name: 'Test'}
],
categories: ['Test', 'Test'],
calories: 100,
protein: 100,
fat: 100,
sodium: 100,
rating: 100,
date: new Date(),
}
]
recipes: []
}

this.handleDelete = this.handleDelete.bind(this);
this.handleAddition = this.handleAddition.bind(this);
this.updateRecipes = this.updateRecipes.bind(this);
}

componentDidMount() {
Expand All @@ -109,8 +61,6 @@ class App extends Component<any, State> {
})
.catch()
}



render() {
const {tags, suggestions, recipes} = this.state;
Expand Down Expand Up @@ -139,7 +89,7 @@ class App extends Component<any, State> {

<br/>

<div className='row'>
<div className='rrs row'>
{
recipes.map( (recipe: IRecipe, idx:number) => <RecipeCard data={recipe} key={idx}/>)
}
Expand All @@ -155,18 +105,39 @@ class App extends Component<any, State> {
);
}

private updateRecipes(state: State) {
const tags = state.tags;
if (tags.length == 0 ) {
this.setState(state);
return;
}
const url = 'http://localhost:5000/recipe?' + tags.map( elm => `tag=${elm.name}`).join('&')
console.log(url)
axios
.get(url)
.then( res => {
console.log(res.data)
state.recipes = res.data;
this.setState(state)
})
.catch()
}

private handleDelete(i:number) {
const state = { ...this.state };
const state: State = { ...this.state };
const tags = state.tags.slice(0);
tags.splice(i, 1);
this.setState({ tags });
state.tags = tags;
this.updateRecipes(state);
}

private handleAddition(tag: { id: string | number; name: string }) {
const state = { ...this.state };
const state: State = { ...this.state };
const tags = state.tags.concat(tag);
this.setState({ tags });
state.tags = tags;
this.updateRecipes(state)
}

}

const RecipeCard:StatelessComponent<any> = (props: any) => {
Expand Down
2 changes: 1 addition & 1 deletion server/src/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ async function populateDatabase() {
const app = express();
const port = 5000;

app.use(function(req, res, next) {
app.use(function(_, res, next) {
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
next();
Expand Down

0 comments on commit c99921d

Please sign in to comment.