Skip to content

Commit 075c9e9

Browse files
committed
wip: Provide a simplified editor #1118
1 parent 2895bf7 commit 075c9e9

File tree

1 file changed

+101
-0
lines changed

1 file changed

+101
-0
lines changed

core/client/components/KEditor.vue

+101
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
<template>
2+
<KForm
3+
ref="formRef"
4+
:values="object"
5+
:schema="schema"
6+
:filter="filter"
7+
/>
8+
</template>
9+
10+
<script setup>
11+
import _ from 'lodash'
12+
import logger from 'loglevel'
13+
import { ref } from 'vue'
14+
import { api, Context } from '@kalisio/kdk/core.client'
15+
16+
// Props
17+
const props = defineProps({
18+
service: {
19+
type: String,
20+
required: true
21+
},
22+
baseObject: {
23+
type: Object,
24+
default: () => null
25+
},
26+
object: {
27+
type: Object,
28+
default: () => null
29+
},
30+
schema: {
31+
type: [String, Object],
32+
default: () => null
33+
},
34+
filter: {
35+
type: [String, Array],
36+
default: () => null
37+
},
38+
beforeRequest: {
39+
type: Function,
40+
default: null
41+
},
42+
afterRequest: {
43+
type: Function,
44+
default: null
45+
},
46+
dense: {
47+
type: Boolean,
48+
default: false
49+
}
50+
})
51+
52+
// Data
53+
const formRef = ref(null)
54+
const mode = props.object ? 'edition' : 'creation'
55+
56+
// Functions
57+
async function apply () {
58+
// validate the form
59+
const { isValid, values: formValues } = formRef.value.validate()
60+
if (isValid) {
61+
// merge values with base object
62+
const values = _.merge(formValues, props.baseObject)
63+
// retrieve the service
64+
const context = Context.get()
65+
const service = api.getService(props.service, context?.value)
66+
if (!service) {
67+
logger.error(`[KDK] Cannot find service ${props.service}`)
68+
return
69+
}
70+
// run the beforeRequest hook
71+
let response = { isOk: true, values }
72+
if (props.beforeRequest) {
73+
logger.debug('[KDK] Apply beforeRequest hook')
74+
response = await props.beforeRequest(values, { mode, form: formRef.value, ...props })
75+
}
76+
if (!response.isOk) return false
77+
// do the request
78+
if (mode === 'creation') {
79+
logger.debug('[KDK] Create object with data', response.values)
80+
await service.create(values)
81+
} else {
82+
logger.debug(`[KDK] Patch object ${props.object._id} with data`, response.values)
83+
await service.patch(props.object._id, response.values)
84+
}
85+
// run the afterRequest hook
86+
if (props.afterRequest) {
87+
logger.debug('[KDK] Apply afterRequest hook')
88+
response = await props.afterRequest(values, { mode, form: formRef.value, ...props })
89+
}
90+
if (!response.isOk) return false
91+
return true
92+
} else {
93+
logger.debug('[KDK] Form is invalid')
94+
}
95+
}
96+
97+
// Expose
98+
defineExpose({
99+
apply
100+
})
101+
</script>

0 commit comments

Comments
 (0)