Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adds retry to updating list item in DynamicForm. Closes #1919 #1920

Merged
merged 1 commit into from
Dec 11, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 31 additions & 4 deletions src/controls/dynamicForm/DynamicForm.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ import "@pnp/sp/content-types";
import "@pnp/sp/folders";
import "@pnp/sp/items";
import { IFolder } from "@pnp/sp/folders";
import { IInstalledLanguageInfo } from "@pnp/sp/presets/all";
import { IInstalledLanguageInfo, IItemUpdateResult, IList } from "@pnp/sp/presets/all";
import { cloneDeep, isEqual } from "lodash";
import { ICustomFormatting, ICustomFormattingBodySection, ICustomFormattingNode } from "../../common/utilities/ICustomFormatting";
import SPservice from "../../services/SPService";
Expand All @@ -49,6 +49,10 @@ import { Icon } from "@fluentui/react/lib/Icon";

const stackTokens: IStackTokens = { childrenGap: 20 };

const timeout = (ms: number): Promise<void> => {
return new Promise((resolve) => setTimeout(resolve, ms));
};

/**
* DynamicForm Class Control
*/
Expand Down Expand Up @@ -611,7 +615,7 @@ export class DynamicForm extends React.Component<
// Set the content type ID for the target item
objects[contentTypeIdField] = contentTypeId;
// Update the just created folder or Document Set
const iur = await library.items.getById(folderId).update(objects);
const iur = await this.updateListItemRetry(library, folderId, objects);
if (onSubmitted) {
onSubmitted(
iur.data,
Expand Down Expand Up @@ -689,7 +693,7 @@ export class DynamicForm extends React.Component<
// Set the content type ID for the target item
objects[contentTypeIdField] = contentTypeId;
// Update the just created file
const iur = await library.items.getById(fileId).update(objects);
const iur = await this.updateListItemRetry(library, fileId, objects);
if (onSubmitted) {
onSubmitted(
iur.data,
Expand Down Expand Up @@ -1521,4 +1525,27 @@ export class DynamicForm extends React.Component<
const folder = sp.web.getFolderByServerRelativePath(`${serverRelativeLibraryPath}/${normalizedFolderPath}`);
return folder;
};
}

/**
* Updates a list item and retries the operation if a 409 (Save Conflict) was thrown.
* @param list The list/library on which to execute the operation
* @param itemId The item ID
* @param objects The values to update the item with
* @param retry The retry index
* @returns An update result
*/
private updateListItemRetry = async (list: IList, itemId: number, objects: {}, retry: number = 0): Promise<IItemUpdateResult> => {
try {
return await list.items.getById(itemId).update(objects);
}
catch (error)
{
if (error.status === 409 && retry < 3) {
await timeout(100);
return await this.updateListItemRetry(list, itemId, objects, retry + 1);
}

throw error;
}
}
}
Loading