Skip to content

Commit

Permalink
feat: Adds ability for users to cancel their own design requests; opt…
Browse files Browse the repository at this point in the history
…ion by way of config toggle.
  • Loading branch information
preimpression committed May 18, 2024
1 parent a93a527 commit 8b196a9
Show file tree
Hide file tree
Showing 6 changed files with 98 additions and 14 deletions.
47 changes: 47 additions & 0 deletions app/Http/Controllers/Characters/DesignController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ public function getDesignUpdate($id) {

return view('character.design.request', [
'request' => $r,
'canCancel' => config('lorekeeper.extensions.design_return_to_draft')
]);
}

Expand Down Expand Up @@ -363,4 +364,50 @@ public function postDelete(DesignUpdateManager $service, $id) {

return redirect()->to('designs');
}

/**
* Shows the design update request cancellation confirmation modal for a user.
*
* @param int $id
*
* @return \Illuminate\Contracts\Support\Renderable
*/
public function getCancel($id) {
$r = CharacterDesignUpdate::find($id);
if (!$r || ($r->user_id != Auth::user()->id && !Auth::user()->hasPower('manage_characters'))) {
abort(404);
}

return view('character.design._cancel_modal', [
'request' => $r,
]);
}

/**
* Cancels a design update request and returns it to drafts.
*
* @param App\Services\DesignUpdateManager $service
* @param int $id
*
* @return \Illuminate\Http\RedirectResponse
*/
public function postCancel(DesignUpdateManager $service, $id) {
$r = CharacterDesignUpdate::find($id);
if (!$r) {
abort(404);
}
if ($r->user_id != Auth::user()->id) {
abort(404);
}

if ($service->cancelRequest(null, $r, Auth::user(), true)) {
flash('Request canceled successfully.')->success();
} else {
foreach ($service->errors()->getMessages()['error'] as $error) {
flash($error)->error();
}
}

return redirect()->back();
}
}
33 changes: 20 additions & 13 deletions app/Services/DesignUpdateManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -802,7 +802,7 @@ public function rejectRequest($data, $request, $user, $forceReject = false, $not
*
* @return bool
*/
public function cancelRequest($data, $request, $user) {
public function cancelRequest($data, $request, $user, $self = null) {
DB::beginTransaction();

try {
Expand All @@ -819,21 +819,28 @@ public function cancelRequest($data, $request, $user) {
// to add a comment to it. Status is returned to Draft status.
// Use when rejecting a request that just requires minor modifications to approve.

// Set staff comment and status
$request->staff_id = $user->id;
$request->staff_comments = $data['staff_comments'] ?? null;
if($self == null){
// Set staff comment if this is not a self-cancel
$request->staff_id = $user->id;
$request->staff_comments = $data['staff_comments'] ?? null;
if (!isset($data['preserve_queue'])) {
$request->submitted_at = null;
}
} else $request->submitted_at = null;

// Set status
$request->status = 'Draft';
if (!isset($data['preserve_queue'])) {
$request->submitted_at = null;
}
$request->save();

// Notify the user
Notifications::create('DESIGN_CANCELED', $request->user, [
'design_url' => $request->url,
'character_url' => $request->character->url,
'name' => $request->character->fullName,
]);
if($self == null){
// Notify the user if it is not being canceled by the user themself.
// Note that an admin canceling their own will also not result in a notification
Notifications::create('DESIGN_CANCELED', $request->user, [
'design_url' => $request->url,
'character_url' => $request->character->url,
'name' => $request->character->fullName,
]);
}

return $this->commitReturn(true);
} catch (\Exception $e) {
Expand Down
3 changes: 3 additions & 0 deletions config/lorekeeper/extensions.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,7 @@
// Show Small Badges on the User's Characters/MYO Slots Page
// Indicating Trading Status (and Gift Art & Gift Writing Status)
'badges_on_user_character_page' => 0,

// Allow users to return a pending design update to drafts, for instance if they make a mistake. - Uri
'design_return_to_draft' => 1,
];
9 changes: 9 additions & 0 deletions resources/views/character/design/_cancel_modal.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
@if ($request->user_id == Auth::user()->id)
<p>This will cancel the pending design approval request and will send it back to your drafts. <br>
You will need to resubmit in order for it to be in the queue again.</p>
<p>Are you sure you want to cancel this request?</p>
{!! Form::open(['url' => 'designs/' . $request->id . '/cancel', 'class' => 'text-right']) !!}
{!! Form::submit('Cancel Request', ['class' => 'btn btn-primary']) !!}
@else
<div>You cannot cancel this request.</div>
@endif
17 changes: 16 additions & 1 deletion resources/views/character/design/request.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,15 @@
<p>
This request is in the approval queue.
@if (!Auth::user()->hasPower('manage_characters'))
Please wait for it to be processed.
Please wait for it to be processed{{ isset($canCancel) && $canCancel ? ' or cancel it below.' : '.' }}
@if(isset($canCancel) && $canCancel)
<div class="card mb-3">
<div class="card-body">
<a href="#" class="btn btn-outline-secondary cancel-button btn-sm float-right" data-action="cancel">Cancel</a>
<strong class="text-secondary">Cancelling</strong> the request returns it to its draft status, allowing you to make further edits.
</div>
</div>
@endif
@else
As a staff member with the ability to edit the masterlist, you can view the details of the request, but can only edit certain parts of it.
@endif
Expand Down Expand Up @@ -81,6 +89,13 @@
});
@endif
@if ($request->user_id == Auth::user()->id && $request->status == 'Pending' && isset($canCancel) && $canCancel)
$('.cancel-button').on('click', function(e) {
e.preventDefault();
loadModal("{{ url('designs/' . $request->id . '/cancel/') }}", 'Cancel Submission');
});
@endif
@if (Auth::user()->hasPower('manage_characters'))
$('.process-button').on('click', function(e) {
e.preventDefault();
Expand Down
3 changes: 3 additions & 0 deletions routes/lorekeeper/members.php
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,9 @@

Route::get('{id}/delete', 'DesignController@getDelete');
Route::post('{id}/delete', 'DesignController@postDelete');

Route::get('{id}/cancel', 'DesignController@getCancel');
Route::post('{id}/cancel', 'DesignController@postCancel');
});

/**************************************************************************************************
Expand Down

0 comments on commit 8b196a9

Please sign in to comment.