Skip to content

Commit

Permalink
Merge pull request agiledev-students-fall2023#112 from agiledev-stude…
Browse files Browse the repository at this point in the history
…nts-fall2023/test/104/108/109/110/111

Test/104/108/109/110/111
  • Loading branch information
lunnnnnn authored Nov 9, 2023
2 parents 4092754 + 7316203 commit 361982e
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 106 deletions.
7 changes: 5 additions & 2 deletions back-end/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
{
"name": "back-end",
"version": "1.0.0",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand All @@ -16,9 +15,13 @@
"dotenv": "^16.3.1",
"express": "^4.18.2",
"mocha": "^10.2.0",
"mongoose": "^6.12.3",
"morgan": "^1.10.0",
"multer": "^1.4.5-lts.1",
"nodemon": "^3.0.1",
"path": "^0.12.7",
"url": "^0.11.3"
}
},
"keywords": [],
"description": ""
}
12 changes: 10 additions & 2 deletions back-end/src/app.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ import url from 'url';
import path from 'path';
// middlewares
import multer from "multer";
import axios from 'axios';
import cors from 'cors';
import "dotenv/config";
import morgan from 'morgan';
// routes
import delaccountRouter from './routes/delaccoountRouter.mjs';
import getpieceRouter from './routes/getpieceRouter.mjs';

import {addFavListRouter,favListRouter, getArts} from './routes/modifyFavListRouter.mjs'
const app = express();

// use the morgan middleware to log all incoming http requests
Expand Down Expand Up @@ -46,5 +45,14 @@ app.post("/getpiece", getpieceRouter);
app.post("/delaccount", delaccountRouter);


// Favorites list routes
app.get('/getfavlist', favListRouter);
app.post('/getArts', getArts);
app.post('/favlist/add',addFavListRouter);
// app.post('/favlist/remove',removeFavListRouter);




// export the express app we created to make it available to other modules
export default app;
6 changes: 3 additions & 3 deletions back-end/src/package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "back-end",
"version": "1.0.0",
"description": "The back-end of your project will live in this directory.",
"description": "",
"main": "index.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
Expand All @@ -13,10 +13,10 @@
"cors": "^2.8.5",
"dotenv": "^16.3.1",
"express": "^4.18.2",
"mocha": "^10.2.0",
"morgan": "^1.10.0",
"multer": "^1.4.5-lts.1",
"nodemon": "^3.0.1",
"path": "^0.12.7",
"url": "^0.11.3"
}
}
}
46 changes: 46 additions & 0 deletions back-end/src/routes/modifyFavListRouter.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
let arts = [
// Sample data structure
{ id: '1', name: 'art1', author: 'Leonardo da Vinci', year: '4321', url: 'https://picsum.photos/200', inFavList: true },
{ id: '2', name: 'art2', author: 'XXXXXX', year: '1234', url: 'https://picsum.photos/200', inFavList: false },

];

// export const removeFavListRouter = (req, res) => {
// const { id } = req.body;
// const index = favorites.findIndex(f => f.id === id);
// if (index !== -1) {
// favorites.splice(index, 1);
// res.status(200).send('Item removed from favorites.');
// }
// }

export const getArts = (req,res) => {
try {
const {location, time} = req.body;
res.status(200).send(arts);
} catch (error) {
res.sendStatus(404);
}
}

export const favListRouter = (req, res) => {
const favorites = arts.filter(art => art.inFavList === true);
res.status(200).send(favorites);
};


export const addFavListRouter = (req, res) => {
const artToUpdate = req.body;

const artIndex = arts.findIndex(art => art.id === artToUpdate.id);

if (artIndex > -1) {
arts[artIndex].inFavList = !arts[artIndex].inFavList;
res.status(200).json(arts[artIndex]);
} else {
res.status(404).send('Art not found');
}
};


export default { addFavListRouter,favListRouter, getArts };
74 changes: 52 additions & 22 deletions front-end/src/components/art/ArtItem.jsx
Original file line number Diff line number Diff line change
@@ -1,33 +1,63 @@
import React from 'react';
import { useNavigate, useLocation } from 'react-router-dom'
import React, { useState } from 'react';
import { useNavigate, useLocation } from 'react-router-dom';

import Card from '../common/card';
import IconButton from '@mui/material/IconButton';
import FavoriteIcon from '@mui/icons-material/Favorite';
import FavoriteBorderIcon from '@mui/icons-material/FavoriteBorder';
import axios from 'axios';

