Skip to content

Commit da726aa

Browse files
authored
Merge pull request #1450 from digitalgreenorg/fix/moa
fix(FARMSTACK-36): added option as per the user accesibilty
2 parents 74cf2e7 + c078f60 commit da726aa

File tree

2 files changed

+187
-36
lines changed

2 files changed

+187
-36
lines changed

src/features/default/src/Components/Datasets_New/DataSetsView.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,6 +285,7 @@ const DataSetsView = (props) => {
285285
)}
286286
datasetId={response?.data?.id}
287287
id={tempFile.id}
288+
getDataset={getDataset}
288289
usagePolicy={tempFile.usage_policy}
289290
fileType={tempFile.accessibility}
290291
userType={userType === "guest" ? "guest" : ""}

src/features/default/src/Components/Table/DataTableForDatasetView.jsx

Lines changed: 186 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,12 @@
11
import React, { useContext, useEffect, useMemo, useState } from "react";
2-
import { Box, Button, CircularProgress } from "@mui/material";
3-
import { useHistory } from "react-router-dom";
2+
import {
3+
Box,
4+
Button,
5+
CircularProgress,
6+
useMediaQuery,
7+
useTheme,
8+
} from "@mui/material";
9+
import { useHistory, useLocation } from "react-router-dom";
410
import EmptyFile from "../Datasets_New/TabComponents/EmptyFile";
511
import { Table } from "antd";
612
import DownloadIcon from "@mui/icons-material/Download";
@@ -12,6 +18,12 @@ import HTTPService from "../../Services/HTTPService";
1218
import global_style from "./../../Assets/CSS/global.module.css";
1319
import ArrowForwardIosIcon from "@mui/icons-material/ArrowForwardIos";
1420
import ArrowBackIosNewIcon from "@mui/icons-material/ArrowBackIosNew";
21+
import {
22+
getUserMapId,
23+
isLoggedInUserAdmin,
24+
isLoggedInUserCoSteward,
25+
} from "common/utils/utils";
26+
import { FarmStackContext } from "common/components/context/DefaultContext/FarmstackProvider";
1527

