Skip to content

Commit 23415b7

Browse files
committed
CR-96:Changes to '+ Add Document' button
1 parent f4305af commit 23415b7

File tree

3 files changed

+97
-17
lines changed

3 files changed

+97
-17
lines changed

condition-web/src/components/Documents/index.tsx

Lines changed: 63 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { useState, useEffect } from "react";
22
import { useNavigate } from "@tanstack/react-router";
33
import { BCDesignTokens } from "epic.theme";
4-
import { AllDocumentModel, DocumentStatus } from "@/models/Document";
4+
import { AllDocumentModel, DocumentStatus, DocumentTypeModel } from "@/models/Document";
55
import { Box, styled, Stack, FormControlLabel, Typography, Switch, Grid } from "@mui/material";
66
import { ContentBoxSkeleton } from "../Shared/ContentBox/ContentBoxSkeleton";
77
import { ContentBox } from "../Shared/ContentBox";
88
import DocumentTable from "./DocumentTable";
99
import DocumentStatusChip from "../Projects/DocumentStatusChip";
1010
import LayersOutlinedIcon from '@mui/icons-material/LayersOutlined';
11+
import AddIcon from '@mui/icons-material/Add';
12+
import LoadingButton from "../Shared/Buttons/LoadingButton";
13+
import { CreateDocumentModal } from "../Projects/CreateDocumentModal";
14+
import { ProjectModel } from "@/models/Project";
1115

