Skip to content

Commit

Permalink
update docs
Browse files Browse the repository at this point in the history
  • Loading branch information
foyarash committed Feb 5, 2025
1 parent 58f1149 commit b1fe397
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
22 changes: 20 additions & 2 deletions apps/docs/pages/docs/api/model-configuration.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -474,8 +474,7 @@ When you define a field, use the field's name as the key and the following objec
type: "Function",
description: (
<>
an async function that is used to remove a file from a remote provider. Called only for multi file upload.
For single file deletion, use <a href="#middlewares">middlewares.delete</a> instead.
an async function that is used to remove a file from a remote provider. Takes the file URI as an argument.
</>
)
},
Expand Down Expand Up @@ -681,6 +680,25 @@ The `actions` property is an array of objects that allows you to define a set of
]}
/>

## `middlewares` property

The `middlewares` property is an object of functions executed either before a record's update or deletion, where you can control if the deletion and update should happen or not. It can have the following properties:

<OptionsTable
options={[
{
name: "edit",
type: "Function",
description: "a function that is called before the form data is sent to the database. It takes the submitted form data as its first argument, and the current value in the database as its second argument. If false is returned, the update will not happen."
},
{
name: "delete",
type: "Function",
description: "a function that is called before the record is deleted. It takes the record to delete as its only argument. If false is returned, the deletion will not happen."
}
]}
/>

## NextAdmin Context

The `NextAdmin` context is an object containing the following properties:
Expand Down
3 changes: 1 addition & 2 deletions packages/next-admin/src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,8 +348,7 @@ export type Handler<
}
) => Promise<string>;
/**
* an async function that is used to remove a file from a remote provider. Called only for multi file upload.
* For single file deletion, use `middlewares.delete` on the model.
* an async function that is used to remove a file from a remote provider
*
* @param fileUri string - the remote file uri
* @returns success - Promise<boolean> - true if the deletion succeeded, false otherwise. If false is returned, the file will not be removed from the record.
Expand Down
9 changes: 7 additions & 2 deletions packages/next-admin/src/utils/server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -979,8 +979,6 @@ export const formattedFormData = async <M extends ModelName>(
? JSON.parse(formData[propertyName]!)
: formData[propertyName];

console.log("formDataValue", formDataValue);

if (
propertyType === "Int" ||
propertyType === "Float" ||
Expand Down Expand Up @@ -1084,6 +1082,13 @@ export const formattedFormData = async <M extends ModelName>(
const uploadErrorMessage =
editOptions?.[propertyName]?.handler?.uploadErrorMessage;
try {
if (currentRecord[propertyName]) {
await handleFileDeletion({
fileUris: [currentRecord[propertyName]],
property: propertyName,
editOptions,
});
}
const uploaded = await handleUploadProperty({
files: formData[propertyName] as unknown as UploadedFile[],
resourceId,
Expand Down

0 comments on commit b1fe397

Please sign in to comment.