Skip to content

Dilruba M. Crud lab #43

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions src/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,20 @@ import { BrowserRouter as Router, Routes, Route } from "react-router-dom";
import Footer from "./components/common/Footer";
import Home from "./components/home/Home";
import Nav from "./components/common/Nav";

// shows

import Show from "./components/shows/Show";
import ShowsEditForm from "./components/shows/ShowsEditForm";
import ShowsIndex from "./components/shows/ShowsIndex";
import ShowsNewForm from "./components/shows/ShowsNewForm";

// movies
import Movie from "./components/movies/Movie";
import MoviesEditForm from "./components/movies/MoviesEditForm";
import MoviesIndex from "./components/movies/MoviesIndex";
import MoviesNewForm from "./components/movies/MoviesNewForm"

function App() {
return (
<div className="wrapper">
Expand All @@ -20,6 +29,11 @@ function App() {
<Route path="/shows/new" element={<ShowsNewForm />} />
<Route path="/shows/:id" element={<Show />} />
<Route path="/shows/:id/edit" element={<ShowsEditForm />} />
<Route path="/movies" element={<MoviesIndex />} />
<Route path="/movies/new" element={<MoviesNewForm />}/>
<Route path="/movies/:id" element={<Movie />} />
<Route path="/movies/:id/edit" element={<MoviesEditForm />} />

</Routes>
<Footer />
</Router>
Expand Down
62 changes: 56 additions & 6 deletions src/api/fetch.js
Original file line number Diff line number Diff line change
@@ -1,32 +1,82 @@
// Shows
const URL = process.env.REACT_APP_API_BASE_URL;

// Create
export function createShow(show) {
return;
const options ={
method: "POST",
body: JSON.stringify(show),
headers: {"Content-type": "application/json"},
};
return fetch (`${URL}/shows/`, options).then(res => res.json());
}

// Delete
export function destroyShow(id) {
return;
const options = { method: "DELETE" };
return fetch(`${URL}/shows/${id}`, options);
}

// Index/Get all
export function getAllShows() {
return;
return fetch(`${URL}/shows`).then((res) => res.json());
}

// Show/Get one
export function getOneShow(id) {
return;
return fetch(`${URL}/shows/${id}`).then((res) => res.json());
}

// Update
export function updateShow(id, show) {
return;
const options = {
method: "PUT",
body: JSON.stringify(show),
headers: { "Content-Type": "application/json" },
};
return fetch(`${URL}/shows/${id}`, options).then((res) => res.json());
}


// Movies


// Index/Get all
export function getAllMovies() {
return;
return fetch(`${URL}/movies`).then((res) => res.json());
;
}

// Create

export function createMovie(movie) {
const options = {
method: "POST",
body: JSON.stringify(movie),
headers: {"Content-Type": "application/json"}
};
return fetch (`${URL}/movies/`, options).then((res)=> res.json())
}

// Delete
export function destroyMovie(id) {
const options = { method: "DELETE" };
return fetch(`${URL}/movies/${id}`, options);
}


// Show/Get one
export function getOneMovie(id) {
return fetch(`${URL}/movies/${id}`).then((res) => res.json());
}


// Update
export function updateMovie(id, movie) {
const options = {
method: "PUT",
body: JSON.stringify(movie),
headers: { "Content-Type": "application/json" },
};
return fetch(`${URL}/movies/${id}`, options).then((res) => res.json());
}
47 changes: 47 additions & 0 deletions src/components/movies/Movie.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
.movies-movie-wrapper {
margin: 0 auto;
width: 70%;
}

.movies-movie {
border: 1px solid var(--primary);
border-radius: 3px;
color: palevioletred;
display: grid;
grid-template-rows: auto 1fr auto;
}

.movies-movie article {
padding: 10px 25px;
}

.movies-movie aside {
background: var(--primary);
color: pink;
}

.movies-movie aside p {
display: inline-block;
font-size: 14px;
margin: 10px 25px;
padding: 0;
width: auto;
}

.movies-movie aside span {
font-weight: bold;
}

.movies-movie aside button {
background: none;
border: none;
color: rgb(240, 191, 240);
cursor: pointer;
margin: 10px 25px;
text-decoration: underline;
}

.movies-movie aside button:hover {
text-decoration: none;
}

81 changes: 81 additions & 0 deletions src/components/movies/Movie.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import { useState, useEffect } from "react";
import { Link, useParams, useNavigate } from "react-router-dom";

import { destroyMovie, getOneMovie } from "../../api/fetch";

import "./Movie.css";

import ErrorMessage from "../errors/ErrorMessage";

function Movie() {
const [movie, setMovie] = useState({});
const [loadingError, setLoadingError] = useState(false);

const { id } = useParams();
const navigate = useNavigate()

function handleDelete() {
destroyMovie(id)
.then(() => navigate("/movies"))
.catch((error) => {
console.log(error);
setLoadingError(true)
})
}

useEffect(() => {
getOneMovie(id)
.then((res) => {
setMovie(res);
if (Object.keys(res).length === 0) setLoadingError(true);
else setLoadingError(false);
})
.catch((error) => {
setLoadingError(true);
});
}, [id]);

return (
<section className="movies-movie-wrapper">
<h2>{movie.title}</h2>
<section className="movies-movie">
{loadingError ? (
<ErrorMessage />
) : (
<>
<aside>
<p>
<span>Duration:</span> {movie.duration}
</p>
<p>
<span>Listed Categories:</span> {movie.listedIn}
</p>
<p>
<span>Country:</span> {movie.country}
</p>
<p>
<span>Rating:</span> {movie.rating}
</p>
<p>
<span>Date Added:</span> {movie.dateAdded}
</p>
</aside>
<article>
<p>{movie.description}</p>
</article>
<aside>
<button className="delete" onClick={() => handleDelete(movie.id)}>
Remove show
</button>
<Link to={`/movies/${id}/edit`}>
<button>Edit</button>
</Link>
</aside>
</>
)}
</section>
</section>
);
}

export default Movie;
38 changes: 38 additions & 0 deletions src/components/movies/MovieListing.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
.movie {
border: 1px solid var(--primary);
border-radius: 3px;
color: palevioletred;
display: grid;
grid-template-rows: auto 1fr auto;
padding: 10px 25px;
}

.movie a {
border-bottom: 1px solid var(--primary);
color: rgb(255, 178, 106);
text-decoration: none;
}

.movie .title {
text-decoration: none;
font-variant: small-caps;
margin-bottom: 0;
}

.movie .description {
font-size: 14px;
}

.movie .details {
border-top: 1px solid var(--primary);
padding-top: 10px;
}

.movie .details p {
font-size: 12px;
margin: 0 0 5px;
}

.movie .details span {
font-weight: bold;
}
23 changes: 23 additions & 0 deletions src/components/movies/MovieListing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {Link} from 'react-router-dom'
import "./MovieListing.css";

export default function MovieListing({ movie }) {
return (
<article className="movie">
<h3 className="title">
<Link to={`/movies/${movie.id}`}>{movie.title}</Link>
</h3>
<p className="description">{movie.description}</p>
<aside className="details">
<p>
<span>Listed Categories:</span>
{movie.listedIn}
</p>
<p>
<span>Duration:</span> {movie.duration}
</p>
</aside>
</article>
);
}

Loading