Thanks for the report
I’ve updated the widget so that it works with the latest. And added some other minor fixes.
https://github.com/angusmcleod/layouts-profile/commit/0fb6caa459754cd798d7bfd858390ce95201a153
As it sounds like you’ve already read my previous explanation of how to do this and given it a shot, here’s some working methods with an explanation of what is going on. Add the methods to your version of the profile widget and see how you go.
First, let’s add a new button widget to the actions array, at the bottom of the html()
method (before actions
is pushed to contents
).
actions.push(this.attach('button', {
label: 'topic.create',
icon: 'plus',
action: 'createTopic',
className: 'btn-small'
}));
Then add the createTopic
method as a new method in the widget. I’ve added some comments to explain what’s going on. I’ve assumed that you want the button to work in both discovery (i.e. topic lists) and topic routes. This method is designed to work in both.
createTopic() {
// First we get the composer controller, as that is what we'll need
// to open the composer with. As we want to also check for existing
// drafts and the current discovery category, we'll also get the
// discovery controller.
const discoveryController = this.register.lookup('controller:discovery');
const composerController = this.register.lookup('controller:composer');
// I'm assuming the action you want to take is createTopic. The other
// actions you can take are listed at the top of the composer model.
let params = { action: 'createTopic' };
// Here we load the relevant composer params from the discovery controller
// (they may not exist though, e.g. if we loaded a topic directly)
const categoryId = discoveryController.get('category.id');
const draftKey = discoveryController.get('model.draft_key');
const draftSequence = discoveryController.get('model.draft_sequence');
// let's assign the topic if we're in a topic
const topic = this.attrs.topic;
// First we'll set the initial category in the compose category selector.
// You could set this to any category id you wanted. But let's set it
// to the topic category if we're in a topic and, failing that, the
// discovery category if there is one.
if (topic && topic.get('category')) {
params['categoryId'] = topic.get('category.id');
} else if (categoryId) {
params['categoryId'] = categoryId;
}
// if there is a draft new topic in discovery, load it, otherwise start
// a new one.
if (draftKey && draftSequence) {
params['draftKey'] = draftKey;
params['draftSequence'] = draftSequence;
} else {
params['draftKey'] = 'new_topic';
}
// Finally, let's open the composer with our params!
composerController.open(params);
},
Now we’ve got a New Topic button in the profile widget.
If you wanted a createTopic
button that only works in discovery routes, that’s actually a lot simpler, as you just need to send createTopic
to route:discovery
and it will handle the rest. However, as you seem to be using the profile widget in both discovery and topic contexts, the above method will work in both.