-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TO MAIN] DESENG-513 - Add poll results to results tab (#2422)
* DESENG-513: Poll results view * DESENG-513: Lint fixes * DESENG-513: Updated changelog * DESENG-513: Votes text update * Removed comments * DESENG-513: Fixing review comments
- Loading branch information
1 parent
29ea71f
commit 50999d4
Showing
12 changed files
with
503 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
118 changes: 118 additions & 0 deletions
118
...mponents/engagement/form/EngagementFormTabs/Results/PollResults/EngagementPollContext.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
import React, { createContext, useContext, useEffect, useState } from 'react'; | ||
import { openNotification } from 'services/notificationService/notificationSlice'; | ||
import { getWidgets } from 'services/widgetService'; | ||
import { fetchPollWidgets, fetchPollResults } from 'services/widgetService/PollService/index'; | ||
import { ActionContext } from '../../../ActionContext'; | ||
import { Widget, WidgetType } from 'models/widget'; | ||
import { PollWidget, PollResultResponse } from 'models/pollWidget'; | ||
import { useAppDispatch } from 'hooks'; | ||
|
||
export interface EngagementPollContextProps { | ||
widget: Widget | null; | ||
isWidgetsLoading: boolean; | ||
pollWidget: PollWidget | null | undefined; | ||
isLoadingPollWidget: boolean; | ||
pollResults: PollResultResponse | null; | ||
isPollResultsLoading: boolean; | ||
} | ||
|
||
export const EngagementPollContext = createContext<EngagementPollContextProps>({ | ||
widget: null, | ||
isWidgetsLoading: true, | ||
pollWidget: null, | ||
isLoadingPollWidget: true, | ||
pollResults: null, | ||
isPollResultsLoading: true, | ||
}); | ||
|
||
export const EngagementPollContextProvider = ({ children }: { children: JSX.Element | JSX.Element[] }) => { | ||
const { savedEngagement } = useContext(ActionContext); | ||
const [widgets, setWidgets] = useState<Widget[] | null>(null); | ||
const [widget, setWidget] = useState<Widget | null>(null); | ||
const [isWidgetsLoading, setIsWidgetsLoading] = useState(true); | ||
const [pollWidget, setPollWidget] = useState<PollWidget | null | undefined>(null); | ||
const [isLoadingPollWidget, setIsLoadingPollWidget] = useState(true); | ||
const [pollResults, setPollResults] = useState<PollResultResponse | null>(null); | ||
const [isPollResultsLoading, setIsPollResultsLoading] = useState(true); | ||
const dispatch = useAppDispatch(); | ||
|
||
const loadWidgets = async () => { | ||
if (!savedEngagement.id) { | ||
setIsWidgetsLoading(false); | ||
return; | ||
} | ||
|
||
try { | ||
const widgetsList = await getWidgets(savedEngagement.id); | ||
setWidgets(widgetsList); | ||
setIsWidgetsLoading(false); | ||
} catch (err) { | ||
setIsWidgetsLoading(false); | ||
dispatch(openNotification({ severity: 'error', text: 'Error fetching engagement widgets' })); | ||
} finally { | ||
setIsWidgetsLoading(false); | ||
} | ||
}; | ||
|
||
const loadPollWidget = async () => { | ||
const widget = widgets?.find((w) => w.widget_type_id === WidgetType.Poll) ?? null; | ||
setWidget(widget); | ||
if (!widget) { | ||
setIsLoadingPollWidget(false); | ||
return; | ||
} | ||
try { | ||
const result = await fetchPollWidgets(widget.id); | ||
setPollWidget(result.at(-1)); | ||
setIsLoadingPollWidget(false); | ||
} catch (error) { | ||
dispatch(openNotification({ severity: 'error', text: 'An error occurred while trying to load Poll data' })); | ||
setIsLoadingPollWidget(false); | ||
} finally { | ||
setIsLoadingPollWidget(false); | ||
} | ||
}; | ||
|
||
const getPollResults = async () => { | ||
try { | ||
if (!widget || !pollWidget) { | ||
return; | ||
} | ||
const data = await fetchPollResults(widget.id, pollWidget.id); | ||
setPollResults(data); | ||
setIsPollResultsLoading(false); | ||
} catch (error) { | ||
dispatch(openNotification({ severity: 'error', text: 'Error fetching poll results' })); | ||
setIsPollResultsLoading(false); | ||
} finally { | ||
setIsPollResultsLoading(false); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
loadWidgets(); | ||
}, [savedEngagement.id]); | ||
|
||
useEffect(() => { | ||
loadPollWidget(); | ||
}, [widgets]); | ||
|
||
useEffect(() => { | ||
getPollResults(); | ||
}, [widget, pollWidget]); | ||
|
||
return ( | ||
<EngagementPollContext.Provider | ||
value={{ | ||
widget, | ||
isWidgetsLoading, | ||
pollWidget, | ||
isLoadingPollWidget, | ||
pollResults, | ||
isPollResultsLoading, | ||
}} | ||
> | ||
{children} | ||
</EngagementPollContext.Provider> | ||
); | ||
}; |
37 changes: 37 additions & 0 deletions
37
met-web/src/components/engagement/form/EngagementFormTabs/Results/PollResults/PollResult.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import React, { useContext } from 'react'; | ||
import { Grid } from '@mui/material'; | ||
import { EngagementPollContext } from './EngagementPollContext'; | ||
import ResultView from './ResultView'; | ||
import { MidScreenLoader } from 'components/common'; | ||
|
||
export const PollResult = () => { | ||
const { widget, isWidgetsLoading, isLoadingPollWidget, pollWidget, pollResults, isPollResultsLoading } = | ||
useContext(EngagementPollContext); | ||
|
||
// Show a loader while the data is being loaded | ||
if (isLoadingPollWidget || isWidgetsLoading || isPollResultsLoading) { | ||
return ( | ||
<Grid container direction="row" alignItems={'flex-start'} justifyContent="flex-start" spacing={2}> | ||
<Grid item xs={12}> | ||
<MidScreenLoader /> | ||
</Grid> | ||
</Grid> | ||
); | ||
} | ||
|
||
// Check if both widget and pollWidget are available | ||
if ( | ||
widget && | ||
pollWidget && | ||
widget.id !== undefined && | ||
pollWidget.id !== undefined && | ||
pollResults?.poll_id !== undefined | ||
) { | ||
return <ResultView pollResult={pollResults} widget={widget} />; | ||
} | ||
|
||
// Display a message or handle the null case when widgetId or pollId is not available | ||
return null; | ||
}; | ||
|
||
export default PollResult; |
13 changes: 13 additions & 0 deletions
13
...src/components/engagement/form/EngagementFormTabs/Results/PollResults/PollResultsView.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import React from 'react'; | ||
import { EngagementPollContextProvider } from './EngagementPollContext'; | ||
import PollResult from './PollResult'; | ||
|
||
export const PollResultsView = () => { | ||
return ( | ||
<EngagementPollContextProvider> | ||
<PollResult /> | ||
</EngagementPollContextProvider> | ||
); | ||
}; | ||
|
||
export default PollResultsView; |
Oops, something went wrong.