Skip to content

Commit

Permalink
Merge pull request #123 from Cambalab/97-routes-being-recreated-on-ho…
Browse files Browse the repository at this point in the history
…treload

Routes being recreated on hotreload
  • Loading branch information
sgobotta authored Jun 15, 2019
2 parents 6cdee5e + 153f98c commit f6f69d3
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 32 deletions.
34 changes: 21 additions & 13 deletions src/components/Resource/src/Resource.vue
Original file line number Diff line number Diff line change
Expand Up @@ -44,24 +44,23 @@ export default {
}
},
created: function() {
createCrudModule({
apiUrl: this.apiUrl,
resourceName: this.name,
resourceIdName: this.resourceIdName,
parseResponses: this.parseResponses,
store: this.$store
})
if (!this.storeHasModule(this.name)) {
createCrudModule({
apiUrl: this.apiUrl,
resourceName: this.name,
resourceIdName: this.resourceIdName,
parseResponses: this.parseResponses,
store: this.$store
})
}
},
methods: {
addRoute: function(path, name) {
addRoute: function(path, name, addedRouteCallback) {
const resourceName = "resources/addRoute"
this.$store.commit(resourceName, { path, name })
return this.$store.commit(resourceName, { path, name, addedRouteCallback })
},
loadRoutes() {
const resourcePath = `/${this.name}`
bindComponentsOnRoutes: function() {
const routes = []
// Adds the 'resourceName' path, mainly used for the drawer
this.addRoute(resourcePath, this.name)
// Initialises bindings to create the navigation routes
const bind = createRouteBindings({
list: this.list,
Expand All @@ -82,6 +81,15 @@ export default {
routes.push(bind.edit({ wrapper: Edit }))
// Adds the routes to the global router
this.$router.addRoutes(routes)
},
loadRoutes() {
const resourcePath = `/${this.name}`
// Adds the 'resourceName' path, mainly used for the drawer
// and if the route was successfully added then it bind the components with their route
this.addRoute(resourcePath, this.name, this.bindComponentsOnRoutes)
},
storeHasModule(moduleName) {
return !!this.$store.state[moduleName]
}
},
mounted: function() {
Expand Down
25 changes: 13 additions & 12 deletions src/components/Ui/src/ui.vue
Original file line number Diff line number Diff line change
Expand Up @@ -126,20 +126,21 @@ export default {
};
},
mounted() {
// Listen to addRoutes mutations
let whitelist = ["resources/addRoute"];
this.$store.subscribe(mutation => {
if (whitelist.includes(mutation.type)) {
this.menuItems[0].children.push({
icon: "list",
title: mutation.payload.name,
link: mutation.payload.path
});
}
});
this.mapCurrentRegisteredRoutes()
},
computed: {},
methods: {
// Listen to addRoutes mutations
mapCurrentRegisteredRoutes() {
let whitelist = ["resources/addRoute"];
this.$store.subscribe((mutation, state) => {
if (whitelist.includes(mutation.type)) {
const currentRoutes = state.resources.routes.map(route => {
return { icon: 'list', title: route.name, link: route.path }
})
this.menuItems[0].children = currentRoutes
}
});
}
}
};
</script>
Expand Down
15 changes: 10 additions & 5 deletions src/store/modules/resource.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,16 @@ export default {
routes: []
},
mutations: {
addRoute({ routes }, payload) {
routes.push({
path: payload.path,
name: payload.name
});
addRoute({ routes }, { path, name, addedRouteCallback }) {
let matchingPathRouteIndex
let newRoute = { path, name }
routes.forEach((route, index) => (route.name === name) && (matchingPathRouteIndex = index))
if (matchingPathRouteIndex !== undefined) {
routes[matchingPathRouteIndex] = newRoute
} else {
routes.push(newRoute)
addedRouteCallback()
}
}
},
getters: {
Expand Down
4 changes: 3 additions & 1 deletion tests/unit/factory/store/initial.mutations.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ export default ({ snapshot = 'default' }) => {
Resource: initMutationsForResource
}
const resourcesMutations = {
'resources/addRoute': () => {}
'resources/addRoute': (state, args) => {
args.addedRouteCallback && args.addedRouteCallback()
}
}

// Initialises default mutations
Expand Down
3 changes: 2 additions & 1 deletion tests/unit/specs/resource.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,8 @@ describe('Resource.vue', () => {
const { storeMethods } = resourceFixture.methods
const { params } = storeMethods[methodName]
expect(storeSpy.addRoute).toHaveBeenCalledTimes(1)
expect(storeSpy.addRoute).toHaveBeenCalledWith(methodName, params)
const addedRouteCallbackExpectation = { addedRouteCallback: expect.any(Function) }
expect(storeSpy.addRoute).toHaveBeenCalledWith(methodName, { ...params, ...addedRouteCallbackExpectation })
})

it('should have the store initialised with {initialResources} getters', () => {
Expand Down

0 comments on commit f6f69d3

Please # to comment.