Skip to content

Commit

Permalink
first working revision
Browse files Browse the repository at this point in the history
  • Loading branch information
atGit2021 committed Aug 10, 2023
1 parent bb16872 commit a54f3a1
Show file tree
Hide file tree
Showing 2 changed files with 111 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,83 @@ import {
DialogTitle,
Typography,
} from '@mui/material';
import { Role } from '~/api/schema/schema';
import { PromptResponseListFragment } from '~/common/fragments';
import { useSession } from '~/components/Session';
import { ProgressReportEditFragment } from '../../ProgressReportEdit.graphql';

interface ConfirmEmptyVariantsProps {
report: ProgressReportEditFragment;
open: boolean;
onContinue: () => void;
onCancel: () => void;
updateConfirmStatus: () => void;
}

export const ConfirmEmptyVariants = ({
report,
open,
onContinue,
onCancel,
updateConfirmStatus,
}: ConfirmEmptyVariantsProps) => {
const { reportFile, teamNews, communityStories } = report;
const { session } = useSession();
const roles = new Set(session?.roles.value);
const hasReportProgressFile = !!reportFile.value;
const projectMgmtHeader = !hasReportProgressFile && reportFile.canEdit;
const hasSelectedTeamNewsPrompt = !!(teamNews.items.length > 0);
const hasSelectedStoryPrompt = !!(communityStories.items.length > 0);
const listItems = [];

const handleConfirmDialog = () => {
onContinue();
};

const handleCloseDialog = () => {
onCancel();
};

const getEmptyResponsesForRole = (
list: PromptResponseListFragment,
roles: Set<Role>
) => {
if (list.items.length === 0) return [];
const responsesForRole = list.items[0]?.responses.filter((item) => {
return item.variant.responsibleRole
? roles.has(item.variant.responsibleRole)
: false;
});
const emptyResponsesByRole = responsesForRole?.filter((item) =>
!item.response.value && item.response.canEdit ? true : false
);
return emptyResponsesByRole ? emptyResponsesByRole : [];
};

const emptyTeamNewsResponsesByRole = getEmptyResponsesForRole(
teamNews,
roles
);
if (!hasSelectedTeamNewsPrompt || emptyTeamNewsResponsesByRole.length > 0) {
listItems.push(<li>Team News</li>);
}

const emptyStoryResponsesByRole = getEmptyResponsesForRole(
communityStories,
roles
);
if (!hasSelectedStoryPrompt || emptyStoryResponsesByRole.length > 0) {
listItems.push(<li>Story</li>);
}

const investorConnectionHeader = !!(listItems.length > 0);
if (!investorConnectionHeader && !projectMgmtHeader && !open) {
updateConfirmStatus();
}

return (
<Dialog
open={open}
open={(investorConnectionHeader || projectMgmtHeader) && open}
onClose={handleCloseDialog}
aria-labelledby="dialog-title"
>
Expand All @@ -38,12 +93,37 @@ export const ConfirmEmptyVariants = ({
<Typography component="h3" variant="h4" paragraph>
The following was not updated or left blank:
</Typography>
{investorConnectionHeader && (
<>
<Typography component="h3" variant="h4" id="investor-connection">
Investor Connection
</Typography>
<ul aria-labelledby="investor-connection">
{listItems.map((item) => item)}
</ul>
</>
)}
{projectMgmtHeader && (
<>
<Typography component="h3" variant="h4" id="project-management">
Project Management
</Typography>
<ul aria-labelledby="progress">
<li>Progress</li>
</ul>
</>
)}
</DialogContent>
<DialogActions>
<Button onClick={handleCloseDialog} variant="text" color="secondary">
Cancel
</Button>
<Button autoFocus onClick={onContinue} variant="text" color="secondary">
<Button
autoFocus
onClick={handleConfirmDialog}
variant="text"
color="secondary"
>
Confirm
</Button>
</DialogActions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,6 @@ import { ConfirmEmptyVariants } from './ConfirmVariantDialog';
import { ProgressReportStatusFragment } from './ProgressReportStatus.graphql';

export const SubmitReportStep: StepComponent = ({ report }) => {
const [isDialogOpen, setIsDialogOpen] = useState(false);
const [formValues, setFormValues] = useState<TransitionFormValues | null>(null);

const navigate = useNavigate();
const { enqueueSnackbar } = useSnackbar();
const createConfetti = useConfetti();
Expand All @@ -30,6 +27,11 @@ export const SubmitReportStep: StepComponent = ({ report }) => {
// Maintain the old status state while closing the drawer.
// Prevents a flash of the new status before the drawer closes.
const [prevStatus, setPrevStatus] = useState<ProgressReportStatusFragment>();
const [isDialogOpen, setIsDialogOpen] = useState(false);
const [formValues, setFormValues] = useState<TransitionFormValues | null>(
null
);
const [isConfirmNeeded, setIsConfirmNeeded] = useState(true);

const executeTransition = useExecuteTransition({
id: report.id,
Expand Down Expand Up @@ -58,17 +60,25 @@ export const SubmitReportStep: StepComponent = ({ report }) => {
},
});

const handleFormSubmit = (formValues: TransitionFormValues) => {
setFormValues(formValues);
setIsDialogOpen(true);
const updateVariantConfirmStatus = () => {
setIsConfirmNeeded(false);
};

const processFormSubmission = async (values: TransitionFormValues) => {
await executeTransition(values);
};

const handleFormSubmit = async (formValues: TransitionFormValues) => {
const values = formValues;
setFormValues(values);
!isConfirmNeeded
? await processFormSubmission(values)
: setIsDialogOpen(true);
};

const handleDialogConfirm = async () => {
setIsDialogOpen(false);

if (formValues) {
await executeTransition(formValues);
}
if (formValues) await processFormSubmission(formValues);
};

const handleDialogCancel = () => {
Expand Down Expand Up @@ -103,12 +113,15 @@ export const SubmitReportStep: StepComponent = ({ report }) => {
/>
</Box>

<ConfirmEmptyVariants
report={report}
open={isDialogOpen}
onContinue={handleDialogConfirm}
onCancel={handleDialogCancel}
/>
{isConfirmNeeded && (
<ConfirmEmptyVariants
report={report}
open={isDialogOpen}
onContinue={handleDialogConfirm}
onCancel={handleDialogCancel}
updateConfirmStatus={updateVariantConfirmStatus}
/>
)}
</Box>
)}
</Form>
Expand Down

0 comments on commit a54f3a1

Please sign in to comment.