Gửi một biểu mẫu với loại trường thực thể (trang, sản phẩm...)
✅ Mục đích của kịch bản:
- Lấy cấu hình biểu mẫu từ Nền tảng OneEntry
- Người dùng chọn một trong các phần tử của thực thể
- Gửi dữ liệu đã thu thập đến API OneEntry.
✅ Những gì bạn cần:
- Một PROJECT_URL và APP_TOKEN hợp lệ để xác thực với API OneEntry.
- Một biểu mẫu đã được cấu hình trước trong OneEntry với một dấu hiệu (v í dụ: thực thể) và các trường bao gồm một trường loại "thực thể".
- Các trường biểu mẫu đã được cấu hình trước bao gồm một trường loại "thực thể".
📌 Quan trọng:
- Chúng tôi không xử lý lỗi trong các ví dụ này.
- Bạn có thể xử lý lỗi trong trycatch hoặc trong một cấu trúc như "await Promise.catch((error) => error)"
Kịch bản
1. Nhập oneEntry và định nghĩa url và token
Ví dụ:
import { defineOneEntry } from 'oneentry';
const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';
2. Tạo một khách hàng API với hàm defineOneEntry()
Ví dụ:
const { AuthProvider, Orders, Payments, Forms } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});
3. Chúng tôi nhận dữ liệu biểu mẫu từ Nền tảng OneEntry để tạo một biểu mẫu trên giao diện người dùng với Forms.getFormByMarker()
Ví dụ:
const formData = await Forms.getFormByMarker('entity');
Kết quả:
{
"id": 2,
"attributeSetId": 1,
"type": "data",
"localizeInfos": {
"title": "Thực thể",
"titleForSite": "",
"successMessage": "",
"unsuccessMessage": "",
"urlAddress": "",
"database": "0",
"script": "0"
},
"version": 17,
"position": 1,
"identifier": "entity",
"processingType": "script",
"templateId": null,
"attributes": [
{
"type": "entity",
"marker": "entity",
"isLogin": null,
"isSignUp": null,
"position": 1,
"settings": {},
"isVisible": true,
"listTitles": [
{
"id": 1,
"depth": 0,
"title": "Danh mục",
"parentId": null,
"position": 1,
"selected": true
},
{
"id": 7,
"depth": 0,
"title": "Trang cao cấp",
"parentId": null,
"position": 2,
"selected": true
}
],
"validators": {},
"localizeInfos": {
"title": "Thực thể"
},
"additionalFields": [],
"isNotificationEmail": null,
"isNotificationPhoneSMS": null,
"isNotificationPhonePush": null
}
]
}
4. Chọn một trong các phần tử của thuộc tính loại thực thể và truyền vào giá trị dưới dạng mảng các id(số) cho trang hoặc mảng các chuỗi cho sản phẩm
Ví dụ:
[
{
"marker": "entity",
"type": "entity",
"value": [
1
]
}
]
5. Đăng dữ liệu biểu mẫu với FormData.postFormsData()
Ví dụ:
const response = FormData.postFormsData({
formIdentifier: 'entity',
formData: fieldsData,
});
console.log(response);
Kết quả:
{
"formData": {
"formIdentifier": "entity",
"time": "2025-04-30T20:11:30.059Z",
"formData": [
{
"marker": "entity",
"type": "entity",
"value": [
1
]
}
],
"id": 67
},
"actionMessage": ""
}
Ví dụ cuối cùng
// 1. Nhập oneEntry và định nghĩa PROJECT_URL và APP_TOKEN
import { defineOneEntry } from 'oneentry';
const PROJECT_URL = 'your-project-url';
const APP_TOKEN = 'your-app-token';
// 2. Tạo một khách hàng API:
const { Forms, FormData } = defineOneEntry(PROJECT_URL, {
token: APP_TOKEN,
});
// 3. Chúng tôi nhận dữ liệu biểu mẫu từ Nền tảng OneEntry để tạo một biểu mẫu trên giao diện người dùng
const formData = await Forms.getFormByMarker('entity');
// 4. chọn một trong các phần tử của thuộc tính loại thực thể
const fieldsData = [
{
marker: 'entity',
type: 'entity',
value: [
1,
],
},
];
// 5. Đăng dữ liệu biểu mẫu với FormData.postFormsData()
let response = await FormData.postFormsData({
formIdentifier: 'entity',
formData: fieldsData,
});
// console.log(response);