const ArtItem = ({ art, updateFavorites }) => { // Added updateFavorites prop to update parent component
const navigate = useNavigate();
const location = useLocation();
const [isFavorited, setIsFavorited] = useState(art.inFavList);

const navigateToDetail = () => {
navigate("/info", { state: { from: location.pathname, art } });
};

const toggleFavorite = async (event) => {
event.stopPropagation();
const newFavoritedState = !isFavorited;

setIsFavorited(newFavoritedState);
const artData = {
id: art.id,
inFavList: newFavoritedState,
};

try {
const response = await axios.post('http://localhost:3000/favlist/add', artData);

if (response.data) {
setIsFavorited(response.data.inFavList);
if (updateFavorites) {
updateFavorites(art.id, response.data.inFavList);
}
}
} catch (error) {
console.error('Error updating favorites', error);
setIsFavorited(!newFavoritedState);
}
};

const ArtItem = ({ art,onRemoveFromFavorites }) => {
const navigate = useNavigate()
const location = useLocation()

const navigateToDetail = (evt) => {
evt.preventDefault()
navigate("/info", {state:{from:location.pathname}}); // Adjust this path as necessary
// window.location.href = "/info";
}

return (
<div className="mb-4">
<Card>
<div className="relative mx-auto">
<img className="w-full rounded-lg" src={art.url} alt={art.name} onClick={navigateToDetail} />
<p className='absolute bottom-[0.15rem] left-[0.15rem] rounded-xl px-1
<Card onClick={navigateToDetail}>
{/* ...other card content... */}
<img className="w-full rounded-lg" src={art.url} alt={art.name} />
<p className='absolute bottom-[0.15rem] left-[0.15rem] rounded-xl px-1
text-lg text-center bg-white bg-opacity-60'>
<span className='pr-[0.15rem]' onClick={onRemoveFromFavorites}>❤️</span>
{art.name}
{/* {art.name} */}
</p>
</div>
<p className="pt-1">{`Van Gogh, Holland, ${art.year}`}</p>
<IconButton onClick={toggleFavorite}>
{isFavorited ? <FavoriteIcon style={{ color: 'red' }} /> : <FavoriteBorderIcon />}
</IconButton>
<p className="pt-1">{`${art.name} , ${art.year}`}</p>
{/* The rest of your card content, such as art title, author, year, etc. */}
</Card>
</div>
);

};

export default ArtItem;
export default ArtItem;
45 changes: 29 additions & 16 deletions front-end/src/components/popup/popupSearch.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const PopupSearch = (props) => {
const navigate = useNavigate()
const location = useLocation()


useEffect(() => {
async function getData() {
const postData = {
Expand All @@ -24,7 +25,7 @@ const PopupSearch = (props) => {
}
try {
const res = await axiosProvider.post(
"/getpiece",
"http://localhost:3000/getArts",
JSON.stringify(postData),
postOptions
)
Expand All @@ -37,10 +38,11 @@ const PopupSearch = (props) => {
getData()
}, [props?.foundData])

const navigateToDetail = (evt) => {
evt.preventDefault()
navigate("/info", {state:{from:location.pathname}}); // Adjust this path as necessary
}
const handleArtItemClick = (artId) => {
// Navigate to the art information page
navigate("/info", { state: { from: location.pathname } });
};


const handleClosePopup = (evt) => {
evt.stopPropagation()
Expand All @@ -50,20 +52,31 @@ const PopupSearch = (props) => {
}))
}

return(
const updateFavorites = (artId, newFavoritedState) => {
setArts(prevArts => prevArts.map(art => {
if (art.id === artId) {
return { ...art, inFavList: newFavoritedState };
}
return art;
}));
};


return (
<>
<div className="overflow-scroll absolute z-[2000] rounded-lg bottom-0 w-full h-[60vh] bg-beige2">

<img className="w-4 m-[4%]" src="/close.png" alt="x" onClick={handleClosePopup}/>
<div className="mx-[10%] items-center rounded-lg h-[calc(60vh-8%-1rem)] overflow-scroll">
{arts.map(art =>
<div onClick={navigateToDetail}>
<ArtItem key={art.id} art={art}/>
</div>)}
</div>
<img className="w-4 m-[4%]" src="/close.png" alt="x" onClick={handleClosePopup} />
<div className="mx-[10%] items-center rounded-lg h-[calc(60vh-8%-1rem)] overflow-scroll">
{arts.map((art) => (
<div key={art.id} onClick={() => handleArtItemClick(art.id)}>
<ArtItem art={art} updateFavorites={updateFavorites} />
</div>
))}
</div>
</div>
</>
)
}
);
};


export default PopupSearch
9 changes: 4 additions & 5 deletions front-end/src/pages/AccountEdit/AccountEdit.jsx
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@

import React, { useState } from 'react';
import { useNavigate } from 'react-router-dom';
import PopupContent from '../../components/popup/popupContent';
Expand All @@ -12,6 +11,7 @@ const AccountEdit = (props) => {
const [currentActionData, setCurrentActionData] = useState(null);
// const [showPopup, setShowPopup] = useState(false);

const location = useLocation();
//TO DO for Richard: send data to backend
const discardChange = (evt) => {
evt.preventDefault()
Expand All @@ -38,10 +38,9 @@ const AccountEdit = (props) => {
evt.preventDefault()
evt.stopPropagation()
}
const deleteAccount = (evt) => {
const confirmLogOutAccount = (evt) => {
evt.preventDefault()
evt.stopPropagation()
setCurrentActionData(confirmDelAccount)
navigate("/login", { state: { from: location.pathname } });
}

const confirmDeleteAccount = async (evt) => {
Expand Down Expand Up @@ -90,7 +89,7 @@ const AccountEdit = (props) => {
"logout": {
link: "Log Out",
title: "Log out of this account",
buttons: [{value:"Confirm", handleClick: confirmLogout},
buttons: [{value:"Confirm", handleClick: confirmLogOutAccount},
{value:"Discard", handleClick: discardChange}],
},
"deleteAccount": {
Expand Down
Loading

0 comments on commit 361982e

Please sign in to comment.