Skip to content

Commit

Permalink
review frontend (#287)
Browse files Browse the repository at this point in the history
  • Loading branch information
andreiio authored Sep 12, 2023
1 parent 2eb91d1 commit a480ac8
Show file tree
Hide file tree
Showing 111 changed files with 2,291 additions and 1,959 deletions.
24 changes: 24 additions & 0 deletions app/Exceptions/Handler.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace App\Exceptions;

use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
use Inertia\Inertia;
use Throwable;

class Handler extends ExceptionHandler
Expand All @@ -29,4 +30,27 @@ public function register(): void
//
});
}

public function render($request, Throwable $e)
{
$response = parent::render($request, $e);

if (! app()->isLocal() && \in_array($response->status(), [401, 403, 404, 429, 500, 503])) {
return Inertia::render('Error', [
'status' => $response->status(),
'title' => __('error.' . $response->status() . '.title'),
'message' => __('error.' . $response->status() . '.message'),
])
->toResponse($request)
->setStatusCode($response->status());
}

if ($response->status() === 419) {
return back()->with([
'message' => __('error.419.message'),
]);
}

return $response;
}
}
72 changes: 25 additions & 47 deletions app/Http/Controllers/Auth/RegisteredUserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,15 @@
namespace App\Http\Controllers\Auth;

use App\Enums\OrganizationStatus;
use App\Enums\UserRole;
use App\Http\Controllers\Controller;
use App\Http\Requests\RegistrationRequest;
use App\Models\ActivityDomain;
use App\Models\Organization;
use App\Models\User;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\RedirectResponse;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Log;
use Inertia\Inertia;
use Inertia\Response;

Expand All @@ -26,20 +24,10 @@ class RegisteredUserController extends Controller
*/
public function create(): Response
{
$activityDomains = cache()->remember('activityDomains', 60 * 60 * 24, function () {
return ActivityDomain::get(['name', 'id']);
});
$counties = cache()->remember('counties', 60 * 60 * 24, function () {
return \App\Models\County::get(['name', 'id']);
});

return Inertia::render(
'Auth/Register',
[
'activity_domains' => $activityDomains,
'counties' => $counties,
]
);
return Inertia::render('Auth/Register', [
'counties' => $this->getCounties(),
'domains' => $this->getActivityDomains(),
]);
}

