Skip to content

Commit

Permalink
Merge Fix instruments
Browse files Browse the repository at this point in the history
Closes #14
  • Loading branch information
MiraGeowerkstatt authored Nov 10, 2022
2 parents 09b625c + 8fe400e commit cac8d1b
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 106 deletions.
10 changes: 0 additions & 10 deletions src/client/cypress/e2e/editor/instrumentation.cy.js
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,6 @@ describe("Instrumentation tests", () => {
casingDropDown.contains("Moonshine Bike");

cy.wait("@casing-layers");
cy.wait(3000);

// Choose second casingLayer
let casingLayerDropDown = cy
Expand Down Expand Up @@ -218,9 +217,6 @@ describe("Instrumentation tests", () => {
.click({ force: true }),
);

cy.wait("@casing-layers");
cy.wait(3000);

casingDropDown.contains("Sunshine Bike");

casingLayerDropDown = cy
Expand All @@ -233,9 +229,6 @@ describe("Instrumentation tests", () => {
// Change of casing should reset casingLayer
casingLayerDropDown.should("not.contain", "Moonshine Bike");

cy.wait("@casing-layers");
cy.wait(3000);

// Dropdown options in casingLayer dropdown have updated
casingLayerDropDown.each((el, index, list) =>
cy
Expand All @@ -246,9 +239,6 @@ describe("Instrumentation tests", () => {
.click({ force: true }),
);

cy.wait("@casing-layers");
cy.wait(3000);

casingLayerDropDown.contains("Sunshine Honey");
cy.contains("a", "Stop editing").click();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,26 @@ import * as Styled from "./styles";
import { Input, Form, Button } from "semantic-ui-react";
import TranslationText from "../../../../../translationText";
import DomainDropdown from "../../../../../domain/dropdown/domainDropdown";
import { patchLayer } from "../../../../../../../api-lib/index";
import { InstrumentAttributes } from "../../data/InstrumentAttributes";
import { useTranslation } from "react-i18next";
import CasingList from "../../../casingList";
import { produce } from "immer";
import { fetchApiV2 } from "../../../../../../../api/fetchApiV2";
import produce from "immer";

const Instrument = props => {
const { index, info, deleting, isEditable, update, casing } = props.data;
const {
index,
info,
deleting,
isEditable,
casing,
instruments,
setInstruments,
} = props.data;

const { t } = useTranslation();
const [instrumentInfo, setInstrumentInfo] = useState({
id: null,
instrument_kind: null,
depth_from: null,
depth_to: null,
notes: null,
instrument_status: null,
instrument_casing_id: null,
instrument_id: null,
});
const [casingLayers, setCasingLayers] = useState([]);
const [instrument, setInstrument] = useState([]);
const [updateAttributeDelay, setUpdateAttributeDelay] = useState({});

async function fetchLayersByProfileId(profileId) {
return await fetchApiV2(`layer?profileId=${profileId}`, "GET");
Expand Down Expand Up @@ -58,8 +54,6 @@ const Instrument = props => {
}, [instrument.instrumentCasingId]);

useEffect(() => {
setInstrumentInfo(info);

// fetch layer
fetchLayerById(info.id).then(response => {
setInstrument(response);
Expand All @@ -68,71 +62,38 @@ const Instrument = props => {
fetchCasingLayers();
}, [fetchCasingLayers, info]);

const updateChange = (attribute, value, isNumber = false) => {
const updateInstrument = (attribute, value) => {
if (!isEditable) {
alert(t("common:errorStartEditing"));
return;
}

// refresh casing layers if casing changes
if (attribute === "instrument_casing_id") {
setCasingLayers([]);
updateLayer({ ...instrument, instrumentCasingLayerId: null });
instrument.instrumentCasingId && fetchCasingLayers();
let updatedInstrument;
// reset casing layer if casing changes.
if (attribute === "instrumentCasingId") {
updatedInstrument = {
...instrument,
[attribute]: value,
instrumentCasingLayerId: null,
};
} else {
updatedInstrument = {
...instrument,
[attribute]: value,
};
}

setInstrumentInfo(
produce(draft => {
draft[attribute] = value;
setInstrument(updatedInstrument);
setInstruments(
produce(instruments, draft => {
const index = draft.findIndex(d => d.id === updatedInstrument.id);
if (index >= 0) {
draft[index].instrument_casing_id =
updatedInstrument.instrumentCasingId;
}
}),
);

if (isNumber) {
if (value === null) {
patch(attribute, value);
} else if (/^-?\d*[.,]?\d*$/.test(value)) {
patch(attribute, parseInt(value));
}
} else {
patch(attribute, value);
}
};

const updateInstrument = (attribute, value) => {
if (!isEditable) {
alert(t("common:errorStartEditing"));
return;
}

setInstrument({ ...instrument, [attribute]: value });
updateLayer({ ...instrument, [attribute]: value });
};

const patch = (attribute, value) => {
clearTimeout(updateAttributeDelay?.[attribute]);

let setDelay = {
[attribute]: setTimeout(() => {
patchLayer(info?.id, attribute, value)
.then(response => {
if (response.data.success) {
if (attribute === "instrument_casing_id") {
update();
}
} else {
alert(response.data.message);
window.location.reload();
}
})
.catch(function (error) {
console.error(error);
});
}, 500),
};

Promise.resolve().then(() => {
setUpdateAttributeDelay(setDelay);
});
updateLayer(updatedInstrument);
};

return (
Expand All @@ -149,30 +110,30 @@ const Instrument = props => {
{item.type === "Input" && (
<Styled.AttributesItem data-cy={item.label}>
<Input
type={item.isNumber ? "number" : "text"}
autoCapitalize="off"
autoComplete="off"
autoCorrect="off"
onChange={e => {
updateChange(
updateInstrument(
item.value,
e.target.value === "" ? null : e.target.value,
item?.isNumber,
);
}}
spellCheck="false"
style={{ width: "100%" }}
value={instrumentInfo?.[item.value] ?? ""}
value={instrument?.[item.value] ?? ""}
/>
</Styled.AttributesItem>
)}
{item.type === "Dropdown" && (
<Styled.AttributesItem data-cy={item.label}>
<DomainDropdown
multiple={item.multiple}
onSelected={e => updateChange(item.value, e.id, false)}
onSelected={e => updateInstrument(item.value, e.id)}
schema={item.schema}
search={item.search}
selected={instrumentInfo?.[item.value] ?? null}
selected={instrument?.[item.value] ?? null}
/>
</Styled.AttributesItem>
)}
Expand All @@ -181,8 +142,8 @@ const Instrument = props => {
<Styled.AttributesItem data-cy={item.label}>
<CasingList
data={casing}
dropDownValue={instrumentInfo?.[item.value] ?? null}
handleCasing={updateChange}
dropDownValue={instrument?.[item.value] ?? null}
handleCasing={updateInstrument}
ItemValue={item.value}
/>
</Styled.AttributesItem>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ export const InstrumentAttributes = [
id: 0,
type: "Input",
label: "fromMeters",
value: "depth_from",
value: "fromDepth",
require: true,
isNumber: true,
isVisible: true,
Expand All @@ -12,7 +12,7 @@ export const InstrumentAttributes = [
id: 1,
type: "Input",
label: "toMeters",
value: "depth_to",
value: "toDepth",
require: true,
isNumber: true,
isVisible: true,
Expand All @@ -21,15 +21,15 @@ export const InstrumentAttributes = [
id: 2,
type: "Input",
label: "instrumentId",
value: "instrument_id",
value: "instrument",
require: true,
isVisible: true,
},
{
id: 3,
type: "Dropdown",
label: "kindInstrument",
value: "instrument_kind",
value: "instrumentKindId",
require: true,
schema: "inst100",
multiple: false,
Expand All @@ -40,7 +40,7 @@ export const InstrumentAttributes = [
id: 4,
type: "Dropdown",
label: "statusInstrument",
value: "instrument_status",
value: "instrumentStatusId",
require: true,
schema: "inst101",
multiple: false,
Expand All @@ -51,7 +51,7 @@ export const InstrumentAttributes = [
id: 5,
type: "CasingDropdown",
label: "casingName",
value: "instrument_casing_id",
value: "instrumentCasingId",
require: true,
multiple: false,
search: false,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,17 +49,17 @@ const ProfileInstrument = props => {
getInstrumentProfile();
}, [getInstrumentProfile]);

const setData = useCallback(
instrumentID => {
getData(instrumentID).then(response => {
setInstruments(response);
setHasInstrumentWithoutCasing(
response.some(i => i.instrument_casing_id === 0),
);
});
},
[setHasInstrumentWithoutCasing],
);
const setData = useCallback(instrumentID => {
getData(instrumentID).then(response => {
setInstruments(response);
});
}, []);

useEffect(() => {
setHasInstrumentWithoutCasing(
instruments.some(i => i.instrument_casing_id === 0),
);
}, [instruments, setHasInstrumentWithoutCasing]);

useEffect(() => {
if (instrumentProfileId) {
Expand Down Expand Up @@ -131,6 +131,8 @@ const ProfileInstrument = props => {
deleting: deleteLayer,
onUpdated,
isEditable,
instruments,
setInstruments,
update: () => {
setReload(prevState => prevState + 1);
},
Expand Down

0 comments on commit cac8d1b

Please sign in to comment.