Skip to content

Commit

Permalink
review frontend (#280)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio authored Sep 6, 2023
1 parent 396df3d commit 243f11b
Show file tree
Hide file tree
Showing 188 changed files with 9,620 additions and 9,281 deletions.
16 changes: 16 additions & 0 deletions app/Concerns/HasCounties.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace App\Concerns;

use App\Models\County;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

trait HasCounties
{
public function counties(): MorphToMany
{
return $this->morphToMany(County::class, 'model', 'model_has_counties', 'model_id');
}
}
39 changes: 39 additions & 0 deletions app/Concerns/HasUuid.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace App\Concerns;

use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Str;

trait HasUuid
{
public static function bootHasUuid()
{
// Generate UUID if none provided
static::creating(function (Model $model) {
if (! $model->uuid) {
$model->uuid = (string) Str::orderedUuid();
}
});

// Make sure UUIDs can't be changed
static::updating(function (Model $model) {
$originalUuid = $model->getOriginal('uuid');

if (
! \is_null($originalUuid) &&
$originalUuid !== $model->uuid
) {
$model->uuid = $originalUuid;
}
});
}

public function scopeWhereUuid(Builder $query, string $uuid): Builder
{
return $query->where('uuid', $uuid);
}
}
31 changes: 31 additions & 0 deletions app/Concerns/HasVolunteers.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

declare(strict_types=1);

namespace App\Concerns;

use App\Models\Volunteer;
use App\Models\VolunteerRequest;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Relations\MorphToMany;

trait HasVolunteers
{
public function volunteers(): MorphToMany
{
return $this->morphToMany(Volunteer::class, 'model', 'model_has_volunteers', 'model_id')
->using(VolunteerRequest::class)
->withTimestamps()
->withPivot('status');
}

public function scopeWhereHasVolunteers(Builder $query): Builder
{
return $query->whereHas('volunteers');
}

public function scopeWhereDoesntHaveVolunteers(Builder $query): Builder
{
return $query->whereDoesntHave('volunteers');
}
}
6 changes: 3 additions & 3 deletions app/Enums/VolunteerStatus.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

enum VolunteerStatus: string
{
case pending = 'pending';
case active = 'active';
case inactive = 'inactive';
case PENDING = 'pending';
case APPROVED = 'approved';
case REJECTED = 'rejected';
}
3 changes: 2 additions & 1 deletion app/Http/Controllers/Auth/PasswordController.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function storeInitialPassword(Request $request, User $user): RedirectResp
]);
$user->markPasswordAsSet();

return redirect()->route('login')->with('success_message', __('user.messages.set_initial_password_success'));
return redirect()->route('login')
->with('success', __('user.messages.set_initial_password_success'));
}
}
11 changes: 7 additions & 4 deletions app/Http/Controllers/Auth/RegisteredUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,11 +79,12 @@ public function store(RegistrationRequest $request): RedirectResponse
Auth::login($user);

return redirect()->route('register')
->with('success_message', ['message' => 'Contul a fost creat', 'usrid' => $user['id']]);
->with('success', ['message' => 'Contul a fost creat', 'usrid' => $user['id']]);
} catch(\Throwable $th) {
Log::log('error', $th->getMessage());

return redirect()->back()->with('error_message', __('auth.failed'));
return redirect()->back()
->with('error', __('auth.failed'));
}
}

Expand All @@ -94,9 +95,11 @@ public function update(Request $request, $userId): RedirectResponse
$user->source_of_information = $request->input('source_of_information');
$user->save();

return redirect()->back()->with('success_message', 'Multumim pentru feedback');
return redirect()->back()
->with('success', 'Multumim pentru feedback');
} catch(\Throwable $th) {
return redirect()->back()->with('error_message', 'Something went wrong');
return redirect()->back()
->with('error', 'Something went wrong');
}
}
}
30 changes: 25 additions & 5 deletions app/Http/Controllers/HomeController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,10 @@

namespace App\Http\Controllers;

use App\Http\Resources\BCRProjectCardsResource;
use App\Http\Resources\ProjectCardsResource;
use App\Models\Article;
use App\Models\Organization;
use App\Models\Project;
use Inertia\Inertia;

Expand All @@ -14,12 +17,29 @@ public function index()
{
$articles = Article::latest()->take(3)->get();

$bcr_projects = Project::publish()->paginate(9)->withQueryString();
$donate_projects = Project::publish()->paginate(9)->withQueryString();

return Inertia::render('Public/Home', [
'bcr_projects' => $bcr_projects,
'donate_projects' => $donate_projects,
'projects_count' => Project::query()
->publish()
->count(),

'organizations_count' => Organization::query()
->isApproved()
->count(),

'projects' => ProjectCardsResource::collection(
Project::publish()
->inRandomOrder()
->limit(12)
->get()
),

'bcr_projects' => BCRProjectCardsResource::collection(
Project::publish()
// TODO: ->whereOrganizationIsBCR()
->limit(12)
->get()
),

'articles' => $articles,
]);
}
Expand Down
32 changes: 24 additions & 8 deletions app/Http/Controllers/Ngo/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,33 @@

use App\Http\Controllers\Controller;
use App\Http\Requests\Project\StoreRequest;
use App\Http\Resources\ProjectCardsResource;
use App\Models\Activity;
use App\Models\County;
use App\Models\Project;
use App\Models\ProjectCategory;
use App\Services\ProjectService;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Http\Request;
use Inertia\Inertia;

