Skip to content
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

Glasgow 6 | Haroun Alarabi | Full-Stack-Project-Assessment | Level 300 #393

Closed
wants to merge 155 commits into from
Closed
Show file tree
Hide file tree
Changes from 15 commits
Commits
Show all changes
155 commits
Select commit Hold shift + click to select a range
69edd6a
level 100
HarounAlarabi Aug 15, 2023
1bf955f
updated 199
HarounAlarabi Aug 21, 2023
8e17e0a
level 300
HarounAlarabi Sep 3, 2023
c6a5a26
installed pg
HarounAlarabi Sep 9, 2023
31714ca
remove node -module
HarounAlarabi Sep 9, 2023
11db4a6
add git ignore
HarounAlarabi Sep 9, 2023
f430199
update server
HarounAlarabi Sep 12, 2023
5a65a94
fixed server
HarounAlarabi Sep 15, 2023
f1719dd
update fetch adresses
HarounAlarabi Sep 15, 2023
9b6119d
update fetch adresses
HarounAlarabi Sep 15, 2023
c9b4d77
Create settings.json
HarounAlarabi Sep 15, 2023
2a5c39b
update fetch adresses
HarounAlarabi Sep 15, 2023
e03cf78
update fetch adresses
HarounAlarabi Sep 16, 2023
693fca6
updated delete function
HarounAlarabi Sep 16, 2023
617033c
updated data base link
HarounAlarabi Sep 16, 2023
bf67c03
update date base
HarounAlarabi Jan 26, 2024
47555fc
added aws server and database
HarounAlarabi Jan 31, 2024
e9264f8
remove build
HarounAlarabi Jan 31, 2024
4c0da5a
Update Video.jsx
HarounAlarabi Feb 1, 2024
9f9b409
Update Video.jsx
HarounAlarabi Feb 1, 2024
1520c40
Update package.json
HarounAlarabi Feb 2, 2024
f2cda8e
Create frontend-s3-deploy.yml
HarounAlarabi Feb 2, 2024
aef98ae
Update frontend-s3-deploy.yml
HarounAlarabi Feb 2, 2024
320d332
Update frontend-s3-deploy.yml
HarounAlarabi Feb 2, 2024
b57ec28
Update frontend-s3-deploy.yml
HarounAlarabi Feb 2, 2024
7430cc7
Update frontend-s3-deploy.yml
HarounAlarabi Feb 2, 2024
71e8c2d
Update frontend-s3-deploy.yml
HarounAlarabi Feb 2, 2024
6f3ee8d
Update frontend-s3-deploy.yml
HarounAlarabi Feb 2, 2024
19830d9
Update frontend-s3-deploy.yml
HarounAlarabi Feb 2, 2024
30815d8
Update frontend-s3-deploy.yml
HarounAlarabi Feb 2, 2024
115139e
Update frontend-s3-deploy.yml
HarounAlarabi Feb 2, 2024
e19b396
Update frontend-s3-deploy.yml
HarounAlarabi Feb 2, 2024
901b741
Update frontend-s3-deploy.yml
HarounAlarabi Feb 2, 2024
e71d384
Update frontend-s3-deploy.yml
HarounAlarabi Feb 2, 2024
c92bab6
Update frontend-s3-deploy.yml
HarounAlarabi Feb 2, 2024
35cd4f4
Update frontend-s3-deploy.yml
HarounAlarabi Feb 2, 2024
a5f68f3
Update frontend-s3-deploy.yml
HarounAlarabi Feb 2, 2024
23bad34
Update frontend-s3-deploy.yml
HarounAlarabi Feb 2, 2024
16f6fd4
Update frontend-s3-deploy.yml
HarounAlarabi Feb 2, 2024
477fbe4
Update frontend-s3-deploy.yml
HarounAlarabi Feb 2, 2024
804270b
Update frontend-s3-deploy.yml
HarounAlarabi Feb 2, 2024
afd5a23
Update frontend-s3-deploy.yml
HarounAlarabi Feb 2, 2024
13b37d3
Update frontend-s3-deploy.yml
HarounAlarabi Feb 3, 2024
6671127
Update frontend-s3-deploy.yml
HarounAlarabi Feb 3, 2024
5e62bfd
Update frontend-s3-deploy.yml
HarounAlarabi Feb 3, 2024
7300e2c
Update frontend-s3-deploy.yml
HarounAlarabi Feb 3, 2024
f12b030
Delete client/build directory
HarounAlarabi Feb 3, 2024
43fe4f2
Update frontend-s3-deploy.yml
HarounAlarabi Feb 3, 2024
a906534
Update frontend-s3-deploy.yml
HarounAlarabi Feb 3, 2024
b306cfc
Update frontend-s3-deploy.yml
HarounAlarabi Feb 3, 2024
0eedb67
Update frontend-s3-deploy.yml
HarounAlarabi Feb 3, 2024
579aa03
Update frontend-s3-deploy.yml
HarounAlarabi Feb 3, 2024
a1e1880
Update frontend-s3-deploy.yml
HarounAlarabi Feb 3, 2024
8732f6e
Update frontend-s3-deploy.yml
HarounAlarabi Feb 3, 2024
70a7999
Update frontend-s3-deploy.yml
HarounAlarabi Feb 3, 2024
261e797
Create backend-ec2-deploy.yml
HarounAlarabi Feb 3, 2024
8b5ce3c
Update backend-ec2-deploy.yml
HarounAlarabi Feb 3, 2024
92858f6
Update backend-ec2-deploy.yml
HarounAlarabi Feb 3, 2024
32dcfc6
Update backend-ec2-deploy.yml
HarounAlarabi Feb 3, 2024
f0ff110
Delete .github/workflows/backend-ec2-deploy.yml
HarounAlarabi Feb 3, 2024
b83eb83
Create backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
76fa5e8
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
4dcd2c2
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
ce1b92c
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
fb135aa
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
95fc825
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
ab85fab
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
3fd740f
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
5ad87f3
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
d690b8a
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
43d9965
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
d650d30
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
a6f048b
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
794f1bb
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
a4a53f2
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
e6b4807
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
c0f1f20
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
67795d1
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
578418f
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
e60997a
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
893e0f2
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
6bbea86
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
24a4ce8
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
f23cf54
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
1ea8d8e
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
e3ebf78
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
72307fb
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
8127274
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
23587e9
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
ba66db8
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
3b1894c
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
fd789d8
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
5d1268a
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
dda8446
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
94e0bd6
Update backend-ec2-deploy.yml
HarounAlarabi Feb 4, 2024
8056f8f
Update backend-ec2-deploy.yml
HarounAlarabi Feb 5, 2024
bf916fa
Update backend-ec2-deploy.yml
HarounAlarabi Feb 5, 2024
4e5f556
Update backend-ec2-deploy.yml
HarounAlarabi Feb 5, 2024
3f53887
Update backend-ec2-deploy.yml
HarounAlarabi Feb 5, 2024
b731eae
Update backend-ec2-deploy.yml
HarounAlarabi Feb 6, 2024
6508f1e
Update backend-ec2-deploy.yml
HarounAlarabi Feb 6, 2024
838535c
Update backend-ec2-deploy.yml
HarounAlarabi Feb 6, 2024
8ad4d17
Update backend-ec2-deploy.yml
HarounAlarabi Feb 6, 2024
16c0651
Update backend-ec2-deploy.yml
HarounAlarabi Feb 6, 2024
2e99ba6
Update backend-ec2-deploy.yml
HarounAlarabi Feb 6, 2024
617bdf5
Update backend-ec2-deploy.yml
HarounAlarabi Feb 6, 2024
d8f9024
Update backend-ec2-deploy.yml
HarounAlarabi Feb 6, 2024
6f128f4
Update backend-ec2-deploy.yml
HarounAlarabi Feb 6, 2024
f2607a3
Update backend-ec2-deploy.yml
HarounAlarabi Feb 6, 2024
930139f
Update backend-ec2-deploy.yml
HarounAlarabi Feb 6, 2024
5ee7015
Update backend-ec2-deploy.yml
HarounAlarabi Feb 6, 2024
f750ce6
Update backend-ec2-deploy.yml
HarounAlarabi Feb 6, 2024
f2cfe23
Update backend-ec2-deploy.yml
HarounAlarabi Feb 6, 2024
6c7a8b7
Update backend-ec2-deploy.yml
HarounAlarabi Feb 6, 2024
e203619
Update backend-ec2-deploy.yml
HarounAlarabi Feb 7, 2024
ea626fb
Update backend-ec2-deploy.yml
HarounAlarabi Feb 7, 2024
6aa2965
Update backend-ec2-deploy.yml
HarounAlarabi Feb 7, 2024
6eebe36
Update backend-ec2-deploy.yml
HarounAlarabi Feb 7, 2024
40ec1c9
Update backend-ec2-deploy.yml
HarounAlarabi Feb 7, 2024
6de5644
Update backend-ec2-deploy.yml
HarounAlarabi Feb 7, 2024
c1c4962
Update backend-ec2-deploy.yml
HarounAlarabi Feb 7, 2024
aab3a4f
Update backend-ec2-deploy.yml
HarounAlarabi Feb 7, 2024
48e3a88
Update backend-ec2-deploy.yml
HarounAlarabi Feb 7, 2024
c7b1afc
Update VideosList.jsx
HarounAlarabi Feb 7, 2024
8205303
Update Video.jsx
HarounAlarabi Feb 7, 2024
283d8cc
Update index.scss
HarounAlarabi Feb 7, 2024
ba46007
Update backend-ec2-deploy.yml
HarounAlarabi Feb 13, 2024
1f540b3
Update backend-ec2-deploy.yml
HarounAlarabi Feb 13, 2024
54fb352
Update backend-ec2-deploy.yml
HarounAlarabi Feb 13, 2024
1c5bd0f
Update backend-ec2-deploy.yml
HarounAlarabi Feb 13, 2024
45b4590
Update backend-ec2-deploy.yml
HarounAlarabi Feb 13, 2024
d2297a6
Update backend-ec2-deploy.yml
HarounAlarabi Feb 13, 2024
add2bb1
Update backend-ec2-deploy.yml
HarounAlarabi Feb 13, 2024
3cd0fa9
Update backend-ec2-deploy.yml
HarounAlarabi Feb 13, 2024
11347fc
Update backend-ec2-deploy.yml
HarounAlarabi Feb 13, 2024
8df7ad8
Update backend-ec2-deploy.yml
HarounAlarabi Feb 14, 2024
abca632
Update backend-ec2-deploy.yml
HarounAlarabi Feb 14, 2024
41ad17d
Update backend-ec2-deploy.yml
HarounAlarabi Feb 14, 2024
25f60e4
Update backend-ec2-deploy.yml
HarounAlarabi Feb 14, 2024
7bfd385
Update backend-ec2-deploy.yml
HarounAlarabi Feb 14, 2024
a869b0d
added docker file
HarounAlarabi Feb 14, 2024
41a2720
Merge branch 'Level--300' of https://github.com/HarounAlarabi/Full-St…
HarounAlarabi Feb 14, 2024
533abea
added docker file
HarounAlarabi Feb 14, 2024
57546ff
Update backend-ec2-deploy.yml
HarounAlarabi Feb 14, 2024
0dfdb0d
Update backend-ec2-deploy.yml
HarounAlarabi Feb 14, 2024
0f21bb4
Update backend-ec2-deploy.yml
HarounAlarabi Feb 14, 2024
f5e4b79
updated fetch api
HarounAlarabi Feb 14, 2024
595712c
Merge branch 'Level--300' of https://github.com/HarounAlarabi/Full-St…
HarounAlarabi Feb 14, 2024
e25ba7d
added terraform config
HarounAlarabi Feb 24, 2024
49617c4
Update backend-ec2-deploy.yml
HarounAlarabi Mar 1, 2024
11a7fb5
Update backend-ec2-deploy.yml
HarounAlarabi Mar 2, 2024
8daa84c
Update VideosList.jsx
HarounAlarabi Mar 9, 2024
0547896
Update Video.jsx
HarounAlarabi Mar 9, 2024
94b8243
Update VideosList.jsx
HarounAlarabi Mar 9, 2024
ee88fa6
Update Video.jsx
HarounAlarabi Mar 9, 2024
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
3 changes: 3 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"git.ignoreLimitWarning": true
}
1 change: 1 addition & 0 deletions client/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
node_modules/
1,341 changes: 1,121 additions & 220 deletions client/package-lock.json

