Skip to content

Commit

Permalink
refactor: added mixin-base-context from KDK
Browse files Browse the repository at this point in the history
  • Loading branch information
cnouguier committed Jan 14, 2025
1 parent ba12ead commit 2d45f18
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/components/Context.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@

<script>
import _ from 'lodash'
import { Theme, mixins } from '@kalisio/kdk/core.client'
import { Theme } from '@kalisio/kdk/core.client'
import mixins from '../mixins'
export default {
name: 'context',
Expand Down
2 changes: 2 additions & 0 deletions src/mixins/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import { baseContext } from './mixin.base-context.js'
import events from './mixin.events'

export default {
baseContext,
events
}
54 changes: 54 additions & 0 deletions src/mixins/mixin.base-context.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
export const baseContext = {
props: {
contextId: {
type: String,
default: ''
}
},
data () {
return {
contextLoaded: false
}
},
watch: {
$route (to, from) {
// React to route changes but reusing the same component as this one is generic
this.refreshContext()
}
},
methods: {
clearContext () {
this.$store.set('context', null)
this.contextLoaded = false
},
setContext (context) {
// Set context in store so that contextual services are aware of it
this.$store.set('context', context)
this.contextLoaded = true
},
async refreshContext () {
if (this.contextId) {
// Context already set ?
let context = this.$store.get('context')
if (context && context._id === this.contextId) {
return
}
// Otherwise clear so that underlying components will be destroyed
this.clearContext()
// Then update the context
context = await this.service.get(this.contextId)
this.setContext(context)
} else {
this.clearContext()
}
}
},
created () {
this.service = this.$api.getService(this.$config('context.service'))
// Register the context
this.refreshContext()
},
beforeUnmount () {
this.clearContext()
}
}

0 comments on commit 2d45f18

Please sign in to comment.