Skip to content

Commit

Permalink
Add changes detection to record editing
Browse files Browse the repository at this point in the history
  • Loading branch information
kelanik8 committed Aug 2, 2023
1 parent 7e7d92f commit 8fbe512
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 2 deletions.
4 changes: 4 additions & 0 deletions client/web/compose/src/mixins/record.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export default {
processingUndelete: false,
processingSubmit: false,
record: undefined,
initialRecordState: undefined,
errors: new validator.Validated(),
}
},
Expand Down Expand Up @@ -144,6 +145,7 @@ export default {
})
.then(record => {
this.record = new compose.Record(this.module, record)
this.initialRecordState = this.record.clone()
})
.then(() => this.dispatchUiEvent('afterFormSubmit', this.record, { $records: records }))
.then(() => this.updatePrompts())
Expand Down Expand Up @@ -194,6 +196,7 @@ export default {
})
.then(record => {
this.record = new compose.Record(this.module, record)
this.initialRecordState = this.record.clone()
})
.then(() => this.dispatchUiEvent('beforeFormSubmit', this.record))
.then(() => this.updatePrompts())
Expand Down Expand Up @@ -296,6 +299,7 @@ export default {
this.onModalHide()
this.fields = []
this.record = new compose.Record(this.module, {})
this.initialRecordState = this.record.clone()
this.$emit('save')
})
.catch(this.toastErrorHandler(this.$t('notification:record.deleteBulkRecordUpdateFailed')))
Expand Down
17 changes: 17 additions & 0 deletions client/web/compose/src/views/Admin/Modules/Records/View.vue
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
</template>

<script>
import { isEqual } from 'lodash'
import { mapGetters } from 'vuex'
import RecordToolbar from 'corteza-webapp-compose/src/components/Common/RecordToolbar'
import users from 'corteza-webapp-compose/src/mixins/users'
Expand Down Expand Up @@ -220,6 +221,14 @@ export default {
this.createBlocks()
},
beforeRouteLeave (to, from, next) {
this.checkUnsavedChanges(next, to)
},
beforeRouteUpdate (to, from, next) {
this.checkUnsavedChanges(next, to)
},
methods: {
createBlocks () {
this.fields.forEach(f => {
Expand All @@ -240,6 +249,7 @@ export default {
.recordRead({ namespaceID, moduleID, recordID })
.then(record => {
this.record = new compose.Record(module, record)
this.initialRecordState = this.record.clone()
})
.catch(this.toastErrorHandler(this.$t('notification:record.loadFailed')))
}
Expand Down Expand Up @@ -268,6 +278,13 @@ export default {
params: { ...this.$route.params, recordID },
})
},
checkUnsavedChanges (next, to) {
const recordValues = JSON.parse(JSON.stringify(this.record.values))
const initialRecordState = JSON.parse(JSON.stringify(this.initialRecordState.values))
next(!isEqual(recordValues, initialRecordState) ? window.confirm(this.$t('general:editor.unsavedChanges')) : true)
},
},
}
</script>
Expand Down
19 changes: 18 additions & 1 deletion client/web/compose/src/views/Public/Pages/Records/View.vue
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@
</template>

<script>
import { isEqual } from 'lodash'
import { mapGetters, mapActions } from 'vuex'
import Grid from 'corteza-webapp-compose/src/components/Public/Page/Grid'
import RecordToolbar from 'corteza-webapp-compose/src/components/Common/RecordToolbar'
Expand Down Expand Up @@ -255,6 +256,7 @@ export default {
immediate: true,
handler () {
this.record = undefined
this.initialRecordState = undefined
this.loadRecord().then(() => {
this.determineLayout()
})
Expand Down Expand Up @@ -292,7 +294,11 @@ export default {
// Destroy event before route leave to ensure it doesn't destroy the newly created one
beforeRouteLeave (to, from, next) {
this.$root.$off('refetch-record-blocks')
next()
this.checkUnsavedChanges(next, to)
},
beforeRouteUpdate (to, from, next) {
this.checkUnsavedChanges(next, to)
},
methods: {
Expand All @@ -315,13 +321,15 @@ export default {
return this.$ComposeAPI.recordRead({ namespaceID, moduleID, recordID })
.then(record => {
this.record = new compose.Record(module, record)
this.initialRecordState = this.record.clone()
})
.catch(e => {
this.toastErrorHandler(this.$t('notification:record.loadFailed'))(e)
this.handleBack()
})
} else {
this.record = new compose.Record(module, {})
this.initialRecordState = this.record.clone()
}
}
},
Expand Down Expand Up @@ -352,6 +360,7 @@ export default {
this.inEditing = true
this.inCreating = true
this.record = new compose.Record(this.module, { values: this.values })
this.initialRecordState = this.record.clone()
if (!this.showRecordModal) {
this.$router.push({ name: 'page.record.create', params: this.newRouteParams })
}
Expand All @@ -361,6 +370,7 @@ export default {
this.inEditing = true
this.inCreating = true
this.record = new compose.Record(this.module, { values: this.record.values })
this.initialRecordState = this.record.clone()
if (!this.showRecordModal) {
this.$router.push({ name: 'page.record.create', params: { pageID: this.page.pageID, values: this.record.values } })
}
Expand Down Expand Up @@ -474,6 +484,13 @@ export default {
return block
})
},
checkUnsavedChanges (next, to) {
const recordValues = JSON.parse(JSON.stringify(this.record.values))
const initialRecordState = JSON.parse(JSON.stringify(this.initialRecordState.values))
next(!isEqual(recordValues, initialRecordState) ? window.confirm(this.$t('general:editor.unsavedChanges')) : true)
},
},
}
</script>
4 changes: 3 additions & 1 deletion locale/en/corteza-webapp-compose/general.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -173,4 +173,6 @@ variants:
info: Info
success: Success
danger: Danger
warning: Warning
warning: Warning
editor:
unsavedChanges: Unsaved changes will be lost. Do you wish to leave the page?

0 comments on commit 8fbe512

Please sign in to comment.