Skip to content

Commit

Permalink
Merge pull request #349 from MetaCell/feature/SCKAN-339
Browse files Browse the repository at this point in the history
Feature/sckan 339
  • Loading branch information
ddelpiano authored Oct 31, 2024
2 parents 0d50d06 + dfe9c18 commit 2731765
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 47 deletions.
18 changes: 8 additions & 10 deletions frontend/src/Pages/SentenceDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import {
InputOutlined,
} from "@mui/icons-material";
import { checkSentenceOwnership, getOwnershipAlertMessage} from "../helpers/ownershipAlert";
import {ChangeRequestStatus} from "../helpers/settings";

const { bodyBgColor, darkBlue } = vars;

Expand Down Expand Up @@ -121,7 +122,6 @@ const SentencesDetails = () => {
navigate("/");
}
} catch (error) {
console.error("Error fetching the next sentence:", error);
setIsLoading(false);
}
};
Expand All @@ -132,31 +132,29 @@ const SentencesDetails = () => {
.doTransition(sentence, transition)
.then((sentence: Sentence) => fetchNextSentence(sentence));
};

const onAddNewStatement = () => {
return checkSentenceOwnership(
sentence.id,
async () => {
setConnectivityStatements([
// @ts-ignore
...connectivityStatements,
setConnectivityStatements((prev: any = []) => [
...prev,
{
sentence_id: sentence.id,
sentence: sentence.id,
knowledge_statement: "",
sex: null,
phenotype: null,
species: [],
dois: [],
},
]);
},
() => {
console.log("Adding a new statement canceled due to ownership issues.");
return ChangeRequestStatus.CANCELLED;
},
getOwnershipAlertMessage // message to show when ownership needs to be reassigned
getOwnershipAlertMessage
);
};

const handleMenuItemClick = (
event: React.MouseEvent<HTMLLIElement, MouseEvent>,
index: number,
Expand Down
5 changes: 3 additions & 2 deletions frontend/src/Pages/StatementDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import Stack from "@mui/material/Stack";
import {ViaIcon, DestinationIcon, OriginIcon} from "../components/icons";
import {CircularProgress} from "@mui/material";
import {checkOwnership, getOwnershipAlertMessage} from "../helpers/ownershipAlert";
import {ChangeRequestStatus} from "../helpers/settings";

const StatementDetails = () => {
const {statementId} = useParams();
Expand Down Expand Up @@ -79,8 +80,8 @@ const StatementDetails = () => {
return result;
},
() => {
console.log("Transition canceled due to ownership issues.");
},
return ChangeRequestStatus.CANCELLED;
},
getOwnershipAlertMessage // message to show when ownership needs to be reassigned
);
}
Expand Down
9 changes: 5 additions & 4 deletions frontend/src/components/Forms/ProvenanceForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import provenanceService from '../../services/ProvenanceService'
import {Provenance} from "../../apiclient/backend";
import TextfieldWithChips from "../Widgets/TextfieldWithChips";
import {checkOwnership, getOwnershipAlertMessage} from "../../helpers/ownershipAlert";
import {ChangeRequestStatus} from "../../helpers/settings";

const ProvenancesForm = (props: any) => {
const { provenancesData, setter, extraData, isDisabled } = props
Expand All @@ -30,8 +31,8 @@ const ProvenancesForm = (props: any) => {
})
},
() => {
console.log("Adding a provenance canceled due to ownership issues.");
},
return ChangeRequestStatus.CANCELLED;
},
getOwnershipAlertMessage // message to show when ownership needs to be reassigned
);
}
Expand Down Expand Up @@ -61,8 +62,8 @@ const ProvenancesForm = (props: any) => {
refresh()
},
() => {
console.log("Deleting a provenance canceled due to ownership issues.");
},
return ChangeRequestStatus.CANCELLED;
},
getOwnershipAlertMessage // message to show when ownership needs to be reassigned
);

