1import { createResource } from "frappe-ui";
2import { reactive } from "vue";
3
4const doctypeMeta = reactive({});
5const userSettings = reactive({});
6
7export function getMeta(doctype: string) {
8 const meta = createResource({
9 url: "frappe.desk.form.load.getdoctype",
10 params: {
11 doctype: doctype,
12 with_parent: 1,
13 cached_timestamp: null,
14 },
15 cache: ["Meta", doctype],
16 onSuccess: (res) => {
17 let dtMetas = res.docs;
18 for (let dtMeta of dtMetas) {
19 doctypeMeta[dtMeta.name] = dtMeta;
20 }
21
22 userSettings[doctype] = JSON.parse(res.user_settings);
23 },
24 });
25 if (!doctypeMeta[doctype] && !meta.loading) {
26 meta.fetch();
27 }
28 function getFields(dt = null) {
29 dt = dt || doctype;
30 return doctypeMeta[dt]?.fields.map((f) => f) || [];
31 }
32
33 function getField(fieldname: string) {
34 return (
35 doctypeMeta[doctype]?.fields.find((f) => f.fieldname === fieldname) ||
36 null
37 );
38 }
39
40 return { meta, doctypeMeta, getField, getFields };
41}
42