1628
const DataTableForDatasetView = ({
1729
datasetId,
@@ -22,9 +34,14 @@ const DataTableForDatasetView = ({
2234
fileType,
2335
userType,
2436
isOther,
37+
getDataset,
2538
}) => {
2639
const antIcon = <CircularProgress color="inherit" />;
40+
const { callLoader, callToast } = useContext(FarmStackContext);
2741
const history = useHistory();
42+
const location = useLocation();
43+
const theme = useTheme();
44+
const mobile = useMediaQuery(theme.breakpoints.down("sm"));
2845
const [data, setData] = useState();
2946
const [pages, setPages] = useState({
3047
current: 1,
@@ -157,6 +174,104 @@ const DataTableForDatasetView = ({
157174
}
158175
};
159176

177+
const askToDownload = () => {
178+
let accessToken = getTokenLocal() ?? false;
179+
let url = UrlConstant.base_url + UrlConstant.ask_for_permission;
180+
let body = {
181+
dataset_file: id,
182+
user_organization_map: getUserMapId(),
183+
};
184+
callLoader(true);
185+
HTTPService("POST", url, body, false, true, accessToken)
186+
.then((res) => {
187+
callLoader(false);
188+
getDataset();
189+
callToast(
190+
"Successfully, sent the request for downloading the file",
191+
"success",
192+
true
193+
);
194+
})
195+
.catch((err) => {
196+
callLoader(false);
197+
callToast(
198+
"Something went wrong while asking for the permission.",
199+
"error",
200+
true
201+
);
202+
});
203+
};
204+
205+
const handleDelete = (usagePolicyid) => {
206+
let accessToken = getTokenLocal() ?? false;
207+
let url =
208+
UrlConstant.base_url +
209+
UrlConstant.ask_for_permission +
210+
usagePolicyid +
211+
"/";
212+
callLoader(true);
213+
HTTPService("DELETE", url, "", false, true, accessToken)
214+
.then((res) => {
215+
callLoader(false);
216+
getDataset();
217+
})
218+
.catch((err) => {
219+
callLoader(false);
220+
callToast("Something went wrong while recalling.", "error", true);
221+
});
222+
};
223+
224+
const getButtonName = () => {
225+
if (usagePolicy?.[0]) {
226+
if (usagePolicy[0].approval_status === "requested") {
227+
return "Recall";
228+
} else if (usagePolicy[0].approval_status === "approved") {
229+
return "Download";
230+
} else if (usagePolicy[0].approval_status === "rejected") {
231+
return "Ask to Download";
232+
}
233+
} else {
234+
return "Recall";
235+
}
236+
};
237+
238+
const isLoggedInUserFromHome = () => {
239+
if (
240+
location.pathname === "/home/datasets/" + datasetId &&
241+
getTokenLocal() &&
242+
(fileType === "registered" || fileType === "private")
243+
) {
244+
return true;
245+
} else {
246+
return false;
247+
}
248+
};
249+
const handleButtonClick = (id, name) => {
250+
if (userType !== "guest") {
251+
if (fileType === "public" || fileType === "registered" || !isOther) {
252+
handleDownload(id, name);
253+
}
254+
if (isOther && fileType === "private") {
255+
if (!Object.keys(usagePolicy)?.length) {
256+
askToDownload();
257+
} else {
258+
if (usagePolicy?.[0]?.approval_status === "requested") {
259+
handleDelete(usagePolicy?.[0]?.id);
260+
} else if (usagePolicy?.[0]?.approval_status === "approved") {
261+
handleDownload(id, name);
262+
} else if (usagePolicy?.[0]?.approval_status === "rejected") {
263+
askToDownload(id, name);
264+
}
265+
}
266+
}
267+
} else {
268+
if (fileType === "public") {
269+
handleDownload(id, name);
270+
} else {
271+
history.push("/login");
272+
}
273+
}
274+
};
160275
useEffect(() => {
161276
fetchData(0);
162277
setPages({ current: 1, next: false });
@@ -195,41 +310,76 @@ const DataTableForDatasetView = ({
195310
: " (Meta data)"}
196311
</div>
197312
<div>
198-
{usagePolicy &&
199-
(!isOther ||
200-
usagePolicy[0]?.approval_status === "approved" ||
201-
fileType === "public") ? (
202-
<div>
203-
<Button
204-
sx={{
205-
border: "1px solid #00A94F",
206-
color: "#00A94F ",
207-
textTransform: "capitalize",
208-
size: "20px",
209-
}}
210-
onClick={() => handleDownload(id, name)}
211-
disabled={showLoader}
212-
>
213-
<DownloadIcon
214-
fontSize="small"
215-
sx={{ color: "#00A94F !important" }}
216-
/>{" "}
217-
Download
218-
{showLoader && (
219-
<span style={{ margin: "5px 2px 0px 9px" }}>
220-
<CircularProgressWithLabel
221-
value={progress}
222-
color="success"
223-
size={40}
224-
/>
225-
</span>
226-
)}
227-
</Button>{" "}
228-
</div>
229-
) : (
230-
""
231-
)}
313+
<Button
314+
sx={{
315+
border: "1px solid #00A94F",
316+
color: "#00A94F ",
317+
textTransform: "capitalize",
318+
size: "20px",
319+
display: isLoggedInUserFromHome() ? "none" : "",
320+
}}
321+
onClick={() => handleButtonClick(id, name)}
322+
disabled={showLoader}
323+
>
324+
<DownloadIcon
325+
fontSize="small"
326+
sx={{ color: "#00A94F !important" }}
327+
/>{" "}
328+
{userType !== "guest"
329+
? fileType === "public" ||
330+
fileType === "registered" ||
331+
!isOther
332+
? "Download"
333+
: isOther && !Object.keys(usagePolicy).length
334+
? "Ask to Download"
335+
: getButtonName()
336+
: fileType === "public"
337+
? "Download"
338+
: "Login to Download"}
339+
{showLoader && (
340+
<span style={{ margin: "5px 2px 0px 9px" }}>
341+
<CircularProgressWithLabel
342+
value={progress}
343+
color="success"
344+
size={40}
345+
/>
346+
</span>
347+
)}
348+
</Button>
232349
</div>
350+
{isLoggedInUserFromHome() ? (
351+
<Button
352+
sx={{
353+
fontFamily: "Arial",
354+
fontWeight: 700,
355+
fontSize: mobile ? "11px" : "15px",
356+
width: mobile ? "195px" : "220px",
357+
height: "48px",
358+
border: "1px solid rgba(0, 171, 85, 0.48)",
359+
borderRadius: "8px",
360+
color: "#00A94F",
361+
textTransform: "none",
362+
marginLeft: "35px",
363+
marginRight: "25px",
364+
"&:hover": {
365+
background: "none",
366+
border: "1px solid rgba(0, 171, 85, 0.48)",
367+
},
368+
}}
369+
variant="outlined"
370+
onClick={() =>
371+
history.push(
372+
isLoggedInUserAdmin() || isLoggedInUserCoSteward()
373+
? "/datahub/new_datasets"
374+
: "/participant/new_datasets"
375+
)
376+
}
377+
>
378+
Explore Datasets
379+
</Button>
380+
) : (
381+
<></>
382+
)}
233383
</div>
234384
)}
235385
columns={memoCol}

0 commit comments

Comments
 (0)