-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
17 changed files
with
439 additions
and
793 deletions.
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
194 changes: 14 additions & 180 deletions
194
src/client/src/commons/form/borehole/completion/backfill.js
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 |
---|---|---|
@@ -1,194 +1,28 @@ | ||
import React, { useState, useEffect, useMemo, createRef, useRef } from "react"; | ||
import { useTranslation } from "react-i18next"; | ||
import { | ||
Box, | ||
CircularProgress, | ||
Grid, | ||
Stack, | ||
Tooltip, | ||
Typography, | ||
} from "@mui/material"; | ||
import { AddButton, CompletionCard, CompletionGrid } from "./styledComponents"; | ||
import React from "react"; | ||
import { | ||
getBackfills, | ||
addBackfill, | ||
updateBackfill, | ||
deleteBackfill, | ||
} from "../../../../api/fetchApiV2"; | ||
import { CompletionContentTab } from "./completionContentTab"; | ||
import BackfillInput from "./backfillInput"; | ||
import BackfillDisplay from "./backfillDisplay"; | ||
|
||
const Backfill = ({ isEditable, completionId }) => { | ||
const { t } = useTranslation(); | ||
const mounted = useRef(false); | ||
const [selectedBackfill, setSelectedBackfill] = useState(null); | ||
const [displayedBackfills, setDisplayedBackfills] = useState([]); | ||
const [state, setState] = useState({ | ||
index: 0, | ||
backfills: [], | ||
isLoadingData: true, | ||
}); | ||
|
||
const loadData = index => { | ||
setState({ isLoadingData: true }); | ||
if (completionId && mounted.current) { | ||
getBackfills(completionId).then(response => { | ||
if (response?.length > 0) { | ||
setState({ | ||
index: index, | ||
backfills: response, | ||
isLoadingData: false, | ||
}); | ||
} else { | ||
setState({ | ||
index: 0, | ||
backfills: [], | ||
isLoadingData: false, | ||
}); | ||
} | ||
}); | ||
} else if (completionId === null) { | ||
setState({ | ||
index: 0, | ||
backfills: [], | ||
}); | ||
} | ||
}; | ||
|
||
const handleDataChange = () => { | ||
loadData(state.index); | ||
}; | ||
|
||
useEffect(() => { | ||
mounted.current = true; | ||
loadData(0); | ||
return () => { | ||
mounted.current = false; | ||
}; | ||
// eslint-disable-next-line react-hooks/exhaustive-deps | ||
}, [completionId]); | ||
|
||
useEffect(() => { | ||
setDisplayedBackfills(state.backfills); | ||
}, [state.backfills]); | ||
|
||
// scroll to newly added item | ||
const backfillRefs = useMemo( | ||
() => | ||
Array(displayedBackfills?.length) | ||
.fill(null) | ||
.map(() => createRef(null)), | ||
[displayedBackfills], | ||
); | ||
|
||
useEffect(() => { | ||
if (displayedBackfills?.length > 0) { | ||
const lastBackfillRef = backfillRefs[displayedBackfills?.length - 1]; | ||
if (displayedBackfills[displayedBackfills?.length - 1].id === 0) | ||
lastBackfillRef.current.scrollIntoView({ | ||
behavior: "smooth", | ||
block: "center", | ||
}); | ||
} | ||
}, [displayedBackfills, backfillRefs]); | ||
|
||
return ( | ||
<Stack sx={{ flexGrow: 1 }}> | ||
<Box sx={{ mb: 2 }}> | ||
<Stack direction="row" justifyContent="flex-end" alignItems="center"> | ||
{isEditable && ( | ||
<Tooltip title={t("add")}> | ||
<AddButton | ||
data-cy="add-backfill-button" | ||
onClick={e => { | ||
e.stopPropagation(); | ||
if (!selectedBackfill) { | ||
const tempBackfill = { id: 0 }; | ||
// Check if backfills is iterable | ||
if ( | ||
state.backfills && | ||
Symbol.iterator in Object(state.backfills) | ||
) { | ||
setDisplayedBackfills([...state.backfills, tempBackfill]); | ||
} else { | ||
setDisplayedBackfills([tempBackfill]); | ||
} | ||
setSelectedBackfill(tempBackfill); | ||
} | ||
}}> | ||
{t("addFilling")} | ||
</AddButton> | ||
</Tooltip> | ||
)} | ||
</Stack> | ||
</Box> | ||
<CompletionGrid> | ||
{displayedBackfills?.length > 0 | ||
? displayedBackfills | ||
?.sort((a, b) => a.fromDepthM - b.fromDepthM) | ||
.map((backfill, index) => { | ||
const isSelected = selectedBackfill?.id === backfill.id; | ||
const isTempBackfill = backfill.id === 0; | ||
return ( | ||
<Grid | ||
item | ||
md={12} | ||
lg={12} | ||
xl={6} | ||
key={backfill.id} | ||
ref={backfillRefs[index]}> | ||
{state.backfills ? ( | ||
<CompletionCard key={backfill.id}> | ||
{isEditable && isSelected ? ( | ||
<BackfillInput | ||
backfill={backfill} | ||
setSelectedBackfill={setSelectedBackfill} | ||
completionId={completionId} | ||
updateBackfill={(backfill, data) => { | ||
updateBackfill(backfill, data).then(() => { | ||
handleDataChange(); | ||
}); | ||
}} | ||
addBackfill={data => { | ||
addBackfill(data).then(() => { | ||
handleDataChange(); | ||
}); | ||
}} | ||
/> | ||
) : ( | ||
!isTempBackfill && ( | ||
<BackfillDisplay | ||
backfill={backfill} | ||
selectedBackfill={selectedBackfill} | ||
setSelectedBackfill={setSelectedBackfill} | ||
isEditable={isEditable} | ||
deleteBackfill={backfillId => { | ||
deleteBackfill(backfillId).then(() => { | ||
handleDataChange(); | ||
}); | ||
}} | ||
/> | ||
) | ||
)} | ||
</CompletionCard> | ||
) : ( | ||
<CircularProgress /> | ||
)} | ||
</Grid> | ||
); | ||
}) | ||
: !state.isLoadingData && ( | ||
<Stack | ||
alignItems="center" | ||
justifyContent="center" | ||
sx={{ flexGrow: 1 }}> | ||
<Typography variant="fullPageMessage"> | ||
{t("msgBackfillEmpty")} | ||
</Typography> | ||
</Stack> | ||
)} | ||
</CompletionGrid> | ||
</Stack> | ||
<CompletionContentTab | ||
isEditable={isEditable} | ||
completionId={completionId} | ||
getData={getBackfills} | ||
addData={addBackfill} | ||
updateData={updateBackfill} | ||
deleteData={deleteBackfill} | ||
addLabel="addFilling" | ||
emptyLabel="msgBackfillEmpty" | ||
renderInput={props => <BackfillInput {...props} />} | ||
renderDisplay={props => <BackfillDisplay {...props} />} | ||
/> | ||
); | ||
}; | ||
export default React.memo(Backfill); |
Oops, something went wrong.