Skip to content

Commit

Permalink
Refactor the statuses to be set by PHP
Browse files Browse the repository at this point in the history
  • Loading branch information
PavlosIsaris committed Nov 13, 2024
1 parent 1ddb3a5 commit 5dc97cf
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@

use App\BusinessLogicLayer\CrowdSourcingProject\CrowdSourcingProjectManager;
use App\BusinessLogicLayer\CrowdSourcingProject\CrowdSourcingProjectTranslationManager;
use App\BusinessLogicLayer\lkp\CrowdSourcingProjectStatusLkp;
use App\Models\CrowdSourcingProject\Problem\CrowdSourcingProjectProblem;
use App\Models\CrowdSourcingProject\Problem\CrowdSourcingProjectProblemTranslation;
use App\Repository\CrowdSourcingProject\Problem\CrowdSourcingProjectProblemRepository;
use App\Repository\LanguageRepository;
use App\Utils\FileUploader;
use App\ViewModels\CrowdSourcingProject\Problem\CreateEditProblem;
use App\ViewModels\CrowdSourcingProject\Problem\CrowdSourcingProjectProblemsLandingPage;
use Illuminate\Support\Collection;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Str;

Expand Down Expand Up @@ -113,4 +115,29 @@ public function storeProblem(array $attributes): int {
public function deleteProblem(int $id): bool {
return $this->crowdSourcingProjectProblemRepository->delete($id);
}

public function getProblemStatusesForManagementPage(): Collection {
$problemStatuses = $this->crowdSourcingProjectProblemStatusManager->getAllCrowdSourcingProjectProblemStatusesLkp();
foreach ($problemStatuses as $problemStatus) {
switch ($problemStatus->id) {
case CrowdSourcingProjectStatusLkp::DRAFT:
$problemStatus->badgeCSSClass = 'badge-secondary';
break;
case CrowdSourcingProjectStatusLkp::PUBLISHED:
$problemStatus->badgeCSSClass = 'badge-success';
break;
case CrowdSourcingProjectStatusLkp::FINALIZED:
$problemStatus->badgeCSSClass = 'badge-info';
break;
case CrowdSourcingProjectStatusLkp::UNPUBLISHED:
$problemStatus->badgeCSSClass = 'badge-danger';
break;
default:
$problemStatus->badgeCSSClass = 'badge-dark';
$problemStatus->description = 'The problem is in an unknown status';
}
}

return $problemStatuses;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use App\Repository\CrowdSourcingProject\Problem\CrowdSourcingProjectProblemStatusLkpRepository;

class CrowdSourcingProjectProblemStatusManager {
protected $crowdSourcingProjectProblemStatusLkpRepository;
protected CrowdSourcingProjectProblemStatusLkpRepository $crowdSourcingProjectProblemStatusLkpRepository;

public function __construct(CrowdSourcingProjectProblemStatusLkpRepository $crowdSourcingProjectProblemStatusLkpRepository) {
$this->crowdSourcingProjectProblemStatusLkpRepository = $crowdSourcingProjectProblemStatusLkpRepository;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use App\BusinessLogicLayer\CrowdSourcingProject\Problem\CrowdSourcingProjectProblemManager;
use App\Http\Controllers\Controller;
use Illuminate\Database\Eloquent\ModelNotFoundException;
use Illuminate\Http\JsonResponse;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;
Expand Down Expand Up @@ -100,7 +101,10 @@ public function update(Request $request, string $id) {
* Remove the specified resource from storage.
*/
public function destroy(string $id) {
// return $this->crowdSourcingProjectProblemManager->deleteProblem($id);
throw new \Exception('Not implemented yet');
return $this->crowdSourcingProjectProblemManager->deleteProblem($id);
}

public function getProblemStatusesForManagementPage(): JsonResponse {
return response()->json($this->crowdSourcingProjectProblemManager->getProblemStatusesForManagementPage());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ export default {
projects: [],
selectedProject: "",
problems: [],
problemStatuses: [],
errorMessage: "",
showUnpublishedProblemsOnly: false,
filteredProblems: [],
Expand All @@ -141,10 +142,11 @@ export default {
computed: {
...mapState(["loading", "modal"]),
},
mounted() {
async mounted() {
this.deleteModal = new Modal(document.getElementById("deleteModal"));
this.getCrowdSourcingProjectsForFiltering();
this.$nextTick(() => {
await this.getProblemStatusesForManagementPage();
await this.getCrowdSourcingProjectsForFiltering();
await this.$nextTick(() => {
this.dataTableInstance = $("#problemsTable").DataTable({
pageLength: 5,
data: [],
Expand Down Expand Up @@ -175,8 +177,22 @@ export default {
methods: {
...mapActions(["get", "post", "setLoading"]),
getCrowdSourcingProjectsForFiltering() {
this.get({
async getProblemStatusesForManagementPage() {
return this.get({
url: window.route("api.problems.statuses.management.get"),
data: {},
urlRelative: false,
})
.then((response) => {
this.problemStatuses = response.data;
})
.catch((error) => {
this.showErrorMessage(error);
});
},
async getCrowdSourcingProjectsForFiltering() {
return this.get({
url: window.route("api.crowd-sourcing-projects.for-problems.get"),
data: {},
urlRelative: false,
Expand Down Expand Up @@ -254,32 +270,13 @@ export default {
},
getBadgeClassForProblemStatus(problemStatus) {
switch (problemStatus.id) {
case 1:
return "badge-secondary";
case 2:
return "badge-success";
case 3:
return "badge-info";
case 4:
return "badge-danger";
default:
return "badge-dark";
}
// search by id in the problemStatuses array
const status = this.problemStatuses.find((status) => status.id === problemStatus.id);
return status ? status.badgeCSSClass : "badge-secondary";
},
getBadgeTitleForProblemStatus(problemStatus) {
switch (problemStatus.id) {
case 1:
return "The problem is pending";
case 2:
return "The problem is published";
case 3:
return "The problem is finalized";
case 4:
return "The problem is unpublished";
default:
return "Unknown status";
}
const status = this.problemStatuses.find((status) => status.id === problemStatus.id);
return status ? status.description : "Unknown status";
},
getProblemEditRoute(problem) {
return window.route("problems.edit", problem.id);
Expand Down
2 changes: 2 additions & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

use App\Http\Controllers\CrowdSourcingProject\CrowdSourcingProjectColorsController;
use App\Http\Controllers\CrowdSourcingProject\CrowdSourcingProjectController;
use App\Http\Controllers\CrowdSourcingProject\Problem\CrowdSourcingProjectProblemController;
use App\Http\Controllers\FileController;
use App\Http\Controllers\LanguageController;
use App\Http\Controllers\Questionnaire\QuestionnaireAnswerAnnotationController;
Expand Down Expand Up @@ -53,6 +54,7 @@
Route::post('/questionnaire/mark-translations', [QuestionnaireController::class, 'markQuestionnaireTranslations'])->name('api.questionnaire.translations.mark');
Route::get('/crowd-sourcing-projects/for-problems', [CrowdSourcingProjectController::class, 'getCrowdSourcingProjectsForProblems'])->name('api.crowd-sourcing-projects.for-problems.get');
Route::post('/crowd-sourcing-projects/get-problems-for-management', [CrowdSourcingProjectController::class, 'getProblemsForCrowdSourcingProjectForManagement'])->name('api.crowd-sourcing-projects.problems.get-management');
Route::get('/problems/statuses/management', [CrowdSourcingProjectProblemController::class, 'getProblemStatusesForManagementPage'])->name('api.problems.statuses.management.get');
});

Route::group(['middleware' => ['throttle:api-internal', 'auth', 'can:manage-users']], function () {
Expand Down

0 comments on commit 5dc97cf

Please sign in to comment.