Skip to content

Commit

Permalink
refactor how active is applied and improved reducer
Browse files Browse the repository at this point in the history
  • Loading branch information
Matthew Day committed Aug 4, 2021
1 parent e1a1666 commit b96f385
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 21 deletions.
1 change: 0 additions & 1 deletion react-ui/src/redux/actions.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@ export const updateGender = (gender) => {
};

export const updateFavorite = (id, name) => {
// console.log('id =====> ', id, name);
return {
type: GET_FAVORITE,
payload: {
Expand Down
12 changes: 4 additions & 8 deletions react-ui/src/redux/reducers/favorites.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
import { GET_FAVORITE } from "../actionTypes";

const initialState = '';
const initialState = [];

export default function(state=initialState, action) {
console.log("action ====> ", action);
switch (action.type) {
case GET_FAVORITE:
const {id, name} = action.payload;
return {
...state,
id: id,
name: name,
}
const copy = state.slice()
copy.push(action.payload);
return copy;
default:
return state;
}
Expand Down
30 changes: 18 additions & 12 deletions react-ui/src/routes/decide/DecideList.jsx
Original file line number Diff line number Diff line change
@@ -1,32 +1,34 @@
import React, { useState } from "react";
import React from "react";
import { connect } from "react-redux";
import "./DecideList.css";
import { updateFavorite } from "../../redux/actions";

const DecideList = (props) => {
const DecideList = ({ candidates, updateFavorite }) => {

const toggleClass = (e) => {
console.log("e.target: ===>", e.target);
if (e.target.className === "fa fa-thumbs-up fa-3x like active") {
e.target.classList.remove("active");
const handleSelection = (e) => {
const thumbsUp = e.currentTarget.querySelector('i');
const isActive = thumbsUp.classList.contains('active');
const id = e.currentTarget.attributes['dataid'].value;
const name = e.currentTarget.attributes['dataname'].value;
if (isActive) {
thumbsUp.classList.remove("active");
} else {
e.target.classList.add("active");
thumbsUp.classList.add("active");
}
dispatch(updateFavorite(1, 'heggy here'))
updateFavorite(id, name);
};

const candidates = props.candidates;
const candidateItems = candidates.map((candidate, index) => {
console.log(candidate)
return (
<div className="makemeflex-col give-margin-right-8">
<div className="makemeflex-col give-margin-right-8" dataId={candidate.id} dataName={candidate.name} onClick={handleSelection}>
<img src={candidate.avatar} alt="person" />
<span>
<div className="rating">
<div className="like">
<i
className="fa fa-thumbs-up fa-3x like"
aria-hidden="true"
onClick={toggleClass}
></i>
</div>
</div>
Expand All @@ -37,4 +39,8 @@ const DecideList = (props) => {
return <div className="makemeflex">{candidateItems}</div>;
};

export default connect(null, { updateFavorite })(DecideList);
const mapDispatchToProps = dispatch => ({
updateFavorite: (id, username) => dispatch(updateFavorite(id, username))
});

export default connect(null, mapDispatchToProps)(DecideList);

0 comments on commit b96f385

Please sign in to comment.