/**
Expand All @@ -49,43 +37,33 @@ public function create(): Response
*/
public function store(RegistrationRequest $request): RedirectResponse
{
try {
$data = $request->validated();
$user = $data['user'];
$attributes = $request->validated();

$user = User::create([
'name' => $user['name'],
'email' => $user['email'],
'password' => Hash::make($user['password']),
'role' => $data['type'],
]);
$user = User::create([
'name' => $attributes['user']['name'],
'email' => $attributes['user']['email'],
'password' => Hash::make($attributes['user']['password']),
'role' => UserRole::tryFrom($attributes['type']),
]);

event(new Registered($user));
event(new Registered($user));

if ($data['type'] == 'ngo-admin') {
$ong = $data['ong'];
$ong['status'] = OrganizationStatus::draft;
$organization = Organization::create($ong);
$organization->addMediaFromRequest('ong.logo')->toMediaCollection('logo');
if ($request->hasFile('ong.statute')) {
$organization->addMediaFromRequest('ong.statute')->toMediaCollection('statute');
}
$organization->activityDomains()->attach($ong['activity_domains_ids']);
$organization->counties()->attach($ong['counties_ids']);
$user->organization_id = $organization->id;
$user->save();
}
if ($user->isNgoAdmin()) {
$attributes['ngo']['status'] = OrganizationStatus::draft;

Auth::login($user);
$organization = $user->organization()->create($attributes['ngo']);

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

return redirect()->back()
->with('error', __('auth.failed'));
$organization->activityDomains()->attach($attributes['ngo']['domains']);
$organization->counties()->attach($attributes['ngo']['counties']);
}

Auth::login($user);

return redirect()->route('register')
->with('success', ['message' => 'Contul a fost creat', 'usrid' => $user['id']]);
}

public function update(Request $request, $userId): RedirectResponse
Expand Down
46 changes: 46 additions & 0 deletions app/Http/Controllers/Controller.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,58 @@

namespace App\Http\Controllers;

use App\Models\ActivityDomain;
use App\Models\County;
use App\Models\ProjectCategory;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Support\Facades\Cache;

class Controller extends BaseController
{
use AuthorizesRequests;
use ValidatesRequests;

public function getActivityDomains(): array
{
return Cache::remember('activity-domains', static::optionsCacheTTL(), function () {
return ActivityDomain::query()
->addSelect('id as value')
->addSelect('name as label')
->get()
->toArray();
});
}

public function getCounties(): array
{
return Cache::remember('counties', static::optionsCacheTTL(), function () {
return County::query()
->addSelect('id as value')
->addSelect('name as label')
->get()
->toArray();
});
}

public function getProjectCategories(): array
{
return Cache::remember('project-categories', static::optionsCacheTTL(), function () {
return ProjectCategory::query()
->addSelect('id as value')
->addSelect('name as label')
->get()
->toArray();
});
}

private static function optionsCacheTTL(): int
{
if (app()->isLocal()) {
return MINUTE_IN_SECONDS;
}

return HOUR_IN_MINUTES;
}
}
32 changes: 32 additions & 0 deletions app/Http/Controllers/Dashboard/DonationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Resources\Collections\DonationCollection;
use App\Models\Donation;
use Illuminate\Http\Request;
use Inertia\Inertia;
use Inertia\Response;
use Spatie\QueryBuilder\QueryBuilder;

class DonationController extends Controller
{
public function index(Request $request): Response
{
return Inertia::render('AdminOng/Donations/Index', [
'filter' => $request->query('filter'),
'collection' => new DonationCollection(
QueryBuilder::for(Donation::class)
->where('organization_id', auth()->user()->organization_id)
->with('project:id,name,organization_id')
->allowedSorts('created_at', 'amount')
->defaultSorts('-created_at')
->paginate()
->withQueryString()
),
]);
}
}
44 changes: 44 additions & 0 deletions app/Http/Controllers/Dashboard/OrganizationController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php

declare(strict_types=1);

namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Requests\Organization\UpdateOrganizationRequest;
use App\Http\Resources\Organizations\EditOrganizationResource;
use App\Models\Activity;
use App\Services\OrganizationService;
use Inertia\Inertia;
use Inertia\Response;

class OrganizationController extends Controller
{
public function edit(): Response
{
$organization = auth()->user()->organization;

return Inertia::render('AdminOng/Organizations/Edit', [
'organization' => new EditOrganizationResource($organization),
'activity_domains' => $this->getActivityDomains(),
'counties' => $this->getCounties(),
'changes' => Activity::pendingChangesFor($organization)
->get()
->flatMap(fn (Activity $activity) => $activity->properties->keys())
->unique()
->values(),
]);
}

public function update(UpdateOrganizationRequest $request)
{
$organization = auth()->user()->organization;

$this->authorize('update', $organization);

OrganizationService::update($organization, $request->validated());

return redirect()->route('dashboard.organization.edit')
->with('success', __('organization.messages.update_success'));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Http\Controllers\Ngo;
namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Requests\Project\StoreRequest;
Expand Down Expand Up @@ -61,7 +61,7 @@ public function store(StoreRequest $request)
$fileAdder->toMediaCollection('project_files');
});

return redirect()->route('admin.ong.project.edit', $project->id)->with('success', 'Project created.');
return redirect()->route('dashboard.projects.edit', $project->id)->with('success', 'Project created.');
}

public function edit(Project $project)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

declare(strict_types=1);

namespace App\Http\Controllers\Ngo;
namespace App\Http\Controllers\Dashboard;

use App\Http\Controllers\Controller;
use App\Http\Requests\RegionalProject\StoreRequest;
Expand Down Expand Up @@ -58,7 +58,7 @@ public function store(StoreRequest $request)
$fileAdder->toMediaCollection('regionalProjectFiles');
});

return redirect()->route('admin.ong.project.edit', $project->id)->with('success', 'Project created.');
return redirect()->route('dashboard.projects.edit', $project->id)->with('success', 'Project created.');
}

/**
Expand Down
Loading

0 comments on commit a480ac8

Please sign in to comment.