Large diffs are not rendered by default.

10 changes: 9 additions & 1 deletion client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,19 @@
"version": "0.1.0",
"private": true,
"dependencies": {
"@fortawesome/fontawesome-svg-core": "^6.4.2",
"@fortawesome/free-regular-svg-icons": "^6.4.2",
"@fortawesome/free-solid-svg-icons": "^6.4.2",
"@fortawesome/react-fontawesome": "^0.2.0",
"@testing-library/jest-dom": "^5.11.4",
"@testing-library/react": "^11.2.7",
"@testing-library/user-event": "^12.1.10",
"bootstrap": "^5.1.3",
"bootstrap": "^5.3.1",
"cors": "^2.8.5",
"express": "^4.18.2",
"pg": "^8.11.3",
"react": "^17.0.2",
"react-bootstrap": "^2.8.0",
"react-dom": "^17.0.2",
"react-scripts": "^5.0.0",
"web-vitals": "^1.1.2"
Expand Down
15 changes: 13 additions & 2 deletions client/public/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,25 @@
content="Web site created using create-react-app"
/>
<link rel="apple-touch-icon" href="%PUBLIC_URL%/logo192.png" />
<!---<link href="https://cdn.jsdelivr.net/npm/bootstrap/dist/css/bootstrap.min.css" rel="stylesheet">--->
<!-- Bootstrap CSS -->
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous"
/>

<script
src="https://kit.fontawesome.com/97841cab25.js"
crossorigin="anonymous"
></script>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<link
rel="stylesheet"
href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm"
crossorigin="anonymous"
/>
<!--
manifest.json provides metadata used when your web app is installed on a
user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/
Expand All @@ -32,7 +43,7 @@
work correctly both with client-side routing and a non-root public URL.
Learn how to configure a non-root public URL by running `npm run build`.
-->
<title>React App</title>
<title>video play</title>
</head>
<body>
<noscript>You need to enable JavaScript to run this app.</noscript>
Expand Down
4 changes: 4 additions & 0 deletions client/src/App.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import "./App.css";

import VideosList from "./components/VideosList";

function App() {
return (
<div className="App">
<header className="App-header">
<h1>Video Recommendation</h1>
</header>

<VideosList />
</div>
);
}
Expand Down
79 changes: 79 additions & 0 deletions client/src/components/AddVideo.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import React, { useState } from "react";
import "../index.css";

const AddVideo = ({ onVidAdd }) => {
const [title, setTitle] = useState("");
const [url, setUrl] = useState("");

const titleChange = (event) => {
setTitle(event.target.value);
};

const urlChange = (event) => {
setUrl(event.target.value);
};

const newVid = () => {
if (title !== "" && url !== "") {
let embedUrl = url.replace("https://youtu.be/", "https://www.youtube.com/embed/");
console.log("Original URL:", url);
console.log("Embed URL:", embedUrl);

const newInput = {
title: title,
url: embedUrl,
rating: 0,
};

onVidAdd(newInput);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unclear to me what this function does just by reading its name onVidAdd


setTitle("");
setUrl("");
} else {
alert("Please fill in all fields");
}
};

return (
<form>
<div className="form-group row">
<label htmlFor="colFormLabelSm" className="col-sm-2 col-form-label col-form-label-sm">
Title
</label>
<div className="col-sm-10">
<input
type="text"
value={title}
onChange={titleChange}
className="form-control form-control-sm"
id="colFormLabelSm"
placeholder="Title"
/>
</div>
</div>
<div className="form-group row">
<label htmlFor="colFormLabelSm" className="col-sm-2 col-form-label col-form-label-sm">
URL
</label>
<div className="col-sm-10">
<input
type="text"
value={url}
onChange={urlChange}
className="form-control form-control-sm"
id="colFormLabelSm"
placeholder="URL"
/>
</div>
</div>
<button onClick={newVid} type="button" className="btn btn-success">
Add
</button>
<button type="button" className="btn btn-warning">
Cancel
</button>
</form>
);
};

export default AddVideo;
21 changes: 21 additions & 0 deletions client/src/components/Search.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { useState } from "react";

function Search({ onSearch }) {
const [search, setSearch] = useState();
const handleSearch = () => {
if (search) {
onSearch(search);
} else {
alert("please enter a search term");
}
};

return (
<div>
<input onChange={(e) => setSearch(e.target.value)}></input>
<button onClick={handleSearch}>Search</button>
</div>
);
}

export default Search;
71 changes: 71 additions & 0 deletions client/src/components/Video.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
import { useState } from "react";

function Video({ videoObj, deleteVideo }) {
const videoId = getVideoIdFromUrl(videoObj.url);
const [vote, setVote] = useState(videoObj.rating);



const updateVote = (increment) => {
const newRating = videoObj.rating + increment;

fetch(`https://node-server-full-stack.onrender.com/videos/${videoObj.id}`, {
method: "PUT",
headers: {
"Content-Type": "application/json",
},
body: JSON.stringify({ newRating }),
})
.then((res) => {
if (!res.ok) throw new Error(res.statusText);
return res.json();
})
.then(() => {
if (increment === 1) {
setVote(vote + 1);
} else if (increment === -1 && vote > 0) {
setVote(vote - 1);
}
})
.catch((error) => console.error("Error updating vote:", error));
};




const date = new Date().toDateString();

return (
<div className="card">
<iframe
src={`https://www.youtube.com/embed/${videoId}`}
frameBorder="0"
allow="accelerometer; autoplay; encrypted-media; gyroscope; picture-in-picture"
title="YouTube video player"
allowFullScreen
height="300px"
></iframe>
<div className="card-body">
<h5 className="card-title">{videoObj.title}</h5>
<p className="card-text">Requested At: {date}</p>
<div className="buttons">
<i className="fa-solid fa-thumbs-up" onClick={()=>updateVote(1)}></i>
<p>{vote}</p>
<i className="fa-solid fa-thumbs-down" onClick={()=>updateVote(-1)}></i>
<button onClick={() => deleteVideo(videoObj.id)} className="btn btn-danger">
<i className="fa fa-trash-o"></i>
</button>
</div>
</div>
</div>
);
}

export default Video;

function getVideoIdFromUrl(url) {
if (url && typeof url === "string" && url.includes("v=")) {
return url.split("v=")[1];
}
return null;
}
105 changes: 105 additions & 0 deletions client/src/components/VideosList.jsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
import React, { useEffect, useState } from "react";
import Video from "./Video";
import AddVideo from "./AddVideo";
import "../index.css";
import Search from "./Search";

function VideosList() {
const [videos, setVideos] = useState([]);
const [loading, setLoading] = useState(true);
const [searchResult, setSearchResult] = useState([]);

useEffect(() => {
fetch("https://node-server-full-stack.onrender.com/videos")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use an environment variable for this API url - don't just hardcode the string. Do you understand why this is important?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chaned this

.then((res) => res.json())
.then((data) => {
console.log(data);
setVideos(data);
setLoading(false);
})
.catch((error) => {
console.error("Error fetching data:", error);
});
}, []);

const newVideo = (newInput) => {
newInput.id = videos.length + 1;
newInput.rating = 0;
fetch("https://node-server-full-stack.onrender.com/videos", {
method: "POST",
headers: {
"Content-Type": "application/JSON",
},
body: JSON.stringify(newInput),
})
.then((res) => {
fetchVideos();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Call to fetchVideos here - describe the effect of calling this function here

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fetchVideos to update the list of videos after adding new video

return res.json();
})
.then((data) => {
if (data && data.message === "Video stored successfully") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now you're calling setVideos down here as well - I think you need to think very carefully about what happens when after you've POSTed a new video to your database

setVideos([...videos, newInput]);
}
})
.catch((error) => {
console.error("Error:", error);
alert("Something went wrong");
});
};

const deleteVideoItem = (videoId) => {
fetch(`https://node-server-full-stack.onrender.com/videos/${videoId}`, {
method: "DELETE",
headers: {
"Content-Type": "application/JSON",
},
}).then((res) => {
if (!res.ok) {
throw new Error('Network response was not ok');
}
const videoDeleted = videos.filter((el) => el.id !== videoId);
setVideos(videoDeleted);
})
};

const fetchVideos = () => {
fetch("https://node-server-full-stack.onrender.com/videos")
.then((res) => res.json())
.then((data) => {
setVideos(data);
searchResult(data);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here you're fetching the data and then you

  1. call res.json()
  2. call setVideos with the data
  3. and then call searchResult -> can you explain the issue here?

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the issue here is I used searchResult , so I have to change in to setSearchResult()

})
.catch((error) => {
console.error("Error fetching data:", error);
});
};
const searchByName = (search) => {
fetch(`https://node-server-full-stack.onrender.com/videos/${search}`, {
method: "GET",
headers: { "content-type": "application/JSON" },
})
.then((res) => res.json())
.then((data) => {
setSearchResult(data);
console.log(data)
});
};
const sortedVideos = [...videos].sort((a, b) => b.rating - a.rating);
return (
<>
<Search onSearch={searchByName} />
<AddVideo onVidAdd={newVideo} />
<div className="container mt-5">
{searchResult.length > 0 ? (
searchResult.map((video) => <Video videoObj={video} key={video.id} deleteVideo={deleteVideoItem} />)
) : loading ? (
<p>Loading...</p>
) : (
sortedVideos.map((video) => <Video videoObj={video} key={video.id} deleteVideo={deleteVideoItem} />)
)}
</div>
</>
);
}

export default VideosList;
Loading