Expand Down
17 changes: 9 additions & 8 deletions frontend/src/components/Widgets/ArrayFieldTemplate.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
} from "@dnd-kit/core";
import { SortableItem } from "../ProofingTab/SortableItem";
import {checkOwnership, getOwnershipAlertMessage} from "../../helpers/ownershipAlert";
import {ChangeRequestStatus} from "../../helpers/settings";

function ArrayFieldTemplate(props: any) {
const [isDragging, setIsDragging] = useState(false);
Expand Down Expand Up @@ -87,12 +88,12 @@ function ArrayFieldTemplate(props: any) {
}
},
() => {
console.log("Ownership reassignment was canceled.");
},
(owner) => getOwnershipAlertMessage(owner) // Prompt message
return ChangeRequestStatus.CANCELLED;
},
(owner) => getOwnershipAlertMessage(owner)
);
} catch (error) {
console.error("Error during add action:", error);
alert(`Error during add action: ${error}`);
}
}

Expand All @@ -110,12 +111,12 @@ function ArrayFieldTemplate(props: any) {
}
},
() => {
console.log("Ownership reassignment was canceled.");
return ChangeRequestStatus.CANCELLED;
},
(owner) => getOwnershipAlertMessage(owner) // Prompt message
);
} catch (error) {
console.error("Error during add action:", error);
alert(`Error during add action: ${error}`);
}
}

Expand All @@ -130,12 +131,12 @@ function ArrayFieldTemplate(props: any) {
}
},
() => {
console.log("Ownership reassignment was canceled.");
return ChangeRequestStatus.CANCELLED;
},
(owner) => getOwnershipAlertMessage(owner) // Prompt message
);
} catch (error) {
console.error("Error during add action:", error);
alert(`Error during add action: ${error}`);
}
}

Expand Down
8 changes: 2 additions & 6 deletions frontend/src/helpers/ownershipAlert.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,10 @@ export const checkOwnership = (
statementService.assignOwner(fetchedData.id, {})
.then(() => {
return onSave(fetchedData, userId)
.then((result) => resolve(ChangeRequestStatus.SAVED))
.then(() => resolve(ChangeRequestStatus.SAVED))
.catch((error) => reject(error));
})
.catch((error) => {
console.error("Failed to reassign ownership", error);
onCancel(fetchedData, userId);
reject(error);
});
Expand All @@ -42,7 +41,6 @@ export const checkOwnership = (
}
})
.catch((fetchError) => {
console.error("Failed to fetch the data", fetchError);
onCancel(null, userId);
reject(fetchError);
});
Expand All @@ -68,11 +66,10 @@ export const checkSentenceOwnership = (
sentenceService.assignOwner(fetchedData.id, {})
.then(() => {
return onSave(fetchedData, userId)
.then((result) => resolve(ChangeRequestStatus.SAVED))
.then(() => resolve(ChangeRequestStatus.SAVED))
.catch((error) => reject(error));
})
.catch((error) => {
console.error("Failed to reassign ownership", error);
onCancel(fetchedData, userId);
reject(error);
});
Expand All @@ -87,7 +84,6 @@ export const checkSentenceOwnership = (
}
})
.catch((fetchError) => {
console.error("Failed to fetch the data", fetchError);
onCancel(null, userId);
reject(fetchError);
});
Expand Down
22 changes: 10 additions & 12 deletions frontend/src/services/CustomDropdownService.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Option } from "../types";
import { composerApi as api } from "./apis";
import { autocompleteRows } from "../helpers/settings";
import {autocompleteRows, ChangeRequestStatus} from "../helpers/settings";
import {
convertToConnectivityStatementUpdate,
getViasGroupLabel,
Expand Down Expand Up @@ -38,7 +38,6 @@ export async function getAnatomicalEntities(
const anatomicalEntities = response.data.results || [];
return mapAnatomicalEntitiesToOptions(anatomicalEntities, groupLabel);
} catch (error) {
console.error("Error fetching anatomical entities:", error);
return [];
}
}
Expand All @@ -54,11 +53,9 @@ export async function updateOrigins(
};

