From 0ba5af89622d685ebe4bd39deb353d580ff6452c Mon Sep 17 00:00:00 2001 From: Gift Nnko Date: Tue, 24 Sep 2024 14:57:47 +0300 Subject: [PATCH] fix: scorecard list Fixed issues with sharing access not being accurate --- .../components/Cards/ScorecardListCard.tsx | 20 +++++--- .../Cards/ScorecardListCardActions.tsx | 51 ++++++------------- .../src/modules/ScorecardList/hooks/data.ts | 2 +- .../src/modules/ScorecardList/types/index.ts | 2 +- packages/app/src/utils/sharing.ts | 8 +-- 5 files changed, 36 insertions(+), 47 deletions(-) diff --git a/packages/app/src/modules/ScorecardList/components/Cards/ScorecardListCard.tsx b/packages/app/src/modules/ScorecardList/components/Cards/ScorecardListCard.tsx index 68822ca24..015aa4a62 100644 --- a/packages/app/src/modules/ScorecardList/components/Cards/ScorecardListCard.tsx +++ b/packages/app/src/modules/ScorecardList/components/Cards/ScorecardListCard.tsx @@ -1,33 +1,41 @@ import { colors } from "@dhis2/ui"; -import { getOrgUnitIdsFromOrgUnitSelection, ScorecardCardImage as holderImage, truncateDescription } from "@scorecard/shared"; +import { getOrgUnitIdsFromOrgUnitSelection, getUserAuthority, ScorecardCardImage as holderImage, truncateDescription, UserState } from "@scorecard/shared"; import React, { useState } from "react"; import { useNavigate } from "react-router-dom"; import { ScorecardListItem } from "../../types"; import { ScorecardListCardActions } from "./ScorecardListCardActions"; import { OrgUnitSelection } from "@hisptz/dhis2-utils"; +import { getSharingSettingsFromOldConfiguration } from "../../../../utils/sharing"; +import { useRecoilValue } from "recoil"; export default function ScorecardListCard({ scorecard, grid }: { scorecard: ScorecardListItem; - grid: boolean; + grid?: boolean; }) { + const user = useRecoilValue(UserState); const { title, description, id, orgUnitSelection, periodSelection } = scorecard ?? {}; const [showFullDescription, setShowFullDescription] = useState(false); const navigate = useNavigate(); + const accessConfig = getUserAuthority(user, scorecard.sharing ?? getSharingSettingsFromOldConfiguration(scorecard as any)); + const { read } = accessConfig; + const onView = () => { - const orgUnitIds = getOrgUnitIdsFromOrgUnitSelection(orgUnitSelection as OrgUnitSelection).join(";"); - const periodIds = periodSelection.periods.map(({ id }) => id).join(";"); - navigate(`/view/${id}?ou=${orgUnitIds}&pe=${periodIds}`); + if (read) { + const orgUnitIds = getOrgUnitIdsFromOrgUnitSelection(orgUnitSelection as OrgUnitSelection).join(";"); + const periodIds = periodSelection.periods.map(({ id }) => id).join(";"); + navigate(`/view/${id}?ou=${orgUnitIds}&pe=${periodIds}`); + } }; return grid ? (
diff --git a/packages/app/src/modules/ScorecardList/components/Cards/ScorecardListCardActions.tsx b/packages/app/src/modules/ScorecardList/components/Cards/ScorecardListCardActions.tsx index 0887699ac..d3b78ed35 100644 --- a/packages/app/src/modules/ScorecardList/components/Cards/ScorecardListCardActions.tsx +++ b/packages/app/src/modules/ScorecardList/components/Cards/ScorecardListCardActions.tsx @@ -1,58 +1,38 @@ -import { Button, ButtonStrip, CircularLoader } from "@dhis2/ui"; +import { Button, ButtonStrip } from "@dhis2/ui"; import i18n from "@dhis2/d2-i18n"; import React from "react"; -import { useScorecardSharingSettings } from "../../hooks/authority"; import { ScorecardListItem } from "../../types"; -import { useDeleteScorecard } from "@scorecard/shared"; +import { getUserAuthority, useDeleteScorecard, UserState } from "@scorecard/shared"; import { useAlert } from "@dhis2/app-runtime"; import { useDialog } from "@hisptz/dhis2-ui"; import { useNavigate } from "react-router-dom"; +import { useRecoilValue } from "recoil"; +import { getSharingSettingsFromOldConfiguration } from "../../../../utils/sharing"; export interface ScorecardListCardActionsProps { scorecard: ScorecardListItem; } export function ScorecardListCardActions({ - scorecard, -}: ScorecardListCardActionsProps) { - const { - access: accessConfig, - loading, - error, - } = useScorecardSharingSettings({ - id: scorecard.id, - }); + scorecard + }: ScorecardListCardActionsProps) { + const user = useRecoilValue(UserState); const navigate = useNavigate(); const { confirm } = useDialog(); const { show } = useAlert( ({ message }) => message, - ({ type }) => ({ ...type, duration: 3000 }), + ({ type }) => ({ ...type, duration: 3000 }) ); const { remove } = useDeleteScorecard(scorecard.id); - if (loading) { - return ( -
- -
- ); - } - - if (error) { - const message = error.message; - return ( -
- {i18n.t("Could not determine scorecard's access")}: {message} -
- ); - } - const onView = () => { navigate(`/view/${scorecard.id}`); }; + const accessConfig = getUserAuthority(user, scorecard.sharing ?? getSharingSettingsFromOldConfiguration(scorecard as any)); + const { read, write } = accessConfig; const deleteScorecard = async () => { @@ -62,12 +42,12 @@ export function ScorecardListCardActions({ } catch (e: any) { show({ message: e.message, - type: { info: true }, + type: { info: true } }); } show({ message: i18n.t("Scorecard deleted successfully"), - type: { success: true }, + type: { success: true } }); } }; @@ -86,10 +66,11 @@ export function ScorecardListCardActions({ {scorecard.title}

), - onCancel: () => {}, + onCancel: () => { + }, onConfirm: async () => { await deleteScorecard(); - }, + } }); }; @@ -99,7 +80,7 @@ export function ScorecardListCardActions({ {write && (