Skip to content

Commit

Permalink
fix: app
Browse files Browse the repository at this point in the history
Fixed issues with scorecard sharing settings in the scorecard listing
  • Loading branch information
nnkogift committed Aug 8, 2024
1 parent d132760 commit 6306395
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 42 deletions.
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
import i18n from "@dhis2/d2-i18n";
import { Button, ButtonStrip, colors } from "@dhis2/ui";
import { colors } from "@dhis2/ui";
import {
ScorecardCardImage as holderImage,
truncateDescription,
UserAuthorityOnScorecard,
} from "@scorecard/shared";
import React, { useState } from "react";
import { useNavigate } from "react-router-dom";
import { useRecoilValue } from "recoil";
import { ScorecardListItem } from "../../types";
import { useScorecardSharingSettings } from "../../hooks/authority";
import { ScorecardListCardActions } from "./ScorecardListCardActions";

export default function ScorecardListCard({
scorecard,
Expand All @@ -17,16 +16,16 @@ export default function ScorecardListCard({
scorecard: ScorecardListItem;
grid: boolean;
}) {
const { write, delete: deletePermission } = useRecoilValue(
UserAuthorityOnScorecard(scorecard?.id),
);
const { write, read } = useScorecardSharingSettings(scorecard);

const { title, description, id } = scorecard ?? {};
const [showFullDescription, setShowFullDescription] = useState(false);
const navigate = useNavigate();

const onView = () => {
navigate(`/view/${id}`);
};

return grid ? (
<div
className="container-bordered p-16 "
Expand Down Expand Up @@ -68,28 +67,7 @@ export default function ScorecardListCard({
</p>
</div>
<div style={{ margin: "0 8px" }}>
<ButtonStrip middle>
<Button onClick={onView}>{i18n.t("View")}</Button>
{write && (
<Button
dataTest={"edit-scorecard-button"}
onClick={function (_: any, e: any) {
e.stopPropagation();
onEdit();
}}
>
{i18n.t("Edit")}
</Button>
)}
{deletePermission && (
<Button
dataTest="scorecard-delete-button"
onClick={onDelete}
>
{i18n.t("Delete")}
</Button>
)}
</ButtonStrip>
<ScorecardListCardActions scorecard={scorecard} />
</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Button, ButtonStrip } from "@dhis2/ui";
import { Button, ButtonStrip, CircularLoader } from "@dhis2/ui";
import i18n from "@dhis2/d2-i18n";
import React from "react";
import { useScorecardSharingSettings } from "../../hooks/authority";
Expand All @@ -15,7 +15,11 @@ export interface ScorecardListCardActionsProps {
export function ScorecardListCardActions({
scorecard,
}: ScorecardListCardActionsProps) {
const accessConfig = useScorecardSharingSettings({
const {
access: accessConfig,
loading,
error,
} = useScorecardSharingSettings({
id: scorecard.id,
});
const navigate = useNavigate();
Expand All @@ -28,8 +32,21 @@ export function ScorecardListCardActions({
);
const { remove } = useDeleteScorecard(scorecard.id);

if (!accessConfig) {
return null;
if (loading) {
return (
<div className="column w-100 center align-content-center">
<CircularLoader extrasmall />
</div>
);
}

if (error) {
const message = error.message;
return (
<div>
{i18n.t("Could not determine scorecard's access")}: {message}
</div>
);
}

const onView = () => {
Expand All @@ -42,7 +59,7 @@ export function ScorecardListCardActions({
if (write) {
try {
await remove();
} catch (e) {
} catch (e: any) {
show({
message: e.message,
type: { info: true },
Expand All @@ -59,7 +76,6 @@ export function ScorecardListCardActions({
navigate(`/edit/${scorecard.id}`);
}
};

const onDelete = (value: any, event: any) => {
event.stopPropagation();
confirm({
Expand All @@ -79,7 +95,7 @@ export function ScorecardListCardActions({

return (
<ButtonStrip middle>
<Button onClick={onView}>{i18n.t("View")}</Button>
{read && <Button onClick={onView}>{i18n.t("View")}</Button>}
{write && (
<Button
dataTest={"edit-scorecard-button"}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export default function GridScorecardDisplay({
}) {
return (
<div className="scorecard-list-container grid p-32">
{scorecards?.map((scorecard: any) => (
{scorecards?.map((scorecard) => (
<ScorecardListCard
grid
key={scorecard.id}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,12 +49,22 @@ export function useScorecardSharingSettings({ id }: { id: string }) {
},
},
);
return useMemo(() => {
const access = useMemo(() => {
if (!data) {
return;
return {
write: false,
read: false,
delete: false,
};
} else {
const sharing = data.meta.sharing;
return getUserAuthority(user, sharing);
}
}, [data, user]);

return {
access,
loading,
error,
};
}
12 changes: 9 additions & 3 deletions packages/shared/src/utils/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,10 @@ export function sortDataSourcesBasedOnNames({ sort, dataSources }: any) {
return filteredDataSources;
}

function translateAccess(access = "") {
function translateAccess(access: string = ""): {
read: boolean;
write: boolean;
} {
const translatedAccess = {
read: false,
write: false,
Expand All @@ -313,15 +316,18 @@ function translateAccess(access = "") {
return translatedAccess;
}

export function getUserAuthority(user: D2User, sharing: Sharing) {
export function getUserAuthority(
user: D2User,
sharing: Sharing,
): { read: boolean; write: boolean } {
const {
users,
userGroups,
public: publicAccess,
owner: userId,
} = sharing ?? {};
if (user.id === userId) {
return { ...translateAccess("rw-----"), delete: true };
return { ...translateAccess("rw-----") };
}

if (!isEmpty(users)) {
Expand Down

0 comments on commit 6306395

Please sign in to comment.