Skip to content

Commit

Permalink
#77 You can now delete teams. Also got rid of 'prompt' dialogs, repla…
Browse files Browse the repository at this point in the history
…ced with Noty instead.
  • Loading branch information
Wotuu committed Apr 29, 2019
1 parent ccc850d commit e1f54e1
Show file tree
Hide file tree
Showing 7 changed files with 123 additions and 19 deletions.
25 changes: 22 additions & 3 deletions app/Http/Controllers/TeamController.php
Expand Up @@ -61,8 +61,10 @@ public function store($request, Team $team = null)
}
}

// If saving team + logo was successful, save our own user as its first member
$team->addMember(Auth::user(), 'admin');
if ($new) {
// If saving team + logo was successful, save our own user as its first member
$team->addMember(Auth::user(), 'admin');
}
}

return $team;
Expand All @@ -86,6 +88,23 @@ public function edit(Request $request, Team $team)
return view('team.edit', ['model' => $team]);
}

/**
* @param Request $request
* @param Team $team
* @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
*/
public function delete(Request $request, Team $team)
{
if ($team->isUserMember(Auth::user()) && $team->getUserRole(Auth::user()) === 'admin') {
try {
$team->delete();
} catch (\Exception $ex) {
abort(500);
}
}
return redirect()->route('team.list');
}

/**
* @param TeamFormRequest $request
* @param Team $team
Expand Down Expand Up @@ -128,7 +147,7 @@ public function savenew(TeamFormRequest $request)
public function list()
{
$user = Auth::user();
return view('team.list', ['models' => $user->teams()->where('user_id', $user->id)->get()]);
return view('team.list', ['models' => $user->teams]);
}

/**
Expand Down
31 changes: 26 additions & 5 deletions app/Models/Team.php
Expand Up @@ -216,11 +216,14 @@ public function isUserMember($user)
*/
public function addMember($user, $role)
{
$teamUser = new TeamUser();
$teamUser->team_id = $this->id;
$teamUser->user_id = $user->id;
$teamUser->role = $role;
$teamUser->save();
// Prevent duplicate member listings
if (!$this->isUserMember($user)) {
$teamUser = new TeamUser();
$teamUser->team_id = $this->id;
$teamUser->user_id = $user->id;
$teamUser->role = $role;
$teamUser->save();
}
}

/**
Expand All @@ -239,4 +242,22 @@ public static function generateRandomInviteCode()

return $newKey;
}

public static function boot()
{
parent::boot();

// Delete route properly if it gets deleted
static::deleting(function ($item) {
/** @var $item Team */

// Delete icons
$item->iconfile->delete();

// Remove all users associated with this team
TeamUser::where('team_id', $item->id)->delete();
// Unassign all routes from this team
DungeonRoute::where('team_id', $item->id)->update(['team_id' => -1]);
});
}
}
4 changes: 2 additions & 2 deletions resources/assets/js/custom/inline/dungeonroute/table.js
Expand Up @@ -341,7 +341,7 @@ class DungeonrouteTable extends InlineCode {
* @private
*/
_promptDeleteDungeonRoute(clickEvent) {
if (confirm(lang.get('messages.route_delete_confirm'))) {
showConfirmYesCancel(lang.get('messages.route_delete_confirm'), function () {
let publicKey = $(clickEvent.target).data('publickey');

$.ajax({
Expand All @@ -354,7 +354,7 @@ class DungeonrouteTable extends InlineCode {
$('#dungeonroute_filter').trigger('click');
}
});
}
});

// Prevent clicking delete from opening the route after it returns
clickEvent.preventDefault();
Expand Down
32 changes: 32 additions & 0 deletions resources/assets/js/custom/inline/layouts/app.js
Expand Up @@ -206,6 +206,38 @@ function _showNotification(opts) {
}, opts)).show();
}

function _showConfirm(opts) {
let n = new Noty($.extend({
theme: 'bootstrap-v4',
layout: 'center',
modal: true
}, opts));
n.show();
}

function showConfirmYesCancel(text, yesCallback, noCallback, opts = {}) {
_showConfirm($.extend({
type: 'info',
text: text,
buttons: [
Noty.button(lang.get('messages.yes_label'), 'btn btn-success', function (n) {
if (typeof yesCallback === 'function') {
yesCallback();
}
n.close();
}, {id: 'yes-button', 'data-status': 'ok'}),

Noty.button(lang.get('messages.cancel_label'), 'btn btn-danger', function (n) {
if (typeof noCallback === 'function') {
noCallback();
}
n.close();
})
]
}, opts)
);
}

