forked from frappe/lms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNewBatchModal.vue
More file actions
262 lines (249 loc) · 6.12 KB
/
NewBatchModal.vue
File metadata and controls
262 lines (249 loc) · 6.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
<template>
<Dialog
v-model="show"
:options="{
title: __('New Batch'),
size: '3xl',
}"
>
<template #body-content>
<div class="text-base">
<div class="grid grid-cols-3 gap-5">
<FormControl
v-model="batch.title"
:label="__('Title')"
:required="true"
autocomplete="off"
/>
<FormControl
v-model="batch.start_date"
:label="__('Start Date')"
type="date"
:required="true"
/>
<FormControl
v-model="batch.end_date"
:label="__('End Date')"
type="date"
:required="true"
/>
<FormControl
v-model="batch.start_time"
:label="__('Start Time')"
type="time"
:required="true"
/>
<FormControl
v-model="batch.end_time"
:label="__('End Time')"
type="time"
:required="true"
/>
<FormControl
v-model="batch.timezone"
:label="__('Timezone')"
:required="true"
autocomplete="off"
/>
<Link
v-model="batch.category"
doctype="LMS Category"
:label="__('Category')"
:onCreate="createCategory"
/>
<FormControl
v-model="batch.seat_count"
:label="__('Seat Count')"
type="number"
:required="false"
/>
<FormControl
v-model="batch.medium"
type="select"
:options="mediumOptions"
:label="__('Medium')"
class="mb-4"
/>
</div>
<div class="space-y-5 border-t mt-5 pt-5">
<div class="grid grid-cols-2 gap-5">
<FormControl
v-model="batch.description"
:label="__('Description')"
type="textarea"
:required="true"
:rows="4"
/>
<MultiSelect
v-model="batch.instructors"
doctype="User"
:label="__('Instructors')"
:required="true"
:onCreate="() => (showMemberModal = true)"
url="lms.lms.api.search_users_by_role"
:searchParams="{ roles: JSON.stringify(['Batch Evaluator']) }"
/>
</div>
<div class="">
<div class="mb-1.5 text-sm text-ink-gray-5">
{{ __('Batch Details') }}
<span class="text-ink-red-3">*</span>
</div>
<TextEditor
:content="batch.batch_details"
@change="(val: string) => (batch.batch_details = val)"
:editable="true"
:fixedMenu="true"
editorClass="prose-sm max-w-none border-b border-x bg-surface-gray-2 rounded-b-md py-1 px-2 min-h-[10rem] max-h-[14rem] overflow-auto"
/>
</div>
</div>
</div>
</template>
<template #actions="{ close }">
<div class="text-end">
<Button variant="solid" @click="saveBatch(close)">
{{ __('Save') }}
</Button>
</div>
</template>
</Dialog>
<NewMemberModal
v-model="showMemberModal"
:defaultRoles="['batch_evaluator']"
@created="onInstructorCreated"
/>
</template>
<script setup lang="ts">
import { Button, Dialog, FormControl, TextEditor, toast } from 'frappe-ui'
import { useOnboarding, useTelemetry } from 'frappe-ui/frappe'
import { computed, inject, onMounted, onBeforeUnmount, ref } from 'vue'
import { useRouter } from 'vue-router'
import { sanitizeHTML, createLMSCategory, cleanError } from '@/utils'
import MultiSelect from '@/components/Controls/MultiSelect.vue'
import Link from '@/components/Controls/Link.vue'
import NewMemberModal from '@/components/Modals/NewMemberModal.vue'
const show = defineModel<boolean>({ required: true, default: false })
const router = useRouter()
const { capture } = useTelemetry()
const { updateOnboardingStep } = useOnboarding('learning')
const user = inject<any>('$user')
const showMemberModal = ref(false)
const props = defineProps<{
batches: any
}>()
type Batch = {
title: string
start_date: string | null
end_date: string | null
start_time: string | null
end_time: string | null
timezone: string | null
description: string
batch_details: string
instructors: string[]
category: string | null
seat_count: number
medium: string | null
}
const batch = ref<Batch>({
title: '',
start_date: null,
end_date: null,
start_time: null,
end_time: null,
timezone: null,
description: '',
batch_details: '',
instructors: [],
category: null,
seat_count: 0,
medium: null,
})
const createCategory = (name: string, done: () => void) => {
createLMSCategory(name).then((categoryName: string) => {
if (!categoryName) return
batch.value.category = categoryName
done()
})
}
const onInstructorCreated = (user: any) => {
batch.value.instructors = [...batch.value.instructors, user.name]
}
const validateFields = () => {
Object.keys(batch.value).forEach((key) => {
if (typeof batch.value[key as keyof Batch] === 'string') {
batch.value[key as keyof Batch] = sanitizeHTML(
batch.value[key as keyof Batch] as string
)
}
})
}
const saveBatch = (close: () => void = () => {}) => {
validateFields()
props.batches.insert.submit(
{
...batch.value,
instructors: batch.value.instructors.map((instructor) => ({
instructor: instructor,
})),
},
{
onSuccess(data: any) {
toast.success(__('Batch created successfully'))
close()
capture('batch_created')
router.push({
name: 'BatchDetail',
params: { batchName: data.name },
hash: '#settings',
})
if (user.data?.is_system_manager) {
updateOnboardingStep('create_first_batch', true, false, () => {
localStorage.setItem('firstBatch', data.name)
})
}
},
onError(err: any) {
const message = err?.messages?.[0]
toast.error(message ? cleanError(message) : __('Error creating batch'))
console.error(err)
},
}
)
}
const keyboardShortcut = (e: KeyboardEvent) => {
if (
e.key === 's' &&
(e.ctrlKey || e.metaKey) &&
e.target &&
e.target instanceof HTMLElement &&
!e.target.classList.contains('ProseMirror')
) {
saveBatch()
e.preventDefault()
}
}
onMounted(() => {
window.addEventListener('keydown', keyboardShortcut)
capture('batch_form_opened')
})
onBeforeUnmount(() => {
window.removeEventListener('keydown', keyboardShortcut)
capture('batch_form_closed', {
data: batch.value,
})
})
const mediumOptions = computed(() => {
return [
{
label: __('Online'),
value: 'Online',
},
{
label: __('Offline'),
value: 'Offline',
},
]
})
</script>