Skip to content

Commit

Permalink
Merge pull request #238 from orppst/231-save-observation-not-working
Browse files Browse the repository at this point in the history
231 save observation not working
  • Loading branch information
AllanEngland authored Aug 16, 2024
2 parents 1004d26 + 55b2e94 commit 52e0590
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ export interface ObservationFormValues {
observationId: number | undefined,
observationType: ObservationType,
calibrationUse: CalibrationTargetIntendedUse | undefined,
targetDBId: number[] | undefined,
targetDBIds: number[] | undefined,
techGoalId: number | undefined,
fieldId: string | undefined, //string for Select to show existing value in edit-form
timingWindows: TimingWindowGui[],
Expand Down Expand Up @@ -101,15 +101,15 @@ export default function ObservationEditGroup(
observationId: props.observation?._id, //required for deletion of timing windows
observationType: observationType,
calibrationUse: calibrationUse,
targetDBId: props.observation?.target! as number[],
targetDBIds: props.selectedTargets, //check this is working as expected
techGoalId: props.observation?.technicalGoal?._id,
fieldId: props.observation?.field?._id ? String(props.observation?.field?._id) : undefined,
timingWindows: initialTimingWindows
},

validate: {
// targetDBId: (value: number | undefined | string ) =>
// (value === undefined ? 'Please select a target' : null),
targetDBIds: (value: number[] | undefined ) =>
(value === undefined || value.length == 0 ? 'Please select at least one target' : null),
techGoalId: (value: number | undefined | string) =>
(value === undefined ? 'Please select a technical goal' : null),
fieldId: (value: string | undefined) =>
Expand Down Expand Up @@ -140,7 +140,7 @@ export default function ObservationEditGroup(
//Creating new observation
let targetList: Target[] = [];

form.values.targetDBId?.map((thisTarget) =>{
form.values.targetDBIds?.map((thisTarget) =>{
targetList.push({
"@type": "proposal:CelestialTarget",
"_id": thisTarget
Expand Down Expand Up @@ -225,11 +225,10 @@ export default function ObservationEditGroup(
}
//else do nothing
})

if (form.isDirty('targetDBId')) {
if (form.isDirty('targetDBIds')) {
let body: Target[] = [];

form.values.targetDBId?.map((thisTarget) =>{
form.values.targetDBIds?.map((thisTarget) =>{
body.push({
"@type": "proposal:CelestialTarget",
"_id": thisTarget
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {PanelFrame, PanelHeader} from "../../commonPanel/appearance.tsx";
*/
export type ObservationProps = {
observation: Observation | undefined,
selectedTargets: number[] | undefined,
closeModal?: () => void
}

Expand Down Expand Up @@ -202,7 +203,7 @@ function Observations() {
<Space h={"xl"}/>
<Grid>
<Grid.Col span={10}></Grid.Col>
<ObservationEditModal observation={undefined}/>
<ObservationEditModal observation={undefined} selectedTargets={undefined}/>
</Grid>
</PanelFrame>
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import {useQueryClient} from "@tanstack/react-query";
import getErrorMessage from "src/errorHandling/getErrorMessage.tsx";
import CloneButton from "src/commonButtons/clone.tsx";
import DeleteButton from "src/commonButtons/delete.tsx";
import { ReactElement } from 'react';
import {ReactElement, useEffect} from 'react';
import {notifyError} from "../../commonPanel/notifications.tsx";

export type ObservationId = {id: number}
Expand All @@ -43,6 +43,7 @@ export default function ObservationRow(observationId: ObservationId): ReactEleme
const GRAY = theme.colors.gray[6];

const { selectedProposalCode} = useParams();
let observationTargets: number[] = [];

const {
data: observation,
Expand All @@ -61,6 +62,16 @@ export default function ObservationRow(observationId: ObservationId): ReactEleme
return <pre>{getErrorMessage(observationError)}</pre>
}

useEffect(() => {
if(observation?.target?.length != undefined
&& observation.target.length > 0 ) {
observationTargets.splice(0, observationTargets.length);
observation.target.map((thisTarget) => {
observationTargets.push(thisTarget._id!)
})
}
}, [observation]);

/**
* handles the deletion of an observation.
*/
Expand Down Expand Up @@ -243,7 +254,7 @@ export default function ObservationRow(observationId: ObservationId): ReactEleme
<Group position={"right"}>
{
observationLoading ? 'Loading...' :
<ObservationEditModal observation={observation}/>
<ObservationEditModal observation={observation} selectedTargets={observationTargets}/>
}
<CloneButton toolTipLabel={"clone"}
onClick={confirmClone} />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ export default function TargetTypeForm (
= useState<{value: string, label: string}[]>([])

const {
data: targets ,
data: targets,
error: targetListError,
isLoading: targetsLoading } =
useProposalResourceGetTargets({
Expand Down Expand Up @@ -141,8 +141,8 @@ export default function TargetTypeForm (
<Container fluid>
{/* only present the message about selecting a target
when not selected */}
{form.values.targetDBId === undefined ||
form.values.targetDBId.length == 0
{form.values.targetDBIds === undefined ||
form.values.targetDBIds.length == 0
? <p style={{color:theme.colors.yellow[ERROR_YELLOW]}}>
Please select a target.
</p>
Expand All @@ -158,24 +158,19 @@ export default function TargetTypeForm (
showButtons={false}
isLoading={false}
boundTargets={[]}
selectedTargets={[
form.values.targetDBId != undefined ?
form.values.targetDBId[0] :
0]}
setSelectTarget={(value: number) => {
const index = form.values.targetDBId?.indexOf(value);
console.log("index: " + index + " length= " + form.values.targetDBId?.length);
selectedTargets={form.values.targetDBIds}
setSelectedTargetFunction={(value: number) => {
const index = form.values.targetDBIds?.indexOf(value);
if(index === undefined || index == -1) {
if(form.values.targetDBId == undefined) {
form.setFieldValue('targetDBId', [value]);
if(form.values.targetDBIds === undefined) {
form.setFieldValue('targetDBIds', [value]);
} else {
const newTargets = form.values.targetDBId;
newTargets.push(value);
form.setFieldValue('targetDBId', newTargets);
form.values.targetDBIds.push(value);
}
} else {
form.values.targetDBId?.splice(index, 1);
form.values.targetDBIds?.splice(index, 1);
}
form.setDirty({'targetDBIds': true});
}}
/>
</Table.ScrollContainer>
Expand Down
18 changes: 5 additions & 13 deletions src/main/webui/src/ProposalEditorView/targets/TargetTable.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ function TargetTableHeader(props: TargetTableProps): ReactElement {
<Table.Th>RA</Table.Th>
<Table.Th>Dec</Table.Th>
<Table.Th>Epoch</Table.Th>
{ props.showButtons ?
<Table.Th></Table.Th>
: null}
{ props.showButtons && <Table.Th></Table.Th> }
</Table.Tr>
</Table.Thead>
);
Expand Down Expand Up @@ -129,20 +127,14 @@ function TargetTableRow(props: TargetProps): ReactElement {
* @constructor
*/
const RowSelector = (targetId: number | undefined): void => {
console.debug(`row ${targetId} was selected`);
// handle not having a selection method
if (!props.setSelectTarget) {
if (!props.setSelectedTargetFunction) {
return;
}

// handle selection.
//FIXME this is wrong
if(props.selectedTargets !== undefined && targetId !== undefined) {
if ( props.selectedTargets.includes(targetId) ) {
props.setSelectTarget(targetId);
} else {
props.setSelectTarget(targetId);
}
if(targetId !== undefined) {
props.setSelectedTargetFunction(targetId);
}
}

Expand Down Expand Up @@ -255,7 +247,7 @@ export function TargetTable(props: TargetTableProps): ReactElement {
showButtons={props.showButtons}
boundTargets={props.boundTargets}
selectedTargets={props.selectedTargets}
setSelectTarget={props.setSelectTarget}
setSelectedTargetFunction={props.setSelectedTargetFunction}
/>)
})
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/webui/src/ProposalEditorView/targets/targetProps.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export type TargetProps = {
key: string,
boundTargets: (number | undefined)[] | undefined,
selectedTargets: (number)[] | undefined,
setSelectTarget?: (value: number) => void,
setSelectedTargetFunction?: (value: number) => void,
}

/**
Expand All @@ -46,5 +46,5 @@ export type TargetTableProps = {
showButtons: boolean,
boundTargets: (number | undefined)[] | undefined,
selectedTargets: (number)[] | undefined,
setSelectTarget?: (value: number) => void,
setSelectedTargetFunction?: (value: number) => void,
}

0 comments on commit 52e0590

Please sign in to comment.