Skip to content

Commit

Permalink
Unify CompletionContent (#984)
Browse files Browse the repository at this point in the history
  • Loading branch information
tschumpr authored Jan 26, 2024
2 parents ebc3053 + 6b805fa commit 5a703a5
Show file tree
Hide file tree
Showing 17 changed files with 439 additions and 793 deletions.
2 changes: 1 addition & 1 deletion src/client/cypress/e2e/editor/backfill-v2.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("Backfill crud tests", () => {
cy.wait("@backfill_GET");

// add new backfill card
cy.get('[data-cy="add-backfill-button"]').click({ force: true });
cy.get('[data-cy="addFilling-button"]').click({ force: true });
cy.wait("@codelist_GET");

// fill out form
Expand Down
4 changes: 2 additions & 2 deletions src/client/cypress/e2e/editor/casing-v2.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ describe("Casing crud tests", () => {
cy.wait("@casing_GET");

// create casing
cy.get('[data-cy="add-casing-button"]').click();
cy.get('[data-cy="addCasing-button"]').click();
cy.wait("@codelist_GET");

setInput("name", "casing-1");
Expand Down Expand Up @@ -93,7 +93,7 @@ describe("Casing crud tests", () => {
cy.get("[data-cy=completion-content-header-tab-Instrumentation]").click();
cy.wait("@instrumentation_GET");

cy.get('[data-cy="add-instrumentation-button"]').click({ force: true });
cy.get('[data-cy="addInstrument-button"]').click({ force: true });
cy.wait("@casing_GET");

setInput("notes", "Lorem.");
Expand Down
4 changes: 2 additions & 2 deletions src/client/cypress/e2e/editor/instrumentation-v2.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ describe("Instrumentation crud tests", () => {
cy.get("[data-cy=completion-content-header-tab-Casing]").click();
cy.wait("@casing_GET");

cy.get('[data-cy="add-casing-button"]').click({ force: true });
cy.get('[data-cy="addCasing-button"]').click({ force: true });
cy.wait("@codelist_GET");

setInput("name", "casing-1");
Expand All @@ -67,7 +67,7 @@ describe("Instrumentation crud tests", () => {
cy.wait("@instrumentation_GET");

// create instrumentation
cy.get('[data-cy="add-instrumentation-button"]').click({ force: true });
cy.get('[data-cy="addInstrument-button"]').click({ force: true });
cy.wait("@casing_GET");

// fill out form
Expand Down
71 changes: 14 additions & 57 deletions src/client/src/api/fetchApiV2.js
Original file line number Diff line number Diff line change
Expand Up @@ -729,66 +729,23 @@ export const useHydrotestMutations = () => {
};
};

export const instrumentationQueryKey = "instrumentations";
export const getInstrumentation = async completionId => {
return await fetchApiV2(
`instrumentation?completionId=${completionId}`,
"GET",
);
};

export const useInstrumentations = completionId =>
useQuery({
queryKey: [instrumentationQueryKey, completionId],
queryFn: async () => {
return await fetchApiV2(
`instrumentation?completionId=${completionId}`,
"GET",
);
},
});
export const addInstrumentation = async instrumentation => {
return await fetchApiV2("instrumentation", "POST", instrumentation);
};

export const useInstrumentationMutations = () => {
const queryClient = useQueryClient();
const useAddInstrumentations = useMutation(
async instrumentation => {
return await fetchApiV2("instrumentation", "POST", instrumentation);
},
{
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: [instrumentationQueryKey],
});
},
},
);
const useUpdateInstrumentations = useMutation(
async instrumentation => {
return await fetchApiV2("instrumentation", "PUT", instrumentation);
},
{
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: [instrumentationQueryKey],
});
},
},
);
const useDeleteInstrumentations = useMutation(
async instrumentationId => {
return await fetchApiV2(
`instrumentation?id=${instrumentationId}`,
"DELETE",
);
},
{
onSuccess: () => {
queryClient.invalidateQueries({
queryKey: [instrumentationQueryKey],
});
},
},
);
export const updateInstrumentation = async instrumentation => {
return await fetchApiV2("instrumentation", "PUT", instrumentation);
};

return {
add: useAddInstrumentations,
update: useUpdateInstrumentations,
delete: useDeleteInstrumentations,
};
export const deleteInstrumentation = async id => {
return await fetchApiV2(`instrumentation?id=${id}`, "DELETE");
};

export const getBackfills = async completionId => {
Expand Down
194 changes: 14 additions & 180 deletions src/client/src/commons/form/borehole/completion/backfill.js
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);
Loading

0 comments on commit 5a703a5

Please sign in to comment.