Skip to content

Commit

Permalink
Implementation of a CRUD that consumes the Superheroes API , is wrapp…
Browse files Browse the repository at this point in the history
…ed with Redux and connected to Mongodb.
  • Loading branch information
FMFigueroa committed Feb 15, 2022
1 parent fcda31f commit fa4458a
Show file tree
Hide file tree
Showing 33 changed files with 2,107 additions and 3,864 deletions.
3 changes: 0 additions & 3 deletions .eslintrc.json

This file was deleted.

2 changes: 1 addition & 1 deletion components/admin/AllUsers.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ const AllUsers = () => {
</Link>

<button className="btn btn-danger ms-3 btn-sm" onClick={() => deleteUserHandler(user._id)}>
<i class="ri-delete-bin-line"></i>
<i className="ri-delete-bin-line"></i>
</button>

</div>
Expand Down
9 changes: 6 additions & 3 deletions components/content/Home.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { herosDefault } from "../../db/hero";
import HeroItem from "../../components/hero/HeroItem";
import { submitSearch } from "../../redux/actions/heroActions";
import { submitDetails } from "../../redux/actions/heroActions";
import { getHeros } from "../../redux/actions/heroActions";
import { useSelector, useDispatch, connect } from "react-redux";
import { useRouter } from "next/router";
import { toast } from "react-toastify";
Expand All @@ -32,17 +33,19 @@ const Home = (props) => {

/* TODO: Test load wiht intial state from SSR */
const handleViewDetails = (id) => {
if (dispatch && dispatch !== null && dispatch !== undefined)
if (dispatch && dispatch !== null && dispatch !== undefined) {
dispatch(getHeros());
dispatch(submitDetails(id));
router.push(`/details/${id}`);
router.push(`/details/${id}`);
}
};



return (
<div className="sectionTwo container">
<h1 className={styles.title}>
Welcome to <a href="#">Superheroes</a>
Welcome to <a href="https://superheroapi.com/ids.html">Superheroes</a>
</h1>
<p className={styles.description}>
Get started by searching you favorite Superheroes
Expand Down
75 changes: 69 additions & 6 deletions components/hero/AddTeam.jsx
Original file line number Diff line number Diff line change
@@ -1,19 +1,59 @@
import { useSelector, useDispatch } from "react-redux";
import { newHero } from "../../redux/actions/heroActions";
import { getHeros } from "../../redux/actions/heroActions";
import { useRouter } from "next/router";
import { toast } from "react-toastify";
import { ExternalLinkIcon } from "@heroicons/react/outline";

const AddTeam = ({ hero }) => {

const router = useRouter();
const dispatch = useDispatch();

const { user, isAuthenticated } = useSelector(state => state.loadedUser);
const herosTeam = useSelector((state) => state.allHeros.herosTeam);
const heroeDetails = useSelector((state) => state.detailsHero.hero);


// Add Superheroe to Team
// Add Superheroe to Team
const handleNewHero = (id) => {
let heroData = hero;
if (dispatch && dispatch !== null && dispatch !== undefined)
dispatch(newHero(heroData));
router.push(`/details/${id}`); // refresh page for look message success or error
if (isAuthenticated) {
const hero_Id = herosTeam.filter((heroId) => heroId.id == id);//filter the hero with the id in the team
if (hero_Id.length === 0) {
//console.log(hero_Id.length)
// This const is for select if the hero view details is good or bad.
const heroe = heroeDetails.filter((heroDetails) => heroDetails.biography.alignment === "good");
if (heroe.length === 0) {
//console.log("villain")
// This cicle is for Villain
const villain = herosTeam.filter((hero) => hero.biography[0].alignment === "bad");
if (villain.length >= 3) {
//console.log(villain.length)
toast.error("You can't add more than 3 Villains to your Team");
} else {
const heroData = { ...hero, user: user._id }
dispatch(newHero(heroData));
dispatch(getHeros());// update state of the Team
router.push(`/details/${id}`) // refresh page for look message success or error
}
} else {
// This cicle is for Superheroe
const superheroes = herosTeam.filter((hero) => hero.biography[0].alignment === "good");
console.log("superhero")
if (superheroes.length >= 3) {
toast.error("You can't add more than 3 superheroes to your team");
} else {
const heroData = { ...hero, user: user._id }
dispatch(newHero(heroData));
dispatch(getHeros());// update state of the Team
router.push(`/details/${id}`); // refresh page for look message success or error
}
}
} else {
toast.error("This Hero already in team")
}
} else {
(toast.error("You need to be logged in to add a hero to your team"))
}
};


Expand All @@ -29,3 +69,26 @@ const AddTeam = ({ hero }) => {
}

export default AddTeam



/* const heroe = heroeDetails.filter((heroDetails) => heroDetails.biography.alignment === "good");
if (heroe.length === 0) {
const villain = herosTeam.filter((hero) => hero.biography[0].alignment === "bad");
if (villain.length >= 3) {
toast.error("You can't add more than 3 Villains to your Team");
} else {
const heroData = { ...hero, user: user._id }
dispatch(newHero(heroData));
router.push(`/details/${id}`); // refresh page for look message success or error
}
} else {
const superheroes = herosTeam.filter((hero) => hero.biography[0].alignment === "good");
if (superheroes >= 3) {
toast.error("You can't add more than 3 superheroes to your team");
} else {
const heroData = { ...hero, user: user._id }
dispatch(newHero(heroData));
router.push(`/details/${id}`); // refresh page for look message success or error
}
} */
Loading

1 comment on commit fa4458a

@vercel
Copy link

@vercel vercel bot commented on fa4458a Feb 15, 2022

Choose a reason for hiding this comment

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

Please sign in to comment.