I'm trying to create small app based on Json server package which will help me to remember movies I want to watch when I have free time, want to learn React and Axios so I'm doing it with these technologies , Idea is when I click on add movie button - movie will be added to Json database, when click on delete - particular movie will be deleted and when click on the list - I will be able to edit text,
Delete works if I do something like http://localhost:3000/movies/1, to show what id should it delete, but is there any way to set it? To delete the list connected to button I'm clicking at? something like http://localhost:3000/movies/"id"? I will be grateful for any help as I totally don't have any idea how to move on with it
import React from 'react'; import ReactDom from 'react-dom'; import axios from 'axios'; import List from "./list.jsx"; class Form extends React.Component { constructor(props) { super(props) this.state = { name:'', type:'', description:'', id:'', movies: [], } } handleChangeOne = e => { this.setState({ name:e.target.value }) } handleChangeTwo = e => { this.setState({ type:e.target.value }) } handleChangeThree = e => { this.setState({ description:e.target.value }) } handleSubmit = e => { e.preventDefault() const url = `http://localhost:3000/movies/`; axios.post(url, { name: this.state.name, type: this.state.type, description:this.state.description, id:this.state.id }) .then(res => { // console.log(res); // console.log(res.data); this.setState({ movies:[this.state.name,this.state.type,this.state.description, this.state.id] }) }) } handleRemove = (e) => { const id = this.state.id; const url = `http://localhost:3000/movies/`; // const id = document.querySelectorAll("li").props['data-id']; e.preventDefault(); axios.delete(url + id) .then(res => { console.log(res.data); }) .catch((err) => { console.log(err); }) } // editMovie = e => { // const url = `http://localhost:3000/movies/`; // e.preventDefault(); // const id = e.target.data("id"); // axios.put(url + id, { // name: this.state.name, // type: this.state.type, // description:this.state.description, // }) // .then(res => { // console.log(res.data); // }) // .catch((err) => { // console.log(err); // }) // } render() { return ( <form onSubmit={this.handleSubmit}> <input type="text" placeholder="movie" onChange={this.handleChangeOne}/> <input type="text" placeholder="type of movie" onChange={this.handleChangeTwo}/> <textarea cols={40} rows={5} placeholder="description of the movie" onChange={this.handleChangeThree}></textarea> <input type="submit" value="Add movie"></input> <List removeClick={this.handleRemove} editClick={this.editMovie}/> </form> ) } } export default Form List:
import React from 'react'; import ReactDom from 'react-dom'; import axios from 'axios'; class List extends React.Component{ constructor(props){ super(props) this.state = { movies: [], } } componentDidMount() { const url = `http://localhost:3000/movies`; console.log(url); axios.get(url) .then(res => { console.log(res.data); const movies = res.data; this.setState({ movies: movies }) }) .catch((err) => { console.log(err); }) } // editMovie =(e) => { // console.log("it works with edit!"); // if (typeof this.props.editClick === "function") { // this.props.editClick(e) // } else { // console.log("Doesn't work with edit"); // } // } removeMovie =(e) => { console.log("it works with remove!"); if (typeof this.props.removeClick === "function") { this.props.removeClick(e) } else { console.log("Doesn't work with remove"); } } render(){ let movies = this.state.movies.map(e => <ul onClick={this.editMovie}> <li data-id={e.id}> {e.name} </li> <li data-id={e.id}> {e.type} </li> <li data-id={e.id}> {e.description} </li> <button type="submit" onClick={this.removeMovie}>Delete</button> </ul>) return( <div> {movies} </div> ) } } export default List; Json part
{ "movies": [ { "id": 1, "name": "Kongi", "type": "drama", "description": "movie about monkey" }, { "id": 2, "name": "Silent Hill", "type": "thriller", "description": "movie about monsters" }, { "name": "Harry potter", "type": "fantasy", "description": "movie about magic and glory", "id": 3 } ] }
onClicktoonClick={event => this.removeMovie(event, e)}and then inremoveMoviecall the prop function with the movie id instead of the event and use that.<button type="submit" onClick={event => this.removeMovie(event, e)}>Delete</button>and thisremoveMovie =(event,e) => { if (typeof this.props.removeClick === "function") { this.props.removeClick(e.id) }but it seems like it delete states name, type and description but still keep this in data base, like this{ "name": "", "type": "", "description": "", "id": 3 }