Skip to content
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

copy plan implementation #1138

Merged
merged 1 commit into from
Jun 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/components/App.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import EditableProvider from '../providers/EditableProvider';
import PlanProvider from '../providers/PlanProvider';
import theme from './theme';
import { SWRConfig } from 'swr';
import { ConfirmationModalProvider } from '../providers/ConfrimationModalProvider';

const store = configureStore();

Expand All @@ -50,7 +51,9 @@ class App extends Component {
<EditableProvider editable={true}>
<ReferencesProvider>
<ToastProvider>
<Router />
<ConfirmationModalProvider>
<Router />
</ConfirmationModalProvider>
</ToastProvider>
</ReferencesProvider>
</EditableProvider>
Expand Down
1 change: 0 additions & 1 deletion src/components/rangeUsePlanPage/ActionBtns.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,6 @@ const ActionBtns = ({
...permissionsOptions,
};

console.log(permissions);
return (
<>
<div className="rup__actions__btns__buttons">
Expand Down
18 changes: 18 additions & 0 deletions src/components/selectRangeUsePlanPage/CopyPlanMenuItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import React from 'react';
import { MenuItem } from '@material-ui/core';
import { saveDataInLocalStorage } from '../../utils';

const CopyPlanMenuItem = ({ planId, agreementId, menuText }) => {
return (
<MenuItem
onClick={async (e) => {
saveDataInLocalStorage('copyPlanInfo', { planId, agreementId });
e.stopPropagation();
}}
>
{menuText}
</MenuItem>
);
};

export default CopyPlanMenuItem;
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ const CreateReplacementPlan = ({ planId }) => {
onClick={async (e) => {
e.stopPropagation();
const replacementPlan = await createReplacementPlan(planId);
console.log(replacementPlan);
history.push(`${RANGE_USE_PLAN}/${replacementPlan.id}`);
}}
>
Expand Down
52 changes: 17 additions & 35 deletions src/components/selectRangeUsePlanPage/ExtensionColumn.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,16 @@ import * as API from '../../constants/api';
import DatePickerDialog from './DatePickerDialog';
import { PLAN_EXTENSION_STATUS } from '../../constants/variables';
import { Button } from 'formik-semantic-ui';
import PlanExtensionConfirmationModal from './PlanExtensionConfirmationModal';
import { PLAN_EXTENSION_CONFIRMATION_QUESTION } from '../../constants/strings';
import useConfirm from '../../providers/ConfrimationModalProvider';

export default function ExtensionColumn({ user, currentPage, agreement }) {
const history = useHistory();
const [loading, setLoading] = useState(false);
const [voting, setVoting] = useState(false);
const [futureDate, setFutureDate] = useState(null);
const [dialogOpen, setDialogOpen] = useState(false);
const [confirmationModal, setConfirmationModal] = useState({ open: false });
const confirm = useConfirm();

const getPlanFutureDate = (planEndDate) => {
if (isNaN(Date.parse(planEndDate))) planEndDate = new Date();
Expand All @@ -49,15 +49,12 @@ export default function ExtensionColumn({ user, currentPage, agreement }) {
};

const handleRecommend = async (planId) => {
setConfirmationModal({
open: true,
header: 'Confirm',
content: PLAN_EXTENSION_CONFIRMATION_QUESTION('recommend'),
onConfirm: () => {
recommendExtension(planId);
setConfirmationModal({ open: false });
},
const choice = await confirm({
contentText: PLAN_EXTENSION_CONFIRMATION_QUESTION('recommend'),
});
if (choice) {
recommendExtension(planId);
}
};

const extendPlan = async (planId) => {
Expand Down Expand Up @@ -93,15 +90,12 @@ export default function ExtensionColumn({ user, currentPage, agreement }) {
};

const handleApprove = async (planId) => {
setConfirmationModal({
open: true,
header: 'Confirm',
content: PLAN_EXTENSION_CONFIRMATION_QUESTION('approve'),
onConfirm: () => {
approveExtension(planId);
setConfirmationModal({ open: false });
},
const choice = await confirm({
contentText: PLAN_EXTENSION_CONFIRMATION_QUESTION('approve'),
});
if (choice) {
approveExtension(planId);
}
};
const rejectExtension = async (planId) => {
setVoting(true);
Expand All @@ -120,15 +114,12 @@ export default function ExtensionColumn({ user, currentPage, agreement }) {
};

const handleReject = async (planId) => {
setConfirmationModal({
open: true,
header: 'Confirm',
content: PLAN_EXTENSION_CONFIRMATION_QUESTION('reject'),
onConfirm: () => {
rejectExtension(planId);
setConfirmationModal({ open: false });
},
const choice = await confirm({
contentText: PLAN_EXTENSION_CONFIRMATION_QUESTION('reject'),
});
if (choice) {
rejectExtension(planId);
}
};

const renderExtensionForDecisionMaker = (user, agreement) => {
Expand Down Expand Up @@ -376,15 +367,6 @@ export default function ExtensionColumn({ user, currentPage, agreement }) {
extendPlan(agreement.plan.id, futureDate);
}}
/>
<PlanExtensionConfirmationModal
header={confirmationModal.header}
content={confirmationModal.content}
open={confirmationModal.open}
onConfirm={confirmationModal.onConfirm}
onClose={() => {
setConfirmationModal({ open: false });
}}
/>
</>
);
}
61 changes: 61 additions & 0 deletions src/components/selectRangeUsePlanPage/PastePlanMenuItem.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import React from 'react';
import { MenuItem } from '@material-ui/core';
import {
axios,
getAuthHeaderConfig,
getDataFromLocalStorage,
} from '../../utils';
import useConfirm from '../../providers/ConfrimationModalProvider';
import { PLAN_PASTE_CONFIRMATION_QUESTION } from '../../constants/strings';
import * as API from '../../constants/api';
import { useToast } from '../../providers/ToastProvider';
import { useHistory } from 'react-router-dom';

const PastePlanMenuItem = ({ agreementId, menuText, currentPage }) => {
const history = useHistory();
const { errorToast } = useToast();
const confirm = useConfirm();
const sourcePlan = getDataFromLocalStorage('copyPlanInfo');
if (!sourcePlan?.agreementId) {
return null;
}
return (
<MenuItem
onClick={async () => {
if (!sourcePlan?.agreementId) {
errorToast('You need to copy a plan.');
return;
}
const choice = await confirm({
contentText: PLAN_PASTE_CONFIRMATION_QUESTION(
sourcePlan.agreementId,
agreementId,
),
});
if (choice) {
try {
const response = await axios.put(
API.COPY_PLAN(sourcePlan.planId, agreementId),
{},
getAuthHeaderConfig(),
);
history.push({
pathname: `/range-use-plan/${response.data.planId}`,
state: {
page: currentPage,
prevSearch: location.search,
},
});
} catch (e) {
errorToast('Error creating plan');
return;
}
}
}}
>
{menuText}
</MenuItem>
);
};

export default PastePlanMenuItem;
16 changes: 16 additions & 0 deletions src/components/selectRangeUsePlanPage/PlanActions.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@ import KeyboardArrowDown from '@material-ui/icons/KeyboardArrowDown';
import React from 'react';
import * as strings from '../../constants/strings';
import { PLAN_EXTENSION_STATUS } from '../../constants/variables';
import CopyPlanMenuItem from './CopyPlanMenuItem';
import CreateReplacementPlan from './CreateReplacementPlan';
import NewPlanMenuItem from './NewPlanMenuItem';
import ViewPlanMenuItem from './ViewPlanMenuItem';
import PastePlanMenuItem from './PastePlanMenuItem';

export default function PlanActions({
agreement,
Expand Down Expand Up @@ -72,9 +74,23 @@ export default function PlanActions({
menuText={canEdit ? strings.EDIT : strings.VIEW}
/>
)}
{planId && (
<CopyPlanMenuItem
planId={planId}
agreementId={agreement.id}
menuText={'Copy'}
/>
)}
{canCreatePlan && !planId && (
<NewPlanMenuItem agreement={agreement} />
)}
{canCreatePlan && !planId && (
<PastePlanMenuItem
agreementId={agreement.id}
menuText={'Paste'}
currentPage={currentPage}
/>
)}
{[
PLAN_EXTENSION_STATUS.AGREEMENT_HOLDER_REJECTED,
PLAN_EXTENSION_STATUS.STAFF_REJECTED,
Expand Down

This file was deleted.

2 changes: 2 additions & 0 deletions src/constants/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,8 @@ export const REPLACEMENT_PLAN = (planId) =>
export const REJECT_VOTE = (planId) => `/v1/plan/${planId}/extension/reject`;
export const UPDATE_CONFIRMATION = (planId, confirmationId) =>
`/v1/plan/${planId}/confirmation/${confirmationId}`;
export const COPY_PLAN = (planId, agreementId) =>
`/v1/plan/${planId}/copy/${agreementId}`;
export const DISCARD_AMENDMENT = (planId) =>
`v1/plan/${planId}/discard-amendment`;

Expand Down
6 changes: 6 additions & 0 deletions src/constants/strings.js
Original file line number Diff line number Diff line change
Expand Up @@ -345,3 +345,9 @@ export const MANUAL_SIGNING_FAILUTE =

export const PLAN_EXTENSION_CONFIRMATION_QUESTION = (action) =>
`Are you sure you want to ${action} extension?`;

export const PLAN_PASTE_CONFIRMATION_QUESTION = (
sourceAgreement,
destinationAgreement,
) =>
`Are you sure you want to paste content of ${sourceAgreement} and create a new plan ${destinationAgreement}?`;
67 changes: 67 additions & 0 deletions src/providers/ConfrimationModalProvider.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
import {
Dialog,
DialogActions,
DialogContent,
DialogContentText,
DialogTitle,
} from '@material-ui/core';
import React, {
createContext,
useCallback,
useContext,
useRef,
useState,
} from 'react';
import { PrimaryButton } from '../components/common';
const ConfirmationModal = createContext();

export function ConfirmationModalProvider({ children }) {
const [state, setState] = useState({ isOpen: false });
const fn = useRef();

const confirm = useCallback(
(data) => {
return new Promise((resolve) => {
setState({ ...data, isOpen: true });
fn.current = (choice) => {
resolve(choice);
setState({ isOpen: false });
};
});
},
[setState],
);

return (
<ConfirmationModal.Provider value={confirm}>
{children}
<Dialog
open={state.isOpen}
onClose={() => fn.current(false)}
aria-labelledby="alert-dialog-title"
aria-describedby="alert-dialog-description"
>
<DialogTitle id="alert-dialog-title">
{state.titleText || 'Confirm'}
</DialogTitle>
<DialogContent>
<DialogContentText id="alert-dialog-description">
{state.contentText}
</DialogContentText>
</DialogContent>
<DialogActions>
<PrimaryButton inverted onClick={() => fn.current(false)}>
{state.cancelText || 'Cancel'}
</PrimaryButton>
<PrimaryButton onClick={() => fn.current(true)} autoFocus>
{state.confirmText || 'Confirm'}
</PrimaryButton>
</DialogActions>
</Dialog>
</ConfirmationModal.Provider>
);
}

export default function useConfirm() {
return useContext(ConfirmationModal);
}
Loading