Skip to content
This repository was archived by the owner on Apr 17, 2022. It is now read-only.

Commit

Permalink
WIP: update features activation
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelandrieu committed Feb 26, 2017
1 parent d89f1b5 commit 0f7deb6
Show file tree
Hide file tree
Showing 6 changed files with 60 additions and 4 deletions.
4 changes: 3 additions & 1 deletion app/Resources/views/default/configuration.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,9 @@
<h1>Configuration of features</h1>

<div id="app">
<list v-bind:url="'{{ path('api_actions_all') }}'"
<list
v-bind:url="'{{ path('api_actions_all') }}'"
v-bind:update_url="'{{ path('update_settings') }}'"
></list>
</div>
</div>
Expand Down
8 changes: 8 additions & 0 deletions src/AppBundle/BotAction/BotActionRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,14 @@ public function __construct(EntityManager $em)
$this->em = $em;
}

public function find($id)
{
$query = $this->em->createQuery('SELECT a FROM '.BotAction::class.' a WHERE a.id = :id ');
$query->setParameter('id', $id);

return $query->getSingleResult();
}

public function findAll()
{
return $this->em->createQuery('SELECT a FROM '.BotAction::class.' a')
Expand Down
2 changes: 1 addition & 1 deletion src/AppBundle/Controller/Api/BotController.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public function getAllAction()
*/
public function getAction($id)
{
$actionBot = $this->getDoctrine($id)
$actionBot = $this->getDoctrine()
->getRepository('AppBundle\BotAction\BotAction')
->find($id)
;
Expand Down
31 changes: 31 additions & 0 deletions src/AppBundle/Controller/ConfigurationController.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,11 @@

namespace AppBundle\Controller;

use AppBundle\BotAction\BotAction;
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;

class ConfigurationController extends Controller
{
Expand All @@ -14,4 +17,32 @@ public function configureAction()
{
return $this->render('default/configuration.html.twig', []);
}

/**
* @Route("/updateSettings", name="update_settings")
*/
public function updateSettings(Request $request)
{
if ($request->isMethod('POST')) {
$botActions = $request->request->all();
$em = $this->getDoctrine()->getManager();

// @todo: to be extracted
foreach ($botActions as $botId => $botEnabled) {
$botAction = $this->get('app.bot_action.repository')->find($botId);

if ($botAction instanceof BotAction) {
if ($botEnabled) {
$botAction->enable();
}else {
$botAction->disable();
}

$em->flush($botAction);
}
}

return new Response();
}
}
}
2 changes: 2 additions & 0 deletions src/AppBundle/Security/GitHubTokenValidatorSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ public static function getSubscribedEvents()

public function onKernelRequest(GetResponseEvent $event)
{
// @todo: we need to disable this subscriber
return;
$request = $event->getRequest();

if (!$request->isMethod('POST')) {
Expand Down
17 changes: 15 additions & 2 deletions web/js/configuration-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@ Vue.component('list', {
url: {
type: String,
required: true
},
update_url: {
type: String,
required: true
}
},
data: function () {
Expand All @@ -29,7 +33,16 @@ Vue.component('list', {
req.send();
},
updateSettings: function () {
console.log(this.actions);
var self = this;
var req = new XMLHttpRequest();
req.open('POST', this.update_url, true);
req.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded; charset=UTF-8');
var formData = '';
self.actions.map(function(action) {
formData+= action.id+"="+action.enabled+"&";
});

req.send(formData);
}
},
template: '#list-template',
Expand All @@ -49,4 +62,4 @@ var ConfigurationPage = new Vue({
props: {},
filters: {},
methods: {},
});
});

0 comments on commit 0f7deb6

Please sign in to comment.