diff --git a/src/components/SuggestCard.jsx b/src/components/SuggestCard.jsx index ad7feca..8a42087 100644 --- a/src/components/SuggestCard.jsx +++ b/src/components/SuggestCard.jsx @@ -1,18 +1,43 @@ import { Link } from 'react-router-dom'; import { motion } from 'framer-motion'; +import { config } from '../logic/constants'; +import { useState } from "react"; const SuggestCard = ({ name, imageUrl }) => { + const [plants, setPlants] = useState(JSON.parse(localStorage.getItem('plants'))); + + const createPlant = async () => { + var newPlants = plants; + let nextId = Math.max.apply(Math, plants.map(function(o) { return o.id; })) + 1; + let addedPlant = { + "id": nextId, + "img": imageUrl, + "type": name, + "age": 0, + "likes": 0, + "owned": true, + "name": name, + "user": "self", + "description": "self" + }; + newPlants.push(addedPlant); + setPlants(newPlants); + localStorage.setItem('plants', JSON.stringify(newPlants)); + + window.location.href = config.base + "/#/garden"; + }; + return (

{name}

{imageUrl}
- + createPlant()}> { localStorage.setItem('plants', JSON.stringify(newPlants)); window.location.href = config.base + "/#/garden"; - - }; return ( diff --git a/src/pages/SuggestPlant.jsx b/src/pages/SuggestPlant.jsx index ecd35aa..623465f 100644 --- a/src/pages/SuggestPlant.jsx +++ b/src/pages/SuggestPlant.jsx @@ -5,7 +5,6 @@ import { Link } from 'react-router-dom'; import suggestPlants from '../logic/suggest'; import plantData from '../logic/plantData.json'; import SuggestCard from '../components/SuggestCard'; -import { config } from '../logic/constants'; const SuggestPlant = () => { const [budget, setBudget] = useState(0); @@ -212,7 +211,6 @@ const SuggestPlant = () => { { {