Skip to content

Commit d5cb7c4

Browse files
authored
Merge pull request #171 from don-aot/CONDITIONS-task#96
CR-96:Changes to '+ Add Document' button
2 parents a4ff996 + f2a5284 commit d5cb7c4

File tree

3 files changed

+98
-14
lines changed

3 files changed

+98
-14
lines changed

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

Lines changed: 64 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
import { useState, useEffect } from "react";
22
import { BCDesignTokens } from "epic.theme";
3-
import { AllDocumentModel, DocumentStatus } from "@/models/Document";
3+
import { AllDocumentModel, DocumentStatus, DocumentTypeModel } from "@/models/Document";
44
import { Box, styled, Stack, Typography, Grid } from "@mui/material";
55
import { ContentBoxSkeleton } from "../Shared/ContentBox/ContentBoxSkeleton";
66
import { ContentBox } from "../Shared/ContentBox";
77
import DocumentTable from "./DocumentTable";
88
import DocumentStatusChip from "../Projects/DocumentStatusChip";
99
import LayersOutlinedIcon from '@mui/icons-material/LayersOutlined';
10+
import AddIcon from '@mui/icons-material/Add';
11+
import LoadingButton from "../Shared/Buttons/LoadingButton";
12+
import { CreateDocumentModal } from "../Projects/CreateDocumentModal";
13+
import { ProjectModel } from "@/models/Project";
1014

1115
export const CardInnerBox = styled(Box)({
1216
display: "flex",
@@ -21,11 +25,25 @@ type DocumentsParam = {
2125
documents?: AllDocumentModel[];
2226
projectName: string;
2327
projectId: string;
28+
categoryId: number;
2429
documentLabel: string;
30+
project?: ProjectModel;
31+
documentType?: DocumentTypeModel[];
2532
};
2633

27-
export const Documents = ({ projectName, projectId, documentLabel, documents }: DocumentsParam) => {
34+
export const Documents = ({ projectName, projectId, categoryId, documentLabel, documents, project, documentType }: DocumentsParam) => {
2835
const [isAllApproved, setIsAllApproved] = useState<boolean | null>(false);
36+
const [openModal, setOpenModal] = useState(false);
37+
const [isOpeningModal, setIsOpeningModal] = useState(false);
38+
39+
const handleOpenAddDocument = () => {
40+
setIsOpeningModal(true);
41+
setOpenModal(true);
42+
};
43+
44+
const handleCloseAddDocument = () => {
45+
setOpenModal(false);
46+
};
2947

3048
useEffect(() => {
3149
if (documents && documents.length > 0) {
@@ -67,7 +85,7 @@ export const Documents = ({ projectName, projectId, documentLabel, documents }:
6785
}}
6886
>
6987
<Grid container direction="row" paddingBottom={3}>
70-
<Grid item xs={12}>
88+
<Grid item xs={6}>
7189
<Box
7290
sx={{
7391
px: 2.5,
@@ -84,6 +102,38 @@ export const Documents = ({ projectName, projectId, documentLabel, documents }:
84102
</Box>
85103
</Box>
86104
</Grid>
105+
<Grid item xs={6} textAlign="right" sx={{ display: "flex", alignItems: "center", justifyContent: "flex-end", gap: 2 }}>
106+
{project && documentType && (
107+
<LoadingButton
108+
variant="contained"
109+
onClick={handleOpenAddDocument}
110+
loading={isOpeningModal}
111+
sx={{
112+
display: "flex",
113+
padding: "8px 22px 7px 15px",
114+
alignItems: "center",
115+
gap: "14.178px",
116+
height: "70%",
117+
borderRadius: "4px",
118+
border: "2px solid #353433",
119+
backgroundColor: "#013366",
120+
color: "#FFF",
121+
textAlign: "center",
122+
fontFamily: '"BC Sans"',
123+
fontSize: "16px",
124+
fontStyle: "normal",
125+
fontWeight: 400,
126+
lineHeight: "27.008px",
127+
"&:hover": {
128+
backgroundColor: "#002a52",
129+
border: "2px solid #353433",
130+
},
131+
}}
132+
>
133+
<AddIcon fontSize="small" /> Add Document
134+
</LoadingButton>
135+
)}
136+
</Grid>
87137
</Grid>
88138
<Box height={"100%"} px={BCDesignTokens.layoutPaddingXsmall}>
89139
<CardInnerBox
@@ -98,6 +148,17 @@ export const Documents = ({ projectName, projectId, documentLabel, documents }:
98148
</Typography>
99149
</Box>
100150
</ContentBox>
151+
{project && documentType && (
152+
<CreateDocumentModal
153+
open={openModal}
154+
onClose={handleCloseAddDocument}
155+
documentType={documentType}
156+
projectArray={[project]}
157+
preselectedProject={project}
158+
restrictToCategoryId={categoryId}
159+
onTransitionEnd={() => setIsOpeningModal(false)}
160+
/>
161+
)}
101162
</Stack>
102163
);
103164
};

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 & 4 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,10 +76,13 @@ function DocumentPage() {
7076
</Then>
7177
<Else>
7278
<Documents
73-
projectName = {allDocuments?.project_name || ""}
74-
projectId = {projectId}
75-
documentLabel = {allDocuments?.document_category || ""}
79+
projectName={allDocuments?.project_name || ""}
80+
projectId={projectId}
81+
categoryId={categoryId}
82+
documentLabel={allDocuments?.document_category || ""}
7683
documents={allDocuments?.documents}
84+
project={project}
85+
documentType={documentTypeData}
7786
/>
7887
</Else>
7988
</If>

0 commit comments

Comments
 (0)