Skip to content

Commit

Permalink
feat: reporting images added (#147)
Browse files Browse the repository at this point in the history
  • Loading branch information
sumana2001 authored Aug 21, 2022
1 parent 75f4199 commit a25c1ef
Show file tree
Hide file tree
Showing 2 changed files with 75 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/components/ResponsiveAppBar.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ const ResponsiveAppBar = () => {
<Button
color="inherit"
onClick={handleCloseNavMenu}
sx={{ my: 2, display: "block" }}
sx={{ my: 2 }}
component={Link}
to={`/settings`}
data-welcome-tour="settings"
Expand Down
78 changes: 74 additions & 4 deletions src/pages/questions/ProductInformation.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,20 +13,29 @@ import Table from "@mui/material/Table";
import TableBody from "@mui/material/TableBody";
import TableCell from "@mui/material/TableCell";
import TableRow from "@mui/material/TableRow";

import OutlinedFlagIcon from "@mui/icons-material/OutlinedFlag";
import FlagIcon from "@mui/icons-material/Flag";
import Zoom from "react-medium-image-zoom";
import "react-medium-image-zoom/dist/styles.css";

import { useTranslation } from "react-i18next";
import { NO_QUESTION_LEFT } from "../../const";
import offService from "../../off";

import IconButton from "@mui/material/IconButton";
import Tooltip from "@mui/material/Tooltip";
import axios from "axios";
import {
localSettings,
localSettingsKeys,
getHideImages,
} from "../../localeStorageManager";

// src looks like: "https://static.openfoodfacts.org/images/products/004/900/053/2258/1.jpg"
const getImageId = (src) => {
const file = src.split("/").at(-1);
const imageId = file.replace(/\..+$/, "");
return Number(imageId);
};

const getImagesUrls = (images, barcode) => {
if (!images || !barcode) {
return [];
Expand All @@ -42,11 +51,53 @@ const ProductInformation = ({ question }) => {
const { t } = useTranslation();
const [productData, setProductData] = React.useState({});
const [hideImages, setHideImages] = React.useState(getHideImages);
const [flagged, setFlagged] = React.useState([]);

const flagImage = (src, barcode) => {
const imgid = getImageId(src);
axios
.put(
`https://amathjourney.com/api/off-annotation/flag-image/${barcode}`,
{
mode: "no-cors",
imgid: imgid,
url: src,
}
)
.catch(() => {
console.log("Image flagged");
});
const newFlagged = [...flagged];
newFlagged.push(imgid);
setFlagged(newFlagged);
};

const deleteFlagImage = (src, barcode) => {
const imgid = getImageId(src);
axios
.delete(
`https://amathjourney.com/api/off-annotation/flag-image/${barcode}`,
{
mode: "no-cors",
data: {
imgid: imgid,
},
}
)
.catch(() => {
console.log("Something went wrong. Image could not be flagged");
});
const newFlagged = flagged.filter(
(flaggedImageId) => flaggedImageId !== imgid
);
setFlagged(newFlagged);
};

React.useEffect(() => {
if (!question?.barcode) {
return;
}
setFlagged([]);
let isStillValid = true;
setProductData({
code: question.barcode,
Expand Down Expand Up @@ -121,7 +172,11 @@ const ProductInformation = ({ question }) => {
{!hideImages && productData?.images && (
<Grid container rowSpacing={1.5} spacing={1}>
{getImagesUrls(productData.images, question.barcode).map((src) => (
<Grid item key={src}>
<Grid
item
key={src}
style={{ display: "inline-flex", alignItems: "flex-start" }}
>
<Zoom>
<img
src={src}
Expand All @@ -130,6 +185,21 @@ const ProductInformation = ({ question }) => {
style={{ maxWidth: 300, maxHeight: 300 }}
/>
</Zoom>
{flagged.includes(getImageId(src)) ? (
<Tooltip title="Unflag the image">
<IconButton
onClick={() => deleteFlagImage(src, question.barcode)}
>
<FlagIcon />
</IconButton>
</Tooltip>
) : (
<Tooltip title="Something wrong? Report/flag the image!">
<IconButton onClick={() => flagImage(src, question.barcode)}>
<OutlinedFlagIcon />
</IconButton>
</Tooltip>
)}
</Grid>
))}
</Grid>
Expand Down

0 comments on commit a25c1ef

Please sign in to comment.