I’m inserting a button on the header that adds a user to a group. This is working, but making an ajax
call to do so just doesn’t seem right. It would seem like I could “just” somehow call the Group
model and do it there, but I can’t find an example that does it.
This is in a plugin…
I guess there must be some way that I can make the demoGroup
into a model rather than just a dict
?
const demoServerGroupName = siteSettings.pfaffmanager_demo_server_group;
const groups = Site.currentProp("groups");
const router = container.lookup("router:main");
const demoGroup = groups.find((g) => {
return g.name === demoServerGroupName;
});
const hasDemoServer = servers.find((g) => {
return g.hostname.includes("pfaffmanagerdemo");
});
if (demoGroup && !hasDemoServer) {
api.decorateWidget("header-buttons:before", (helper) => {
const addToDemoGroup = function () {
window.console.log("need to add to group", demoGroup);
const groupId = demoGroup.id;
const url = `/groups/${groupId}/join.json`;
ajax(url, {
type: "PUT",
data: currentUser.username,
})
.then(() => {
router.transitionTo("/pfaffmanager/servers"); //TODO: refer to the route?
})
.catch(popupAjaxError);
};
return helper.h(
"button#newDemoServer",
{
className: "btn btn-text btn-primary create headerLink",
title: I18n.t("pfaffmanager.server.create_demo_server_title"),
onclick: addToDemoGroup,
},
I18n.t("pfaffmanager.server.create_demo_server_short")
);
});
}