class ProjectController extends Controller
{
public function index(Request $request)
{
// TODO: fix issue with approved projects not being displayed
$projectStatus = $request->get('project_status');
$projects = Project::query()->with('organization')->where('organization_id', auth()->user()->organization_id);
if ($projectStatus) {
$projects = $projects->where('status', $projectStatus);
}

return Inertia::render('AdminOng/Projects/Projects', [
'query' => $projects->paginate(16)->withQueryString(),
'query' => ProjectCardsResource::collection(
Project::query()
->where('organization_id', auth()->user()->organization_id)
->when($projectStatus, function (Builder $query, $projectStatus) {
return $query->status($projectStatus);
})
->paginate(16)
->withQueryString()
),
'projectStatus' => $projectStatus,
]);
}
Expand Down Expand Up @@ -66,6 +74,11 @@ public function edit(Project $project)
'project' => $project,
'counties' => $counties,
'projectCategories' => ProjectCategory::get(['name', 'id']),
'changes' => Activity::pendingChangesFor($project)
->get()
->flatMap(fn (Activity $activity) => $activity->properties->keys())
->unique()
->values(),
]);
}

Expand All @@ -81,17 +94,20 @@ public function update(Request $request, Project $project)
}
$project->update($request->all());

return redirect()->back()->with('success_message', 'Project updated.');
return redirect()->back()
->with('success', 'Project updated.');
}

public function changeStatus($id, Request $request)
{
try {
(new ProjectService(Project::class))->changeStatus($id, $request->get('status'));
} catch (\Exception $exception) {
return redirect()->back()->with('error_message', $exception->getMessage());
return redirect()->back()
->with('error', $exception->getMessage());
}

return redirect()->back()->with('success_message', 'Project status changed.');
return redirect()->back()
->with('success', 'Project status changed.');
}
}
10 changes: 6 additions & 4 deletions app/Http/Controllers/Ngo/RegionalProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ public function edit(RegionalProject $project)

return Inertia::render('AdminOng/Projects/EditRegionalProject', [
'project' => $project,
'counties' => County::get(['name', 'id']),
'counties' => County::get(['name', 'id']),
'projectCategories' => ProjectCategory::get(['name', 'id']),
]);
}
Expand All @@ -95,7 +95,8 @@ public function update(Request $request, RegionalProject $project)
}
$project->update($request->all());

return redirect()->back()->with('success_message', 'Project updated.');
return redirect()->back()
->with('success', 'Project updated.');
}

/**
Expand All @@ -111,9 +112,10 @@ public function changeStatus(Request $request, string $id)
try {
(new ProjectService(RegionalProject::class))->changeStatus($id, $request->get('status'));
} catch (\Exception $exception) {
return redirect()->back()->with('error_message', $exception->getMessage());
return redirect()->back()
->with('error', $exception->getMessage());
}

return redirect()->back()->with('success_message', 'Project status changed.');
return redirect()->back()->with('success', 'Project status changed.');
}
}
64 changes: 30 additions & 34 deletions app/Http/Controllers/Ngo/VolunteerController.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,62 +4,58 @@

namespace App\Http\Controllers\Ngo;

use App\Enums\VolunteerStatus;
use App\Http\Controllers\Controller;
use App\Models\Volunteer;
use App\Http\Resources\Volunteers\VolunteersTableResource;
use App\Models\VolunteerRequest;
use Illuminate\Http\Request;
use Inertia\Inertia;

class VolunteerController extends Controller
{
public function index(Request $request, $status = '')
public function index(Request $request, string $status = '')
{
if (auth()->user()->organization == null) {
return redirect()->route('admin.ong.edit');
}
if (auth()->user()->organization->projects->count() == 0) {
$volunteers = Volunteer::where('id', 0)->paginate(15)->withQueryString();
} else {
$volunteers = Volunteer::whereHas('projects', function ($query) {
$query->whereIn('projects.id', auth()->user()->organization->projects->pluck('id'));
})->with('projects');
if (\in_array($status, ['pending', 'approved', 'rejected'])) {
$volunteers->where('status', $status);
}
}

return Inertia::render(
'AdminOng/Volunteers/Volunteers',
[
'volunteers' => $volunteers,
'status' => $status,
]
);
$status = VolunteerStatus::tryFrom($status) ?? VolunteerStatus::APPROVED;

return Inertia::render('AdminOng/Volunteers/Volunteers', [
'volunteers' => VolunteersTableResource::collection(
VolunteerRequest::query()
->with('volunteer')
->whereBelongsToOrganization(auth()->user()->organization->id)
->where('status', $status)
->orderBy('created_at', 'desc')
->paginate(15)
->withQueryString()
),
'status' => $status,
]);
}

public function approve(Request $request, $id)
public function approve(Request $request, VolunteerRequest $volunteerRequest)
{
$volunteer = Volunteer::findOrFail($id);
$volunteer->status = 'approved';
$volunteer->save();
$volunteerRequest->markAsApproved();

return redirect()->back()->with('success', 'Voluntarul a fost aprobat cu succes');
return redirect()->back()
->with('success', 'Voluntarul a fost aprobat cu succes');
}

public function reject(Request $request, $id)
public function reject(Request $request, VolunteerRequest $volunteerRequest)
{
$volunteer = Volunteer::findOrFail($id);
$volunteer->status = 'rejected';
$volunteer->save();
$volunteerRequest->markAsRejected();

return redirect()->back()->with('success', 'Voluntarul a fost respins cu succes');
return redirect()->back()
->with('success', 'Voluntarul a fost respins cu succes');
}

public function delete(Request $request, $id)
public function delete(Request $request, VolunteerRequest $volunteerRequest)
{
$volunteer = Volunteer::findOrFail($id);
$projectsIds = $request->get('project_ids', []);
$volunteer->projects()->detach($projectsIds);
$volunteerRequest->delete();

return redirect()->back()->with('success', 'Voluntarul a fost sters cu succes');
return redirect()->back()
->with('success', 'Voluntarul a fost sters cu succes');
}
}
Loading

0 comments on commit 243f11b

Please sign in to comment.