Skip to content

Commit

Permalink
feat: auto save changes on workbook
Browse files Browse the repository at this point in the history
  • Loading branch information
nextchamp-saqib committed Nov 24, 2024
1 parent fbf47f8 commit ab142ab
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 4 deletions.
1 change: 1 addition & 0 deletions frontend/src2/types/workbook.types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ export type InsightsWorkbook = {
queries: WorkbookQuery[]
charts: WorkbookChart[]
dashboards: WorkbookDashboard[]
enable_auto_save?: boolean
}

export type WorkbookQuery = {
Expand Down
7 changes: 6 additions & 1 deletion frontend/src2/workbook/WorkbookNavbarActions.vue
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<script setup lang="ts">
import { Check, PanelRightClose, PanelRightOpen, Share2, Undo2 } from 'lucide-vue-next'
import { Check, PanelRightClose, PanelRightOpen, Pause, Play, Share2, Undo2 } from 'lucide-vue-next'
import { inject, ref } from 'vue'
import { Workbook, workbookKey } from './workbook'
import WorkbookShareDialog from './WorkbookShareDialog.vue'
Expand Down Expand Up @@ -45,6 +45,11 @@ const showShareDialog = ref(false)
<Dropdown
:button="{ icon: 'more-horizontal', variant: 'outline' }"
:options="[
{
label: workbook.doc.enable_auto_save ? 'Disable Auto Save' : 'Enable Auto Save',
icon: workbook.doc.enable_auto_save ? Pause : Play,
onClick: () => (workbook.doc.enable_auto_save = !workbook.doc.enable_auto_save),
},
{
label: workbook.showSidebar ? 'Hide Sidebar' : 'Show Sidebar',
icon: workbook.showSidebar ? PanelRightOpen : PanelRightClose,
Expand Down
20 changes: 19 additions & 1 deletion frontend/src2/workbook/workbook.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { watchDebounced } from '@vueuse/core'
import { call } from 'frappe-ui'
import { computed, InjectionKey, reactive, toRefs } from 'vue'
import { useRouter } from 'vue-router'
Expand All @@ -24,7 +25,6 @@ export default function useWorkbook(name: string) {
const href = window.location.href.replace(name, workbook.doc.name)
window.location.replace(href)
})
workbook.onAfterSave(() => createToast({ title: 'Saved', variant: 'success' }))

wheneverChanges(
() => workbook.doc,
Expand Down Expand Up @@ -229,6 +229,24 @@ export default function useWorkbook(name: string) {
return linkedQueries
}

let stopAutoSaveWatcher: any
wheneverChanges(
() => workbook.doc.enable_auto_save,
() => {
if (!workbook.doc.enable_auto_save && stopAutoSaveWatcher) {
stopAutoSaveWatcher()
stopAutoSaveWatcher = null
}
if (workbook.doc.enable_auto_save && !stopAutoSaveWatcher) {
stopAutoSaveWatcher = watchDebounced(
() => workbook.isdirty,
() => workbook.isdirty && workbook.save(),
{ immediate: true, debounce: 1000 }
)
}
}
)

return reactive({
...toRefs(workbook),
canShare,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
"title",
"queries",
"charts",
"dashboards"
"dashboards",
"enable_auto_save"
],
"fields": [
{
Expand All @@ -32,11 +33,17 @@
"fieldname": "dashboards",
"fieldtype": "JSON",
"label": "Dashboards"
},
{
"default": "0",
"fieldname": "enable_auto_save",
"fieldtype": "Check",
"label": "Enable Auto Save"
}
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2024-11-02 12:44:21.277499",
"modified": "2024-11-24 12:36:33.196238",
"modified_by": "Administrator",
"module": "Insights",
"name": "Insights Workbook",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ class InsightsWorkbook(Document):

charts: DF.JSON | None
dashboards: DF.JSON | None
enable_auto_save: DF.Check
name: DF.Int | None
queries: DF.JSON | None
title: DF.Data
Expand Down

0 comments on commit ab142ab

Please sign in to comment.