From 024b303749095f1989b018a2cc2bcac5383e6d2f Mon Sep 17 00:00:00 2001 From: VineetBala-AOT <90332175+VineetBala-AOT@users.noreply.github.com> Date: Fri, 27 Sep 2024 13:34:42 -0700 Subject: [PATCH] hide 'View Feedback' when sendReport is false (#90) --- .../met_api/resources/engagement_settings.py | 2 +- .../engagement/view/ActionContext.tsx | 31 +++++++++++++++++++ .../engagement/view/SurveyBlock.tsx | 26 +++++++++------- 3 files changed, 47 insertions(+), 12 deletions(-) diff --git a/met-api/src/met_api/resources/engagement_settings.py b/met-api/src/met_api/resources/engagement_settings.py index 28bc71f4a..58122e489 100644 --- a/met-api/src/met_api/resources/engagement_settings.py +++ b/met-api/src/met_api/resources/engagement_settings.py @@ -40,7 +40,7 @@ class EngagementMetadata(Resource): @staticmethod @cross_origin(origins=allowedorigins()) - @auth.require + @auth.optional def get(engagement_id): """Fetch a single engagement's settings matching the provided id.""" try: diff --git a/met-web/src/components/engagement/view/ActionContext.tsx b/met-web/src/components/engagement/view/ActionContext.tsx index 478bf75fb..c80cbc322 100644 --- a/met-web/src/components/engagement/view/ActionContext.tsx +++ b/met-web/src/components/engagement/view/ActionContext.tsx @@ -2,11 +2,14 @@ import React, { createContext, useState, useEffect } from 'react'; import { useNavigate, useParams } from 'react-router-dom'; import { getEngagement, patchEngagement } from '../../../services/engagementService'; import { getEngagementMetadata } from '../../../services/engagementMetadataService'; +import { getEngagementSettings } from '../../../services/engagementSettingService'; import { createDefaultEngagement, createDefaultEngagementMetadata, + createDefaultEngagementSettings, Engagement, EngagementMetadata, + EngagementSettings, } from '../../../models/engagement'; import { useAppDispatch } from 'hooks'; import { openNotification } from 'services/notificationService/notificationSlice'; @@ -31,9 +34,11 @@ interface UnpublishEngagementParams { export interface EngagementViewContext { savedEngagement: Engagement; engagementMetadata: EngagementMetadata; + engagementSettings: EngagementSettings; isEngagementLoading: boolean; isWidgetsLoading: boolean; isEngagementMetadataLoading: boolean; + isEngagementSettingsLoading: boolean; scheduleEngagement: (_engagement: EngagementSchedule) => Promise; unpublishEngagement: ({ id, status_id }: UnpublishEngagementParams) => Promise; widgets: Widget[]; @@ -56,9 +61,11 @@ export const ActionContext = createContext({ }, savedEngagement: createDefaultEngagement(), engagementMetadata: createDefaultEngagementMetadata(), + engagementSettings: createDefaultEngagementSettings(), isEngagementLoading: true, isWidgetsLoading: true, isEngagementMetadataLoading: true, + isEngagementSettingsLoading: true, widgets: [], mockStatus: SubmissionStatus.Upcoming, updateMockStatus: (status: SubmissionStatus) => { @@ -75,11 +82,13 @@ export const ActionProvider = ({ children }: { children: JSX.Element | JSX.Eleme ); const [savedEngagement, setSavedEngagement] = useState(createDefaultEngagement()); const [engagementMetadata, setEngagementMetadata] = useState(createDefaultEngagementMetadata()); + const [engagementSettings, setEngagementSettings] = useState(createDefaultEngagementSettings()); const [mockStatus, setMockStatus] = useState(savedEngagement.submission_status); const [widgets, setWidgets] = useState([]); const [isEngagementLoading, setEngagementLoading] = useState(true); const [isWidgetsLoading, setIsWidgetsLoading] = useState(true); const [isEngagementMetadataLoading, setIsEngagementMetadataLoading] = useState(true); + const [isEngagementSettingsLoading, setIsEngagementSettingsLoading] = useState(true); const [getWidgetsTrigger] = useLazyGetWidgetsQuery(); @@ -206,6 +215,25 @@ export const ActionProvider = ({ children }: { children: JSX.Element | JSX.Eleme } }; + const fetchEngagementSettings = async () => { + if (!savedEngagement.id) { + return; + } + try { + const result = await getEngagementSettings(Number(engagementId)); + setEngagementSettings(result); + setIsEngagementSettingsLoading(false); + } catch (error) { + setIsEngagementSettingsLoading(false); + dispatch( + openNotification({ + severity: 'error', + text: 'Error occurred while fetching Engagement Settings', + }), + ); + } + }; + const handleFetchEngagementIdBySlug = async () => { if (!slug) { return; @@ -229,6 +257,7 @@ export const ActionProvider = ({ children }: { children: JSX.Element | JSX.Eleme useEffect(() => { fetchWidgets(); fetchEngagementMetadata(); + fetchEngagementSettings(); }, [savedEngagement]); return ( @@ -236,11 +265,13 @@ export const ActionProvider = ({ children }: { children: JSX.Element | JSX.Eleme value={{ savedEngagement, engagementMetadata, + engagementSettings, isEngagementLoading, scheduleEngagement, widgets, isWidgetsLoading, isEngagementMetadataLoading, + isEngagementSettingsLoading, updateMockStatus, mockStatus, unpublishEngagement, diff --git a/met-web/src/components/engagement/view/SurveyBlock.tsx b/met-web/src/components/engagement/view/SurveyBlock.tsx index 85d5c5e77..fa00ba3c7 100644 --- a/met-web/src/components/engagement/view/SurveyBlock.tsx +++ b/met-web/src/components/engagement/view/SurveyBlock.tsx @@ -12,17 +12,19 @@ import { Case, Switch } from 'react-if'; import { useNavigate } from 'react-router-dom'; const SurveyBlock = ({ startSurvey }: SurveyBlockProps) => { - const { savedEngagement, isEngagementLoading, mockStatus } = useContext(ActionContext); + const { savedEngagement, engagementSettings, isEngagementLoading, isEngagementSettingsLoading, mockStatus } = + useContext(ActionContext); const navigate = useNavigate(); const isLoggedIn = useAppSelector((state) => state.user.authentication.authenticated); const isPreview = isLoggedIn; const currentStatus = isPreview ? mockStatus : savedEngagement.submission_status; + const sendReport = engagementSettings.send_report; const surveyId = savedEngagement.surveys[0]?.id || ''; const status_block = savedEngagement.status_block; const status_text = status_block.find( (status) => status.survey_status === SubmissionStatus[currentStatus], )?.block_text; - if (isEngagementLoading) { + if (isEngagementLoading || isEngagementSettingsLoading) { return ; } @@ -54,15 +56,17 @@ const SurveyBlock = ({ startSurvey }: SurveyBlockProps) => { - { - navigate(`/engagements/${savedEngagement.id}/dashboard/public`); - }} - > - View Feedback - + {sendReport && ( + { + navigate(`/engagements/${savedEngagement.id}/dashboard/public`); + }} + > + View Feedback + + )}