Skip to content

Commit

Permalink
fix: lesson auto save
Browse files Browse the repository at this point in the history
  • Loading branch information
pateljannat committed Aug 5, 2024
1 parent eed3306 commit 8adfe24
Show file tree
Hide file tree
Showing 13 changed files with 113 additions and 76 deletions.
31 changes: 15 additions & 16 deletions frontend/src/components/AppSidebar.vue
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ import { ChevronRight, Plus } from 'lucide-vue-next'
import { createResource, Button } from 'frappe-ui'
import PageModal from '@/components/Modals/PageModal.vue'
const { user } = sessionStore()
const { user, sidebarSettings } = sessionStore()
const { userResource } = usersStore()
const socket = inject('$socket')
const unreadCount = ref(0)
Expand All @@ -115,6 +115,20 @@ onMounted(() => {
unreadNotifications.reload()
})
addNotifications()
sidebarSettings.reload(
{},
{
onSuccess(data) {
Object.keys(data).forEach((key) => {
if (!parseInt(data[key])) {
sidebarLinks.value = sidebarLinks.value.filter(
(link) => link.label.toLowerCase().split(' ').join('_') !== key
)
}
})
},
}
)
})
const unreadNotifications = createResource({
Expand Down Expand Up @@ -153,21 +167,6 @@ const addNotifications = () => {
}
}
const sidebarSettings = createResource({
url: 'lms.lms.api.get_sidebar_settings',
cache: 'Sidebar Settings',
auto: true,
onSuccess(data) {
Object.keys(data).forEach((key) => {
if (!parseInt(data[key])) {
sidebarLinks.value = sidebarLinks.value.filter(
(link) => link.label.toLowerCase().split(' ').join('_') !== key
)
}
})
},
})
const openPageModal = (link) => {
showPageModal.value = true
pageToEdit.value = link
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/components/CourseOutline.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
<div class="outline-lesson pl-8 py-2 pr-4">
<router-link
:to="{
name: allowEdit ? 'CreateLesson' : 'Lesson',
name: allowEdit ? 'LessonForm' : 'Lesson',
params: {
courseName: courseName,
chapterNumber: lesson.number.split('.')[0],
Expand Down Expand Up @@ -89,7 +89,7 @@
<div v-if="allowEdit" class="flex mt-2 mb-4 pl-8">
<router-link
:to="{
name: 'CreateLesson',
name: 'LessonForm',
params: {
courseName: courseName,
chapterNumber: chapter.idx,
Expand Down
40 changes: 28 additions & 12 deletions frontend/src/components/MobileLayout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
<slot />
</div>
<div
v-if="tabs"
v-if="sidebarSettings.data"
class="fixed flex justify-around border-t border-gray-300 bottom-0 z-10 w-full bg-white standalone:pb-4"
:style="{
gridTemplateColumns: `repeat(${tabs.length}, minmax(0, 1fr))`,
gridTemplateColumns: `repeat(${sidebarLinks.length}, minmax(0, 1fr))`,
}"
>
<button
v-for="tab in tabs"
v-for="tab in sidebarLinks"
:key="tab.label"
:class="isVisible(tab) ? 'block' : 'hidden'"
class="flex flex-col items-center justify-center py-3 transition active:scale-95"
Expand All @@ -29,21 +29,38 @@
<script setup>
import { getSidebarLinks } from '../utils'
import { useRouter } from 'vue-router'
import { computed } from 'vue'
import { computed, ref, onMounted } from 'vue'
import { sessionStore } from '@/stores/session'
import { usersStore } from '@/stores/user'
import * as icons from 'lucide-vue-next'
const { logout, user } = sessionStore()
const { logout, user, sidebarSettings } = sessionStore()
let { isLoggedIn } = sessionStore()
const router = useRouter()
let { userResource } = usersStore()
const sidebarLinks = ref(getSidebarLinks())
const tabs = computed(() => {
let links = getSidebarLinks()
onMounted(() => {
sidebarSettings.reload(
{},
{
onSuccess(data) {
Object.keys(data).forEach((key) => {
if (!parseInt(data[key])) {
sidebarLinks.value = sidebarLinks.value.filter(
(link) => link.label.toLowerCase().split(' ').join('_') !== key
)
}
})
addAccessLinks()
},
}
)
})
const addAccessLinks = () => {
if (user) {
links.push({
sidebarLinks.value.push({
label: 'Profile',
icon: 'UserRound',
activeFor: [
Expand All @@ -54,18 +71,17 @@ const tabs = computed(() => {
'ProfileRoles',
],
})
links.push({
sidebarLinks.value.push({
label: 'Log out',
icon: 'LogOut',
})
} else {
links.push({
sidebarLinks.value.push({
label: 'Log in',
icon: 'LogIn',
})
}
return links
})
}
let isActive = (tab) => {
return tab.activeFor?.includes(router.currentRoute.value.name)
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/pages/Batches.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
>
<Breadcrumbs
class="h-7"
:items="[{ label: __('All Batches'), route: { name: 'Batches' } }]"
:items="[{ label: __('Batches'), route: { name: 'Batches' } }]"
/>
<div class="flex space-x-2">
<div class="w-40">
Expand All @@ -25,7 +25,7 @@
>
<Button variant="solid">
<template #prefix>
<Plus class="h-4 w-4" />
<Plus class="h-4 w-4 stroke-1.5" />
</template>
{{ __('New Batch') }}
</Button>
Expand Down
3 changes: 2 additions & 1 deletion frontend/src/pages/CertifiedParticipants.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,10 @@
<div>
<FormControl
type="text"
placeholder="Search Participants"
placeholder="Search"
v-model="searchQuery"
@input="participants.reload()"
class="w-40"
>
<template #prefix>
<Search class="w-4 stroke-1.5 text-gray-600" name="search" />
Expand Down
26 changes: 14 additions & 12 deletions frontend/src/pages/Courses.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,21 @@
>
<Breadcrumbs
class="h-7"
:items="[{ label: __('All Courses'), route: { name: 'Courses' } }]"
:items="[{ label: __('Courses'), route: { name: 'Courses' } }]"
/>
<div class="flex space-x-2">
<FormControl
type="text"
placeholder="Search Course"
v-model="searchQuery"
@input="courses.reload()"
>
<template #prefix>
<Search class="w-4 stroke-1.5 text-gray-600" name="search" />
</template>
</FormControl>
<div class="flex space-x-2 justify-end">
<div class="w-36">
<FormControl
type="text"
placeholder="Search"
v-model="searchQuery"
@input="courses.reload()"
>
<template #prefix>
<Search class="w-4 h-4 stroke-1.5" name="search" />
</template>
</FormControl>
</div>
<router-link
:to="{
name: 'CreateCourse',
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/Lesson.vue
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
<router-link
v-if="allowEdit()"
:to="{
name: 'CreateLesson',
name: 'LessonForm',
params: {
courseName: courseName,
chapterNumber: props.chapterNumber,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ const editor = ref(null)
const instructorEditor = ref(null)
const user = inject('$user')
const openInstructorEditor = ref(false)
let autoSaveInterval
const props = defineProps({
courseName: {
Expand Down Expand Up @@ -134,32 +135,45 @@ const lessonDetails = createResource({
lesson[key] = data.lesson[key]
})
lesson.include_in_preview = data.include_in_preview ? true : false
editor.value.isReady.then(() => {
if (data.lesson.content) {
editor.value.render(JSON.parse(data.lesson.content))
} else if (data.lesson.body) {
let blocks = convertToJSON(data.lesson)
editor.value.render({
blocks: blocks,
})
}
})
instructorEditor.value.isReady.then(() => {
if (data.lesson.instructor_content) {
instructorEditor.value.render(
JSON.parse(data.lesson.instructor_content)
)
} else if (data.lesson.instructor_notes) {
let blocks = convertToJSON(data.lesson)
instructorEditor.value.render({
blocks: blocks,
})
}
})
addLessonContent(data)
addInstructorNotes(data)
enableAutoSave()
}
},
})
const addLessonContent = (data) => {
editor.value.isReady.then(() => {
if (data.lesson.content) {
editor.value.render(JSON.parse(data.lesson.content))
} else if (data.lesson.body) {
let blocks = convertToJSON(data.lesson)
editor.value.render({
blocks: blocks,
})
}
})
}
const addInstructorNotes = (data) => {
instructorEditor.value.isReady.then(() => {
if (data.lesson.instructor_content) {
instructorEditor.value.render(JSON.parse(data.lesson.instructor_content))
} else if (data.lesson.instructor_notes) {
let blocks = convertToJSON(data.lesson)
instructorEditor.value.render({
blocks: blocks,
})
}
})
}
const enableAutoSave = () => {
autoSaveInterval = setInterval(() => {
saveLesson()
}, 10000)
}
const newLessonResource = createResource({
url: 'frappe.client.insert',
makeParams(values) {
Expand Down Expand Up @@ -357,9 +371,6 @@ const editCurrentLesson = () => {
validate() {
return validateLesson()
},
onSuccess() {
showToast('Success', 'Lesson updated successfully', 'check')
},
onError(err) {
showToast('Error', err.message, 'x')
},
Expand Down Expand Up @@ -418,7 +429,7 @@ const breadcrumbs = computed(() => {
crumbs.push({
label: lessonDetails?.data?.lesson ? 'Edit Lesson' : 'Create Lesson',
route: {
name: 'CreateLesson',
name: 'LessonForm',
params: {
courseName: props.courseName,
chapterNumber: props.chapterNumber,
Expand Down
2 changes: 1 addition & 1 deletion frontend/src/pages/ProfileAbout.vue
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<h2 class="mb-3 text-lg font-semibold text-gray-900">
{{ __('Achievements') }}
</h2>
<div class="grid grid-cols-5 gap-4">
<div class="grid grid-cols-2 md:grid-cols-3 lg:grid-cols-5 gap-4">
<div v-for="badge in badges.data">
<Popover trigger="hover" :leaveDelay="Number(0.01)">
<template #target>
Expand Down
4 changes: 2 additions & 2 deletions frontend/src/router.js
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,8 @@ const routes = [
},
{
path: '/courses/:courseName/learn/:chapterNumber-:lessonNumber/edit',
name: 'CreateLesson',
component: () => import('@/pages/CreateLesson.vue'),
name: 'LessonForm',
component: () => import('@/pages/LessonForm.vue'),
props: true,
},
{
Expand Down
7 changes: 7 additions & 0 deletions frontend/src/stores/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,11 +53,18 @@ export const sessionStore = defineStore('lms-session', () => {
},
})

const sidebarSettings = createResource({
url: 'lms.lms.api.get_sidebar_settings',
cache: 'Sidebar Settings',
auto: false,
})

return {
user,
isLoggedIn,
login,
logout,
branding,
sidebarSettings,
}
})
2 changes: 1 addition & 1 deletion frontend/src/utils/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,7 @@ export function getSidebarLinks() {
'CourseDetail',
'Lesson',
'CreateCourse',
'CreateLesson',
'LessonForm',
],
},
{
Expand Down
1 change: 1 addition & 0 deletions lms/hooks.py
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@
"lms.lms.utils.get_lesson_index",
"lms.lms.utils.get_lesson_url",
"lms.page_renderers.get_profile_url",
"lms.overrides.user.get_palette",
],
"filters": [],
}
Expand Down

0 comments on commit 8adfe24

Please sign in to comment.