Skip to content

Commit

Permalink
delegate button actions to parent & add API active toggle logic
Browse files Browse the repository at this point in the history
  • Loading branch information
cydrobolt committed Dec 12, 2015
1 parent c48bcc4 commit e58cd81
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .jshintrc
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"esnext": true
}
19 changes: 19 additions & 0 deletions app/Http/Controllers/AjaxController.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,24 @@ public function toggleAPIActive(Request $request) {
if (!$this->currIsAdmin()) {
abort(401, 'User not admin.');
}

$user_to_toggle = $request->input('user_id');
$user = User::where('id', $user_id)
->where('active', 1)
->first();
if (!$user) {
abort(404, 'User not found.');
}
$current_status = $user->api_active;

if ($current_status == 1) {
$new_status = 0;
}
else {
$new_status = 1;
}

$user->api_active = $new_status;
$user->save();
}
}
4 changes: 2 additions & 2 deletions app/Http/routes.php
Original file line number Diff line number Diff line change
Expand Up @@ -35,5 +35,5 @@
$app->post('/shorten', ['as' => 'shorten', 'uses' => 'LinkController@performShorten']);

/* API endpoints */
$app->post('/api/v2/link_avail_check', ['as' => 'link_check', 'uses' => 'AjaxController@checkLinkAvailability']);
$app->post('/api/v2/admin/toggle_api_active', ['as' => 'link_check', 'uses' => 'AjaxController@toggleAPIActive']);
$app->post('/api/v2/link_avail_check', ['as' => 'api_link_check', 'uses' => 'AjaxController@checkLinkAvailability']);
$app->post('/api/v2/admin/toggle_api_active', ['as' => 'api_toggle_api_active', 'uses' => 'AjaxController@toggleAPIActive']);
7 changes: 3 additions & 4 deletions public/js/admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ $(function () {
{{else}}
False
{{/if}}
- <a href='#' data-user-id='{{user_id}}' class='toggle-api-active' class='btn btn-xs btn-success'>Active (click to toggle)</a>
- <a data-user-id='{{user_id}}' class='toggle-api-active btn btn-xs btn-success'>Active (click to toggle)</a>
</p>
<p>
<span>API Key: <code>{{api_key}}</code></span>
Expand All @@ -62,6 +62,7 @@ $(function () {
api_key: api_key,
api_active: api_active,
api_quota: api_quota,
user_id: user_id,
title: "API Information for " + username,
body: markup
};
Expand All @@ -74,12 +75,10 @@ $(function () {
$('.activate-edit-modal').click(function () {
// activate modal
});

$('.toggle-api-active').click(function () {
$('body').delegate('.toggle-api-active', 'click', function () {
var toggle_user_id = $(this).data('user-id');
apiCall('admin/toggle_api_active', {
'user_id': toggle_user_id,
});
});

});
3 changes: 2 additions & 1 deletion public/js/api.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
function apiCall(path, data) {
var base_api_path = '/api/v2/';
var base_api_path = BASE_API_PATH;
var api_path = base_api_path + path;
console.log('api call');
$.ajax({
url: api_path,
data: data
Expand Down
3 changes: 3 additions & 0 deletions public/js/constants.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
/* Polr JS Constants */

const BASE_API_PATH = '/api/v2/';
2 changes: 2 additions & 0 deletions resources/views/layouts/base.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
<link rel="stylesheet" href="/css/base.css" />
<link href="/css/font-awesome.min.css" rel="stylesheet">
<link rel="shortcut icon" href="favicon.ico" />

<script src='/js/constants.js'></script>
<script src="/js/jquery-1.11.3.min.js"></script>
<script src="/js/bootstrap.min.js"></script>
@yield('css')
Expand Down

0 comments on commit e58cd81

Please sign in to comment.