-
Notifications
You must be signed in to change notification settings - Fork 19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[TO MAIN] DESENG-513 - Add poll results to results tab #2422
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f2fe246
DESENG-513: Poll results view
ratheesh-aot ad98794
DESENG-513: Lint fixes
ratheesh-aot c43b770
DESENG-513: Updated changelog
ratheesh-aot dcfac42
DESENG-513: Votes text update
ratheesh-aot 7e2a411
Removed comments
ratheesh-aot b90a93a
DESENG-513: Fixing review comments
ratheesh-aot af7a5c2
Merge remote-tracking branch 'upstream/main' into DESENG-513-PollResults
ratheesh-aot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You could add this block in to make sure you set this state regardless of whether
try
orcatch
runsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Doable. Will update other useEffect functions too.