try {
return await statementService.partialUpdate(statementId, patchedStatement, () => {
console.log("User canceled ownership reassignment.");
});
return await statementService.partialUpdate(statementId, patchedStatement);
} catch (error) {
console.error("Error updating origins:", error);
alert(`Error updating origins: ${error}`);
}
}

Expand All @@ -85,7 +82,7 @@ export async function updateEntity({
propertyToUpdate,
}: UpdateEntityParams) {
if (entityId == null) {
console.error(`Error updating ${entityType}`);
alert(`Error updating ${entityType}`);
}

const entityIds = selected.map((option) => parseInt(option.id));
Expand All @@ -105,15 +102,17 @@ export async function updateEntity({
return checkOwnership(
statementId,
() => updateFunction(entityId as number, patchObject), // Re-attempt the update if ownership is reassigned
(fetchedEntity) => console.log(`Ownership assigned, updated entity:`, fetchedEntity), // Optional: handle post-assignment logic
() => {
return ChangeRequestStatus.CANCELLED;
}, // Optional: handle post-assignment logic
(owner) => getOwnershipAlertMessage(owner)
);
}
} else {
console.error(`No update function found for entity type: ${entityType}`);
alert(`No update function found for entity type: ${entityType}`);
}
} catch (error) {
console.error(`Error updating ${entityType}`, error);
alert(`Error updating ${entityType}`);
}
}

Expand Down Expand Up @@ -277,7 +276,6 @@ export async function searchForwardConnection(

return [...sameSentenceOptions, ...differentSentenceOptions];
} catch (error) {
console.error("Error fetching data:", error);
throw error;
}
}
Expand All @@ -299,7 +297,7 @@ export async function updateForwardConnections(
try {
return await statementService.update(updateData);
} catch (error) {
console.error("Error updating statement:", error);
alert(`Error updating statement: ${error}`);
}
}

Expand Down
6 changes: 4 additions & 2 deletions frontend/src/services/NoteService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@ import { composerApi } from "./apis"
import { Note } from "../apiclient/backend"
import { AbstractService } from "./AbstractService"
import {checkOwnership, checkSentenceOwnership} from "../helpers/ownershipAlert";
import {ChangeRequestStatus} from "../helpers/settings";
class NoteService extends AbstractService {
async save(note: Note, onCancel: () => void = () => {}) {
try {
return await composerApi.composerNoteCreate(note).then((response: any) => response.data);
} catch (err) {
const defaultOnCancel = typeof onCancel === 'function' ? onCancel : () => {};
const defaultOnCancel = typeof onCancel === 'function' ? onCancel : () => {
return ChangeRequestStatus.CANCELLED;
};

if (note.sentence_id !== undefined && note.sentence_id !== null) {
const id = note.sentence_id;
Expand All @@ -32,7 +35,6 @@ class NoteService extends AbstractService {
`This statement is currently assigned to ${owner.first_name}. You are in read-only mode. Would you like to assign this statement to yourself and gain edit access?`
);
} else {
console.error("Note does not have a valid ID for ownership check.");
return Promise.reject("Invalid note type: missing both sentence_id and connectivity_statement_id.");
}
}
Expand Down
4 changes: 1 addition & 3 deletions frontend/src/services/StatementService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -49,10 +49,9 @@ class ConnectivityStatementService extends AbstractService {
return await checkOwnership(
id,
// Retry the update after ownership is reassigned, including new owner ID
async (userId: number) => {
async () => {
const updatedStatement = {
...connectivityStatement,
owner_id: userId,
};
await composerApi.composerConnectivityStatementUpdate(id, updatedStatement).then((response: any) => response.data);
return ChangeRequestStatus.SAVED;
Expand Down Expand Up @@ -82,7 +81,6 @@ class ConnectivityStatementService extends AbstractService {
async (userId: number) => {
const updatedPatchedStatement = {
...patchedConnectivityStatementUpdate,
owner_id: userId,
};
return composerApi.composerConnectivityStatementPartialUpdate(id, updatedPatchedStatement).then((response: any) => response.data);
},
Expand Down

0 comments on commit 2731765

Please sign in to comment.