Skip to content

Commit

Permalink
CSCKAN-323 feat: Update vias and destination changes
Browse files Browse the repository at this point in the history
  • Loading branch information
afonsobspinto committed Oct 16, 2024
1 parent caf19d8 commit 9a05404
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
4 changes: 4 additions & 0 deletions frontend/src/components/Forms/StatementForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,7 @@ const StatementForm = (props: any) => {
},
onUpdate: async (selectedOptions: Option[], formId: any) => {
await updateEntity({
statementId: statement.id,
selected: selectedOptions,
entityId: getConnectionId(formId, statement.vias),
entityType: "via",
Expand Down Expand Up @@ -336,6 +337,7 @@ const StatementForm = (props: any) => {
},
onUpdate: async (selectedOptions: Option[], formId: any) => {
await updateEntity({
statementId: statement.id,
selected: selectedOptions,
entityId: getConnectionId(formId, statement.vias),
entityType: "via",
Expand Down Expand Up @@ -468,6 +470,7 @@ const StatementForm = (props: any) => {
},
onUpdate: async (selectedOptions: Option[], formId: string) => {
await updateEntity({
statementId: statement.id,
selected: selectedOptions,
entityId: getConnectionId(formId, statement?.destinations),
entityType: "destination",
Expand Down Expand Up @@ -512,6 +515,7 @@ const StatementForm = (props: any) => {
},
onUpdate: async (selectedOptions: Option[], formId: string) => {
await updateEntity({
statementId: statement.id,
selected: selectedOptions,
entityId: getConnectionId(formId, statement?.destinations),
entityType: "destination",
Expand Down
17 changes: 16 additions & 1 deletion frontend/src/services/CustomDropdownService.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ export async function updateOrigins(selected: Option[], statementId: number,
}

export type UpdateEntityParams = {
statementId: number
selected: Option[];
entityId: number | null;
entityType: "via" | "destination";
Expand All @@ -79,6 +80,7 @@ const apiFunctionMap = {
};

export async function updateEntity({
statementId,
selected,
entityId,
entityType,
Expand All @@ -93,9 +95,22 @@ export async function updateEntity({
const patchObject = { [propertyToUpdate]: entityIds };

try {
// Get the API function from the map
const updateFunction = apiFunctionMap[entityType];
if (updateFunction) {
await updateFunction(entityId, patchObject);
// Attempt to update, using checkOwnership in case of ownership error
try {
await updateFunction(entityId, patchObject);
} catch (error) {
// Ownership error occurred, trigger ownership check
checkOwnership(
statementId,
() => updateFunction(entityId, patchObject), // Re-attempt the update if ownership is reassigned
(fetchedEntity) => console.log(`Ownership assigned, updated entity:`, fetchedEntity), // Optional: handle post-assignment logic
(owner) =>
`This entity is currently assigned to ${owner.first_name}. You are in read-only mode. Would you like to assign this entity to yourself and gain edit access?`
);
}
} else {
console.error(`No update function found for entity type: ${entityType}`);
}
Expand Down

0 comments on commit 9a05404

Please sign in to comment.