Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
7955125-front-end/src/components/favourite.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
69 lines (61 sloc)
1.69 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React from 'react'; | |
import { Col, Row } from 'antd'; | |
import DogCard from './dogcard'; | |
import { status, json } from '../utilities/requestHandlers'; | |
import UserContext from '../contexts/user'; | |
class Favourite extends React.Component { | |
constructor(props) { | |
super(props); | |
this.state = { | |
posts: [], | |
favourites:[] | |
} | |
} | |
static contextType = UserContext; | |
componentDidMount() { | |
fetch('https://melody-annex-3000.codio-box.uk/api/v1/dogs') | |
.then(status) | |
.then(json) | |
.then(data=>{ | |
console.log(data) | |
this.setState({posts:data}) | |
}) | |
.catch(err => console.log("Error fetching dogs", err)) | |
let headers = new Headers(); | |
headers.append('Authorization', this.context.user.Authorization); | |
fetch(`https://melody-annex-3000.codio-box.uk/api/v1/dogs/favourite`, { | |
headers:headers | |
}) | |
.then(status) | |
.then(json) | |
.then(data=>{ | |
console.log(data) | |
this.setState({favourites:data}) | |
}) | |
.catch(err => console.log("Error fetching dogs", err)) | |
} | |
render() { | |
if (!this.state.posts.length||!this.state.favourites.length) { | |
return <h3>Loading posts...</h3> | |
} | |
console.log(this.state.favourites) | |
let favouriteIDS=this.state.favourites.map(favourite=>{ | |
return favourite.dogID | |
}) | |
const cardList = this.state.posts.filter(dog=>favouriteIDS.includes(dog.ID)).map(post => { | |
return ( | |
<div style={{padding:"10px"}} key={post.ID}> | |
<Col span={6}> | |
<DogCard {...post} /> | |
</Col> | |
</div> | |
) | |
}); | |
return ( | |
<Row type="flex" justify="space-around"> | |
{cardList} | |
</Row> | |
); | |
} | |
} | |
export default Favourite; |