Skip to content

Commit

Permalink
wip on public projects
Browse files Browse the repository at this point in the history
  • Loading branch information
gheorghelupu17 committed Oct 16, 2023
1 parent 3868797 commit e7fad7c
Show file tree
Hide file tree
Showing 11 changed files with 186 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class ProjectsRelationManager extends RelationManager

protected static ?string $recordTitleAttribute = 'name';


public static function getTitle(): string
{
return __('project.label.plural');
Expand All @@ -28,6 +29,7 @@ public static function form(Form $form): Form
Forms\Components\TextInput::make('name')
->required()
->maxLength(255),

]);
}

Expand All @@ -41,10 +43,9 @@ public static function table(Table $table): Table
//
])
->headerActions([
Tables\Actions\CreateAction::make(),
])
->actions([
Tables\Actions\EditAction::make(),
Tables\Actions\EditAction::make()->openUrlInNewTab(),
Tables\Actions\DeleteAction::make(),
])
->bulkActions([
Expand Down
43 changes: 31 additions & 12 deletions app/Http/Controllers/Dashboard/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,22 +6,25 @@

use App\Http\Controllers\Controller;
use App\Http\Requests\Project\StoreRequest;
use App\Http\Resources\Project\EditProjectResource;
use App\Http\Resources\Project\ShowProjectResource;
use App\Http\Resources\ProjectCardResource;
use App\Http\Resources\ProjectResource;
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 Illuminate\Support\Facades\Validator;
use Inertia\Inertia;

class ProjectController extends Controller
{
public function index(Request $request)
{
$projectStatus = $request->get('project_status');

return Inertia::render('AdminOng/Projects/Projects', [
'query' => ProjectCardResource::collection(
Project::query()
Expand All @@ -48,12 +51,10 @@ public function store(StoreRequest $request)
{
$data = $request->validated();
$project = (new ProjectService(Project::class))->create($data);
$project->addAllMediaFromRequest()->each(function ($fileAdder,$index) {
if ($index == 0)
{
$project->addAllMediaFromRequest()->each(function ($fileAdder, $index) {
if ($index == 0) {
$fileAdder->toMediaCollection('preview');
}else{

} else {
$fileAdder->toMediaCollection('gallery');
}
});
Expand All @@ -65,12 +66,11 @@ public function edit(Project $project)
{
$this->authorize('view', $project);
$project->load('media');
$counties = County::get(['name', 'id']);

return Inertia::render('AdminOng/Projects/EditProject', [
'project' => new ProjectResource($project),
'counties' => $counties,
'projectCategories' => ProjectCategory::get(['name', 'id']),
'project' => new EditProjectResource($project),
'counties' => $this->getCounties(),
'projectCategories' => $this->getProjectCategories(),
'changes' => Activity::pendingChangesFor($project)
->get()
->flatMap(fn (Activity $activity) => $activity->properties->keys())
Expand All @@ -83,11 +83,12 @@ public function update(Request $request, Project $project)
{
$this->authorize('editAsNgo', $project);


if ($request->has('counties')) {
$project->counties()->sync(collect($request->get('counties'))->pluck('id'));
$project->counties()->sync(collect($request->get('counties')));
}
if ($request->has('categories')) {
$project->categories()->sync(collect($request->get('categories'))->pluck('id'));
$project->categories()->sync(collect($request->get('categories')));
}
$project->update($request->all());

Expand All @@ -97,6 +98,24 @@ public function update(Request $request, Project $project)

public function changeStatus($id, Request $request)
{
$project = Project::findOrFail($id);
$projectArray = $project->toArray();
$project['preview'] = $project->getFirstMediaUrl('preview') ?? null;

Validator::make($projectArray, [
'name' => ['required', 'max:255'],
'start' => ['required', 'date', 'after_or_equal:today'],
'end' => ['required', 'date', 'after:start'],
'target_amount' => ['required', 'numeric', 'min:1'],
'categories' => ['required', 'array', 'min:1'],
'counties' => ['required_if:is_national,0', 'array', 'min:1'],
'description' => ['required', 'min:100', 'max:1000'],
'scope' => ['required', 'min:100', 'max:1000'],
'beneficiaries' => ['required', 'min:100', 'max:1000'],
'reason_to_donate' => ['required', 'min:100', 'max:1000'],
'preview' => ['required'],
])->validate();

try {
(new ProjectService(Project::class))->changeStatus($id, $request->get('status'));
} catch (\Exception $exception) {
Expand Down
55 changes: 55 additions & 0 deletions app/Http/Resources/Project/EditProjectResource.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace App\Http\Resources\Project;

use App\Http\Resources\Resource;
use Illuminate\Http\Request;

class EditProjectResource extends Resource
{
public static $wrap = null;

public function toArray(Request $request): array
{
return [
'id' => $this->id,
// 'type' => $this->type,
'name' => $this->name,
'slug' => $this->slug,

'image' => $this->getFirstMediaUrl('preview'),
'target_budget' => $this->target_budget,
'gallery' => $this->getMedia('gallery')->map(function ($media) {
return [
'id' => $media->id,
'url' => $media->getFullUrl(),
];
})->toArray(),
'organization' => [
'name' => $this->organization->name,
'id' => $this->organization->id,
],
'is_national' => \boolval($this->is_national),
'beneficiaries' => $this->beneficiaries,
'start' => $this->start?->format('Y-m-d'),
'end' => $this->end?->format('Y-m-d'),
'description' => $this->description,
'scope' => $this->scope,
'reason_to_donate' => $this->reason_to_donate,
'accepting_volunteers' => \boolval($this->accepting_volunteers),
'accepting_comments' => \boolval($this->accepting_comments),
'videos' => '',
'external_links' => $this->external_links,
'counties' => $this->counties->pluck('id')->toArray(),
'counties_names' => $this->counties->pluck('name')->join(', '),
'categories' => $this->categories->pluck('id')->toArray(),
'categories_names' => $this->categories->pluck('name')->join(', '),
'championship' => [
'troffees_count' => 2,
'score' => 100,
],
];
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,17 @@

declare(strict_types=1);

namespace App\Http\Resources;
namespace App\Http\Resources\Project;

use App\Http\Resources\Resource;
use Illuminate\Http\Request;

class ProjectResource extends Resource
class ShowProjectResource extends Resource
{
public static $wrap = null;

public function toArray(Request $request): array
{
// dd($this->counties);

return [
'id' => $this->id,
// 'type' => $this->type,
Expand All @@ -34,8 +33,8 @@ public function toArray(Request $request): array
],
'is_national' => \boolval($this->is_national),
'beneficiaries' => $this->beneficiaries,
'start' => $this->start,
'end' => $this->end,
'start' => $this->start?->format('Y-m-d'),
'end' => $this->end?->format('Y-m-d'),
'description' => $this->description,
'scope' => $this->scope,
'reason_to_donate' => $this->reason_to_donate,
Expand Down
5 changes: 5 additions & 0 deletions app/Models/Volunteer.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,9 @@ public function scopeWhereBelongsToOrganization(Builder $query, int $organizatio
fn ($query) => $query->where('organization_id', $organizationId)
);
}

public function getFullNameAttribute(): string
{
return $this->first_name . ' ' . $this->last_name;
}
}
16 changes: 2 additions & 14 deletions app/Services/ProjectService.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@
use App\Models\User;
use App\Notifications\Admin\ProjectCreated as ProjectCreatedAdmin;
use App\Notifications\Ngo\ProjectCreated;
use Carbon\CarbonImmutable;
use Illuminate\Support\Facades\Notification;
use Illuminate\Support\Facades\Validator;

class ProjectService
{
Expand Down Expand Up @@ -77,21 +79,7 @@ private function createPendingProject(array $data): Project|RegionalProject
*/
public function changeStatus($id, string $status): void
{
$this->project = $this->project::findOrFail($id);

if ($this->project->status === ProjectStatus::draft && $status === ProjectStatus::pending->value) {
$fields = $this->project->toArray();
$requiredFields = $this->project->getRequiredFieldsForApproval();
$missingFields = [];
foreach ($fields as $key => $value) {
if (\in_array($key, $requiredFields) && empty($value)) {
$missingFields[] = $key;
}
}
if (! empty($missingFields)) {
throw new ('Project is missing required fields for approval, please fill in all required fields . Please fill: ' . implode(', ', $missingFields));
}
}
$this->project->status = ProjectStatus::pending->value;
$this->project->status_updated_at = now();
$this->project->save();
Expand Down
2 changes: 2 additions & 0 deletions lang/ro.json
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,8 @@
"projects_map": "Hartă proiecte",
"status": "Status",
"period": "Perioada donații",
"period_start_donation": "Data început perioada de donații",
"period_end_donation": "Data sfârșit perioada de donații",
"add_project_title": "Adaugă proiect",
"project_name_label": "Denumire proiect",
"amount_target_label": "Suma target (RON)",
Expand Down
7 changes: 5 additions & 2 deletions lang/ro/project.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,12 +72,15 @@
'visible_status' => 'Status in platforma public',

],
'visible_status'=>[
'visible_status' => [
'published' => 'Publicat',
'archived' => 'Arhivat',
'open' => 'Deschis',
'close' => 'Închis',
'starting_soon' => 'Începe în curând',
]
],
'errors' => [
'start_date_in_past' => 'Data de începere nu poate fi în trecut',
],

];
16 changes: 16 additions & 0 deletions resources/js/Components/Field.vue
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,23 @@
<slot name="value" />

<p v-show="hasPendingChanges" class="mt-2 text-sm text-red-600" v-text="$t('field_has_pending_changes')" />
<p v-show="hasErrors" class="mt-2 text-sm text-red-600" v-text="errors" />
</dd>

<div class="flex md:justify-end">
<slot name="action" />
</div>

</div>
</template>

<script setup>
import {computed, onMounted} from "vue";
onMounted(() => {
console.log('mounted')
console.log(props.errors)
});
const props = defineProps({
label: String,
hasPendingChanges: {
Expand All @@ -34,5 +42,13 @@
type: Boolean,
default: false,
},
errors:{
type: String,
default: null,
}
});
const hasErrors = computed(() => {
return props.errors !== null;
});
</script>
21 changes: 0 additions & 21 deletions resources/js/Components/cards/ProjectCard.vue
Original file line number Diff line number Diff line change
Expand Up @@ -121,15 +121,6 @@
@click="changeProjectStatus(project.id, 'archive', project.type)"
:label="$t('can.be.archived')"
/>

<SecondaryButton

v-if="'admin' == cardType && !project.is_active && !project.is_pending && !project.can_be_archived"
class="w-full mt-4 py-2.5 text-primary-500 ring-1 ring-inset ring-primary-500 hover:bg-primary-400"
@click="changeProjectStatus(project.id, 'pending', project.type)"
:label="$t('publish')"
/>

<!-- Donate modal -->
<DonateModal
v-if="'client' == cardType && 0 < project_end_date"
Expand Down Expand Up @@ -195,16 +186,4 @@
return daysDiff;
});
const changeProjectStatus = (id, status, type) => {
let form = useForm({
status: status,
id: id,
});
console.log(form);
let tmpRoute =
type === 'regional' ? route('dashboard.projects.regional.status', id) : route('dashboard.projects.status', id);
if (confirm('Are you sure you want to change the status of this project?')) {
form.post(tmpRoute);
}
};
</script>
Loading

0 comments on commit e7fad7c

Please sign in to comment.