1216
export const CardInnerBox = styled(Box)({
1317
display: "flex",
@@ -24,13 +28,26 @@ type DocumentsParam = {
2428
projectId: string;
2529
categoryId: number;
2630
documentLabel: string;
31+
project?: ProjectModel;
32+
documentType?: DocumentTypeModel[];
2733
};
2834

29-
export const Documents = ({ projectName, projectId, categoryId, documentLabel, documents }: DocumentsParam) => {
35+
export const Documents = ({ projectName, projectId, categoryId, documentLabel, documents, project, documentType }: DocumentsParam) => {
3036
const navigate = useNavigate();
3137
const [isToggled, setIsToggled] = useState(false);
3238
const [isToggleEnabled, setIsToggleEnabled] = useState<boolean | null>(false);
3339
const [isAllApproved, setIsAllApproved] = useState<boolean | null>(false);
40+
const [openModal, setOpenModal] = useState(false);
41+
const [isOpeningModal, setIsOpeningModal] = useState(false);
42+
43+
const handleOpenAddDocument = () => {
44+
setIsOpeningModal(true);
45+
setOpenModal(true);
46+
};
47+
48+
const handleCloseAddDocument = () => {
49+
setOpenModal(false);
50+
};
3451

3552
const handleToggle = (event: React.ChangeEvent<HTMLInputElement>) => {
3653
const checked = event.target.checked;
@@ -108,13 +125,43 @@ export const Documents = ({ projectName, projectId, categoryId, documentLabel, d
108125
</Box>
109126
</Box>
110127
</Grid>
111-
<Grid item xs={6} textAlign="right">
128+
<Grid item xs={6} textAlign="right" sx={{ display: "flex", alignItems: "center", justifyContent: "flex-end", gap: 2 }}>
129+
{project && documentType && (
130+
<LoadingButton
131+
variant="contained"
132+
onClick={handleOpenAddDocument}
133+
loading={isOpeningModal}
134+
sx={{
135+
display: "flex",
136+
padding: "8px 22px 7px 15px",
137+
alignItems: "center",
138+
gap: "14.178px",
139+
height: "70%",
140+
borderRadius: "4px",
141+
border: "2px solid #353433",
142+
backgroundColor: "#013366",
143+
color: "#FFF",
144+
textAlign: "center",
145+
fontFamily: '"BC Sans"',
146+
fontSize: "16px",
147+
fontStyle: "normal",
148+
fontWeight: 400,
149+
lineHeight: "27.008px",
150+
"&:hover": {
151+
backgroundColor: "#002a52",
152+
border: "2px solid #353433",
153+
},
154+
}}
155+
>
156+
<AddIcon fontSize="small" /> Add Document
157+
</LoadingButton>
158+
)}
112159
<FormControlLabel
113160
control={
114161
<Switch
115-
disabled={!isToggleEnabled} // Disable the switch based on the isToggleEnabled state
162+
disabled={!isToggleEnabled}
116163
checked={isToggled}
117-
onChange={handleToggle} // Add onChange handler to toggle and navigate
164+
onChange={handleToggle}
118165
/>
119166
}
120167
label="View Consolidated Conditions"
@@ -135,6 +182,17 @@ export const Documents = ({ projectName, projectId, categoryId, documentLabel, d
135182
</Typography>
136183
</Box>
137184
</ContentBox>
185+
{project && documentType && (
186+
<CreateDocumentModal
187+
open={openModal}
188+
onClose={handleCloseAddDocument}
189+
documentType={documentType}
190+
projectArray={[project]}
191+
preselectedProject={project}
192+
restrictToCategoryId={categoryId}
193+
onTransitionEnd={() => setIsOpeningModal(false)}
194+
/>
195+
)}
138196
</Stack>
139197
);
140198
};

condition-web/src/components/Projects/CreateDocumentModal.tsx

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@ type CreateDocumentModalProps = {
3838
documentType: DocumentTypeModel[];
3939
projectArray: ProjectModel[];
4040
onTransitionEnd?: () => void;
41+
preselectedProject?: ProjectModel;
42+
restrictToCategoryId?: number;
4143
};
4244

4345
export const CreateDocumentModal = ({
@@ -46,11 +48,13 @@ export const CreateDocumentModal = ({
4648
documentType,
4749
projectArray,
4850
onTransitionEnd,
51+
preselectedProject,
52+
restrictToCategoryId,
4953
}: CreateDocumentModalProps) => {
5054
const queryClient = useQueryClient();
5155
const navigate = useNavigate();
5256
const [formState, setFormState] = useState({
53-
selectedProject: null as ProjectModel | null,
57+
selectedProject: preselectedProject || null as ProjectModel | null,
5458
selectedDocumentType: null as number | null,
5559
selectedDocumentId: null as string | null,
5660
selectedDocumentLabel: null as string | null,
@@ -88,24 +92,28 @@ export const CreateDocumentModal = ({
8892
};
8993

9094
const filteredDocumentTypes = (documentType || []).filter((type) => {
95+
if (restrictToCategoryId && type.document_category_id !== restrictToCategoryId) {
96+
return false;
97+
}
98+
9199
if (!formState.selectedProject || !formState.selectedProject.documents) return true;
92-
100+
93101
const hasCertificate = formState.selectedProject.documents.some((document) =>
94102
document.document_types.includes(DocumentType.Certificate)
95103
);
96-
104+
97105
const hasExemptionOrder = formState.selectedProject.documents.some((document) =>
98106
document.document_types.includes(DocumentType.ExemptionOrder)
99107
);
100-
108+
101109
if (type.document_type === DocumentType.Certificate) {
102110
return !hasExemptionOrder; // Exclude Certificate if ExemptionOrder is present
103111
}
104-
112+
105113
if (type.document_type === DocumentType.ExemptionOrder) {
106114
return !hasCertificate; // Exclude ExemptionOrder if Certificate is present
107115
}
108-
116+
109117
return true;
110118
});
111119

@@ -211,7 +219,7 @@ export const CreateDocumentModal = ({
211219

212220
const resetForm = () => {
213221
setFormState({
214-
selectedProject: null,
222+
selectedProject: preselectedProject || null,
215223
selectedDocumentType: null,
216224
selectedDocumentId: null,
217225
selectedDocumentLabel: null,
@@ -269,6 +277,11 @@ export const CreateDocumentModal = ({
269277
<Typography variant="body1" marginBottom={"2px"}>
270278
Which project does this document belong to?
271279
</Typography>
280+
{preselectedProject ? (
281+
<Typography variant="body1" fontWeight="bold" marginBottom={"8px"}>
282+
{preselectedProject.project_name}
283+
</Typography>
284+
) : (
272285
<Autocomplete
273286
id="project-selector"
274287
data-testid="project-selector"
@@ -295,6 +308,7 @@ export const CreateDocumentModal = ({
295308
setErrors((prev) => ({ ...prev, selectedProject: false }));
296309
}}
297310
/>
311+
)}
298312
{/* Document Type Selector */}
299313
<Typography variant="body1">
300314
What type of document are you adding?

condition-web/src/routes/_authenticated/_dashboard/documents/project/$projectId/document-category/$categoryId/index.tsx

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import { Else, If, Then } from "react-if";
33
import { PageGrid } from "@/components/Shared/PageGrid";
44
import { Grid } from "@mui/material";
55
import { createFileRoute, Navigate, useParams } from "@tanstack/react-router";
6-
import { useGetDocuments } from "@/hooks/api/useDocuments";
6+
import { useGetDocuments, useGetDocumentType } from "@/hooks/api/useDocuments";
7+
import { useGetProjects } from "@/hooks/api/useProjects";
78
import { Documents, DocumentsSkeleton } from "@/components/Documents";
89
import { notify } from "@/components/Shared/Snackbar/snackbarStore";
910
import { useBreadCrumb } from "@/components/Shared/layout/SideNav/breadCrumbStore";
@@ -32,6 +33,11 @@ function DocumentPage() {
3233
isError: isAmendmentsError
3334
} = useGetDocuments(projectId, categoryId);
3435

36+
const { data: projectsData } = useGetProjects();
37+
const { data: documentTypeData } = useGetDocumentType();
38+
39+
const project = projectsData?.find((p) => p.project_id === projectId);
40+
3541
useEffect(() => {
3642
if (isAmendmentsError) {
3743
notify.error("Failed to load documents");
@@ -70,11 +76,13 @@ function DocumentPage() {
7076
</Then>
7177
<Else>
7278
<Documents
73-
projectName = {allDocuments?.project_name || ""}
74-
projectId = {projectId}
75-
categoryId = {categoryId}
76-
documentLabel = {allDocuments?.document_category || ""}
79+
projectName={allDocuments?.project_name || ""}
80+
projectId={projectId}
81+
categoryId={categoryId}
82+
documentLabel={allDocuments?.document_category || ""}
7783
documents={allDocuments?.documents}
84+
project={project}
85+
documentType={documentTypeData}
7886
/>
7987
</Else>
8088
</If>

0 commit comments

Comments
 (0)