Skip to content

Commit

Permalink
hide 'View Feedback' when sendReport is false (#90)
Browse files Browse the repository at this point in the history
  • Loading branch information
VineetBala-AOT authored Sep 27, 2024
1 parent 47101e5 commit 024b303
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 12 deletions.
2 changes: 1 addition & 1 deletion met-api/src/met_api/resources/engagement_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
31 changes: 31 additions & 0 deletions met-web/src/components/engagement/view/ActionContext.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand All @@ -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<Engagement>;
unpublishEngagement: ({ id, status_id }: UnpublishEngagementParams) => Promise<void>;
widgets: Widget[];
Expand All @@ -56,9 +61,11 @@ export const ActionContext = createContext<EngagementViewContext>({
},
savedEngagement: createDefaultEngagement(),
engagementMetadata: createDefaultEngagementMetadata(),
engagementSettings: createDefaultEngagementSettings(),
isEngagementLoading: true,
isWidgetsLoading: true,
isEngagementMetadataLoading: true,
isEngagementSettingsLoading: true,
widgets: [],
mockStatus: SubmissionStatus.Upcoming,
updateMockStatus: (status: SubmissionStatus) => {
Expand All @@ -75,11 +82,13 @@ export const ActionProvider = ({ children }: { children: JSX.Element | JSX.Eleme
);
const [savedEngagement, setSavedEngagement] = useState<Engagement>(createDefaultEngagement());
const [engagementMetadata, setEngagementMetadata] = useState<EngagementMetadata>(createDefaultEngagementMetadata());
const [engagementSettings, setEngagementSettings] = useState<EngagementSettings>(createDefaultEngagementSettings());
const [mockStatus, setMockStatus] = useState(savedEngagement.submission_status);
const [widgets, setWidgets] = useState<Widget[]>([]);
const [isEngagementLoading, setEngagementLoading] = useState(true);
const [isWidgetsLoading, setIsWidgetsLoading] = useState(true);
const [isEngagementMetadataLoading, setIsEngagementMetadataLoading] = useState(true);
const [isEngagementSettingsLoading, setIsEngagementSettingsLoading] = useState(true);

const [getWidgetsTrigger] = useLazyGetWidgetsQuery();

Expand Down Expand Up @@ -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;
Expand All @@ -229,18 +257,21 @@ export const ActionProvider = ({ children }: { children: JSX.Element | JSX.Eleme
useEffect(() => {
fetchWidgets();
fetchEngagementMetadata();
fetchEngagementSettings();
}, [savedEngagement]);

return (
<ActionContext.Provider
value={{
savedEngagement,
engagementMetadata,
engagementSettings,
isEngagementLoading,
scheduleEngagement,
widgets,
isWidgetsLoading,
isEngagementMetadataLoading,
isEngagementSettingsLoading,
updateMockStatus,
mockStatus,
unpublishEngagement,
Expand Down
26 changes: 15 additions & 11 deletions met-web/src/components/engagement/view/SurveyBlock.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 <Skeleton variant="rectangular" height={'15em'} />;
}

Expand Down Expand Up @@ -54,15 +56,17 @@ const SurveyBlock = ({ startSurvey }: SurveyBlockProps) => {
</Case>
<Case condition={currentStatus === SubmissionStatus.Closed}>
<Grid item container direction={{ xs: 'column', sm: 'row' }} xs={12} justifyContent="flex-end">
<PrimaryButton
data-testid="SurveyBlock/view-feedback-button"
disabled={!surveyId}
onClick={() => {
navigate(`/engagements/${savedEngagement.id}/dashboard/public`);
}}
>
View Feedback
</PrimaryButton>
{sendReport && (
<PrimaryButton
data-testid="SurveyBlock/view-feedback-button"
disabled={!surveyId}
onClick={() => {
navigate(`/engagements/${savedEngagement.id}/dashboard/public`);
}}
>
View Feedback
</PrimaryButton>
)}
</Grid>
</Case>
</Switch>
Expand Down

0 comments on commit 024b303

Please sign in to comment.