Last active
July 12, 2018 18:11
-
-
Save jermsam/a83b54d0d9f01bae4c2964803a440a7f to your computer and use it in GitHub Desktop.
How to integrate the RESTfull api with the react app
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
//add this to feather's src/channels.js around line 53 | |
app.service(‘products’) | |
.publish(‘created’, () => app.channel(‘anonymous’)); |
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
// src/CreateProductForm.js | |
import React, { Component } from 'react'; | |
import { Form, Button} from 'semantic-ui-react'; | |
import client from './feathers' | |
export default class CreateProductForm extends Component { | |
state = { | |
product:{ | |
name:'', | |
description:'', | |
price:null | |
} | |
}; | |
onChange = (e, { name:value }) => | |
this.setState((prevState)=>({ | |
...prevState.product, | |
[name]:value | |
})); | |
onSubmit =()=>{ //I am skipping validation | |
const{product}=this.state | |
client.service('products').create(product) | |
} | |
render() { | |
const { product:{name,description,price} } = this.state; | |
return ( | |
<Form onSubmit={this.onSubmit}> | |
<Form.Input | |
name='name' | |
value={name} | |
placeholder='Enter Product Name' | |
onChange={this.onChange} | |
/> | |
<Form.Text | |
name='description' | |
value={description} | |
placeholder='Enter Product Description' | |
onChange={this.onChange} | |
/> | |
<Form.Input | |
name='price' | |
value={price} | |
placeholder='Enter Product Price' | |
onChange={this.onChange} | |
/> | |
<Button type='submit'>Send</Button> | |
</Form> | |
); | |
} | |
} |
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,{Component} from 'react' | |
import { Card } from 'semantic-ui-react'; | |
import client from './feathers' | |
export default class DisplayGrid extends Component { | |
state = { | |
products;[] | |
}; | |
componentDidMount(){ | |
this.fetchFromRemote() | |
} | |
fetchFromRemote =()=>client.service('products').find({ | |
query: { | |
$sort: { | |
price: -1 // sort them by price descending | |
} | |
} | |
}).then( | |
res=>this.setState({products:res.data}) | |
) | |
render() { | |
const { products } = this.state; | |
return ( | |
<Card.Group> | |
{products.map( | |
product=> <Card> | |
<Card.Content> | |
<Card.Header> | |
{product.name} | |
</Card.Header> | |
<Card.Description> | |
{product.description} | |
</Card.Description> | |
<Card.Meta> | |
{product.price} | |
</Card.Meta> | |
</Card.Content> | |
</Card> | |
)} | |
</Card.Group> | |
); | |
} | |
} |
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
// src/DisplayGrid.js | |
import { Card } from 'semantic-ui-react'; | |
import client from './feathers' | |
export default class DisplayGrid extends Component { | |
state = { | |
products;[] | |
}; | |
componentDidMount(){ | |
this.fetchFromRemote() | |
client.service('products').on('created'()=>this.fetchFromRemote()); | |
} | |
fetchFromRemote =()=>client.service('products').find({ | |
query: { | |
$sort: { | |
price: -1 // sort them by price descending | |
} | |
} | |
}).then( | |
res=>this.setState({products:res.data}) | |
) | |
render() { | |
const { products } = this.state; | |
return ( | |
<Card.Group> | |
{products.map( | |
product=> <Card> | |
<Card.Content> | |
<Card.Header> | |
{product.name} | |
</Card.Header> | |
<Card.Description> | |
{product.description} | |
</Card.Description> | |
<Card.Meta> | |
{product.price} | |
</Card.Meta> | |
</Card.Content> | |
</Card> | |
)} | |
</Card.Group> | |
); | |
} | |
} |
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 io from 'socket.io-client' | |
import feathers from '@feathersjs/client' | |
// Socket.io is exposed as the `io` global. | |
const socket = io('http://localhost:3030') | |
// @feathersjs/client is exposed as the `feathers` global. | |
const client = feathers() | |
.configure(feathers.socketio(socket)) | |
//incase we later have to do authentication | |
.configure( | |
feathers.authentication({storage: window.localStorage}) | |
) | |
export default client |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment