mirror of
https://github.com/documenso/documenso.git
synced 2025-11-13 16:23:06 +10:00
♻️ upsertField and delete Field to package
This commit is contained in:
35
packages/lib/api/createOrUpdateField.ts
Normal file
35
packages/lib/api/createOrUpdateField.ts
Normal file
@ -0,0 +1,35 @@
|
||||
import toast from "react-hot-toast";
|
||||
|
||||
export const createOrUpdateField = async (
|
||||
document: any,
|
||||
field: any
|
||||
): Promise<any> => {
|
||||
try {
|
||||
const created = await toast.promise(
|
||||
fetch("/api/documents/" + document.id + "/fields", {
|
||||
method: "POST",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(field),
|
||||
}).then((res) => {
|
||||
if (!res.ok) {
|
||||
throw new Error(res.status.toString());
|
||||
}
|
||||
return res.json();
|
||||
}),
|
||||
{
|
||||
loading: "Adding...",
|
||||
success: "Added.",
|
||||
error: "Could not add :/",
|
||||
},
|
||||
{
|
||||
id: "saving field",
|
||||
style: {
|
||||
minWidth: "200px",
|
||||
},
|
||||
}
|
||||
);
|
||||
return created;
|
||||
} catch (error) {}
|
||||
};
|
||||
36
packages/lib/api/deleteField.ts
Normal file
36
packages/lib/api/deleteField.ts
Normal file
@ -0,0 +1,36 @@
|
||||
import toast from "react-hot-toast";
|
||||
|
||||
export const deleteField = async (field: any) => {
|
||||
if (!field.id) {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
const deleted = toast.promise(
|
||||
fetch("/api/documents/" + 0 + "/fields/" + field.id, {
|
||||
method: "DELETE",
|
||||
headers: {
|
||||
"Content-Type": "application/json",
|
||||
},
|
||||
body: JSON.stringify(field),
|
||||
}).then((res) => {
|
||||
if (!res.ok) {
|
||||
throw new Error(res.status.toString());
|
||||
}
|
||||
return res;
|
||||
}),
|
||||
{
|
||||
loading: "Deleting...",
|
||||
success: "Deleted.",
|
||||
error: "Could not delete :/",
|
||||
},
|
||||
{
|
||||
id: "delete",
|
||||
style: {
|
||||
minWidth: "200px",
|
||||
},
|
||||
}
|
||||
);
|
||||
return deleted;
|
||||
} catch (error) {}
|
||||
};
|
||||
2
packages/lib/api/index.ts
Normal file
2
packages/lib/api/index.ts
Normal file
@ -0,0 +1,2 @@
|
||||
export { createOrUpdateField } from "./createOrUpdateField";
|
||||
export { deleteField } from "./deleteField";
|
||||
Reference in New Issue
Block a user