Newer
Older
import "../stylesheets/PlantDetails.css";
import "font-awesome/css/font-awesome.min.css";
import {useEffect, useState} from "react";
import {Plant} from "../utils/schemas";
import {useParams} from "react-router";
import {IBackendConnector} from "../utils/IBackendConnector";
import useGrowbrosFetcher from "../utils/useGrowbrosFetcher";
const {plantId} = useParams();
const [plant, setPlant] = useState<Plant | null>(null);
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
const bc: IBackendConnector = useGrowbrosFetcher();
useEffect(() => {
(async () => {
const result = await bc.getSinglePlant(Number(plantId!));
if (result.err) {
setError(result.err);
} else {
setPlant(result.value!);
}
})();
}, [setPlant, setError]);
if (plant) {
const attributes = [
{title: "Lateinischer Name", value: plant.latinName},
{title: "Beschreibung", value: plant.description},
{title: "Herkunft", value: plant.origin},
{title: "Anbautipps", value: plant.growingTips},
{
title: "Anbauzeitraum",
value: `Von Kalenderwoche ${plant.plantWeekStart} bis Kalenderwoche ${plant.plantWeekEnd}`,
},
{
title: "Erntezeitraum",
value: `Von Kalenderwoche ${plant.harvestWeekStart} bis Kalenderwoche ${plant.harvestWeekEnd}`,
},
{title: "Bodentyp", value: plant.groundType},
{title: "Nährstoffbedarf", value: plant.nutrientDemand},
{title: "Wasserbedarf", value: plant.waterDemand},
{title: "Lichtbedarf", value: plant.lightingDemand},
];
return (
<>
{
error
: <div style={{display: "flex", flexDirection: "column"}}>
<div className="plantDetails">
<h1>{plant.name}</h1>
<img src={plant.imageUrl}></img>
{attributes.map(
(attribute, index) =>
!!attribute.value && (
<div className="infoBox" key={index}>
<div className="title">{attribute.title}</div>
<p>{attribute.value}</p>
</div>
)
)}
</div>
<div
className="buttons"
style={{
display: "flex",
justifyContent: "space-around",
margin: "0 0 40px 0",
}}
>
<ButtonAddToGarden plantId={plant.id}></ButtonAddToGarden>
Blersch Lara
committed
<ButtonRemoveFromGarden plantId={plant.id}></ButtonRemoveFromGarden>
<ButtonAddToWishlist plantId={plant.id}></ButtonAddToWishlist>
Blersch Lara
committed
<ButtonRemoveFromWishlist plantId={plant.id}></ButtonRemoveFromWishlist>
</div>
</div>
}
</>
);
}
}
function ButtonAddToGarden({plantId}: any) {
const [status, setStatus] = useState<null | String>(null);
const bc: IBackendConnector = useGrowbrosFetcher();
const handleButtonClick = async () => {
await bc.addToGarden(plantId);
};
return (
<>
{status && (
<div
className={`status-message ${
status.includes("erfolgreich") ? "success-message" : "error-message"
}`}
>
{status}
</div>
<button title="Zum Garten hinzufügen" onClick={handleButtonClick}>
<i className="fa fa-leaf"></i>
</button>
</>
);
Blersch Lara
committed
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
function ButtonRemoveFromGarden({plantId}: any) {
const [status, setStatus] = useState<null | String>(null);
const bc: IBackendConnector = useGrowbrosFetcher();
const handleButtonClick = async () => {
await bc.removeEntryFromGarden(plantId);
};
return (
<>
{status && (
<div
className={`status-message ${
status.includes("erfolgreich") ? "success-message" : "error-message"
}`}
>
{status}
</div>
)}
<button title="Aus dem Garten entfernen" onClick={handleButtonClick}>
<i className="fa fa-spoon fa-rotate-180 fa-2x"></i>
</button>
</>
);
}
//TODO status+error handling
//TODO css für status message
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
function ButtonAddToWishlist({plantId}: any) {
const [status, setStatus] = useState<null | String>(null);
const bc: IBackendConnector = useGrowbrosFetcher();
const handleButtonClick = async () => {
await bc.addToWishlist(plantId);
};
return (
<>
{status && (
<div
className={`status-message ${
status.includes("erfolgreich") ? "success-message" : "error-message"
}`}
>
{" "}
{status}
</div>
)}
<button title="Zur Wunschliste hinzufügen" onClick={handleButtonClick}>
<i className="fa fa-heart"></i>
</button>
</>
);
Holzheu Hannah
committed
}
Blersch Lara
committed
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
function ButtonRemoveFromWishlist({plantId}: any) {
const [status, setStatus] = useState<null | String>(null);
const bc: IBackendConnector = useGrowbrosFetcher();
const handleButtonClick = async () => {
await bc.removeFromWishlist(plantId);
};
return (
<>
{status && (
<div
className={`status-message ${
status.includes("erfolgreich") ? "success-message" : "error-message"
}`}
>
{status}
</div>
)}
<button title="Aus der Wunschliste entfernen" onClick={handleButtonClick}>
<i className="fa-solid fa-heart-crack"></i>
</button>
</>
);
}
export default PlantDetails;