/**
* Shows a success notification message.
* @param text The text to display.
Expand Down
10 changes: 8 additions & 2 deletions resources/lang/en/js.php
Expand Up @@ -174,6 +174,9 @@
'team_member' => 'View routes',
'remove_label' => 'Remove',

'team_add_route_successful' => 'Route added to team successfully',
'team_remove_route_successful' => 'Route removed from team successfully',

// Dungeonroute table headers
'preview_label' => 'Preview',
'title_label' => 'Title',
Expand All @@ -190,6 +193,9 @@
'add_route_label' => 'Add',
'remove_route_label' => 'Remove',

'team_add_route_successful' => 'Route added to team successfully',
'team_remove_route_successful' => 'Route removed from team successfully',
'yes_label' => 'Yes',
'no_label' => 'No',
'cancel_label' => 'Cancel',

'delete_team_confirm_label' => 'Are you sure you want to IRREVERSIBLY delete this team and its user/route associations?'
];
39 changes: 32 additions & 7 deletions resources/views/team/edit.blade.php
Expand Up @@ -37,7 +37,7 @@
$(function () {
let code = _inlineManager.getInlineCode('dungeonroute/table');
// Add route to team button
$('#add_route_btn').bind('click', function(){
$('#add_route_btn').bind('click', function () {
let tableView = code.getTableView();
tableView.setAddMode(true);
Expand All @@ -47,7 +47,7 @@
});
// Cancel button when done adding routes
$('#view_existing_routes').bind('click', function(){
$('#view_existing_routes').bind('click', function () {
let tableView = code.getTableView();
tableView.setAddMode(false);
Expand All @@ -56,6 +56,17 @@
$('#add_route_btn').show();
});
$('#delete_team').bind('click', function(clickEvent){
showConfirmYesCancel(lang.get('messages.delete_team_confirm_label'), function(){
// Change the method to DELETE
$('#details [name="_method"]').val('DELETE');
// Submit the form
$('#details form').submit();
}, null, {type: 'error'});
clickEvent.preventDefault();
});
let columns = [{
'targets': 2,
Expand Down Expand Up @@ -221,8 +232,10 @@ function _getIcon(roleName) {
@isset($model)
<div class="tab-pane fade show active" id="routes" role="tabpanel" aria-labelledby="routes-tab">
<div class="form-group">
<button id="add_route_btn" class="btn btn-success col-md-4"><i class="fas fa-plus"></i> {{ __('Add route') }}</button>
<button id="view_existing_routes" class="btn btn-danger col-md-4" style="display: none;"><i class="fas fa-times"></i> {{ __('Cancel') }}</button>
<button id="add_route_btn" class="btn btn-success col-md-4"><i
class="fas fa-plus"></i> {{ __('Add route') }}</button>
<button id="view_existing_routes" class="btn btn-danger col-md-4" style="display: none;"><i
class="fas fa-times"></i> {{ __('Cancel') }}</button>
</div>

<div class="form-group mt-2">
Expand All @@ -245,7 +258,7 @@ function _getIcon(roleName) {
['id' => 'team_members_invite_link', 'class' => 'form-control', 'readonly' => 'readonly']) !!}
<div class="input-group-append">
<button id="team_invite_link_copy_to_clipboard" class="btn btn-info"
data-toggle="tooltip" title="{{ __('Copy to clipboard') }}">
data-toggle="tooltip" title="{{ __('Copy to clipboard') }}">
<i class="far fa-copy"></i>
</button>
</div>
Expand Down Expand Up @@ -277,7 +290,8 @@ function _getIcon(roleName) {
</div>
@endisset

<div class="tab-pane fade @if(!isset($model)) show active @endif" id="details" role="tabpanel" aria-labelledby="details-tab">
<div class="tab-pane fade @if(!isset($model)) show active @endif" id="details" role="tabpanel"
aria-labelledby="details-tab">
@isset($model)
{{ Form::model($model, ['route' => ['team.update', $model->id], 'method' => 'patch', 'files' => true]) }}
@else
Expand Down Expand Up @@ -306,7 +320,18 @@ function _getIcon(roleName) {
</div>
@endif

{!! Form::submit(isset($model) ? __('Save') : __('Submit'), ['class' => 'btn btn-info']) !!}
<div class="row">
<div class="col">
{!! Form::submit(isset($model) ? __('Save') : __('Submit'), ['class' => 'btn btn-info']) !!}
</div>
<div class="col">
@if($model->getUserRole(Auth::user()) === 'admin')
<button id="delete_team" class="btn btn-danger float-right">
<i class="fas fa-trash"></i> {{ __('Delete team') }}
</button>
@endif
</div>
</div>

{!! Form::close() !!}
</div>
Expand Down
1 change: 1 addition & 0 deletions routes/web.php
Expand Up @@ -132,6 +132,7 @@
Route::get('teams', 'TeamController@list')->name('team.list');
Route::get('team/new', 'TeamController@new')->name('team.new');
Route::get('team/{team}', 'TeamController@edit')->name('team.edit');
Route::get('team/{team}', 'TeamController@delete')->name('team.delete');

Route::post('team/new', 'TeamController@savenew')->name('team.savenew');
Route::patch('team/{team}', 'TeamController@update')->name('team.update');
Expand Down

0 comments on commit e1f54e1

Please sign in to comment.