Skip to content

Commit

Permalink
✨ Annotation related changes
Browse files Browse the repository at this point in the history
  • Loading branch information
afariasfermin committed Nov 16, 2022
1 parent b3beacd commit 91a1dd4
Show file tree
Hide file tree
Showing 2 changed files with 217 additions and 14 deletions.
196 changes: 184 additions & 12 deletions app/Http/Livewire/Annotations.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,43 @@

namespace App\Http\Livewire;

use App\Models\Annotation;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Support\Facades\Auth;
use Illuminate\View\View;
use Livewire\Component;
use Illuminate\Support\Arr;
use App\Models\{Annotation, User};
use Illuminate\Support\Facades\Auth;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Collection;

class Annotations extends Component
{
/**
* Undocumented variable
*
* @var boolean
*/
public bool $currentTeamIsEmpty = false;

/**
* Annotation form.
*
* @var boolean
*/
public bool $displayAnnotationCreationForm = false;

/**
* User's accounts suggestions.
*
* @var boolean
*/
public bool $displaySuggestions = false;

/**
* Assignees list.
*
* @var boolean
*/
public bool $displayAssignees = false;

/**
* The given model
*
Expand All @@ -25,6 +53,27 @@ class Annotations extends Component
*/
public Collection $annotations;

/**
* Suggestion list.
*
* @var Collection
*/
public Collection $suggestions;

/**
* Assignees.
*
* @var \Illuminate\Support\Collection
*/
public \Illuminate\Support\Collection $assignees;

/**
* Search criteria.
*
* @var string
*/
public string $query = '';

/**
* The component's state.
*
Expand All @@ -33,7 +82,17 @@ class Annotations extends Component
public array $state = [
'user_id' => '',
'content' => '',
'custom_properties' => null,
'custom_properties' => [
'title' => '',
'assignees' => [],
'status' => '',
'priority' => '',
'commissioning_at' => null,
'feature' => [
'values' => null,
'geometry' => null,
],
],
];

/**
Expand All @@ -43,7 +102,20 @@ class Annotations extends Component
*/
protected array $rules = [
'state.content' => 'required|string',
'state.custom_properties' => 'nullable|string',
'state.custom_properties.title' => 'required|string',
'state.custom_properties.assignees' => 'required|array',
'state.custom_properties.status' => 'required|string',
'state.custom_properties.priority' => 'required|string',
'state.custom_properties.commissioning_at' => 'required|date',
];

/**
* Event Listeners.
*
* @var array
*/
protected $listeners = [
'featureAtPixel' => 'setFeatureAtPixel',
];

/**
Expand All @@ -54,20 +126,120 @@ class Annotations extends Component
public function mount(): void
{
$this->state['user_id'] = Auth::id();

// Get all of the team's users, excluding the owner...
$this->suggestions = Auth::user()->currentTeam->users;

$this->currentTeamIsEmpty = $this->suggestions->isEmpty();

$this->assignees = collect([]);
}

/**
* Feature at pixel.
*
* @param array $payload - feature json object.
*
* @return void
*/
public function setFeatureAtPixel(array $payload = []): void
{
if (Arr::exists($payload, 'values_')) {
$this->state['custom_properties']['feature']['values'] = Arr::except($payload['values_'], ['geometry']);
}

if (Arr::has($payload, 'values_.geometry')) {
$this->state['custom_properties']['feature']['geometry'] = Arr::get($payload, 'values_.geometry');
}
}

/**
* Set default state.
*
* @return void
*/
public function resetState(): void
{
$this->state = [
'user_id' => Auth::id(),
'content' => '',
'custom_properties' => [
'title' => '',
'assignees' => [],
'status' => '',
'priority' => '',
'commissioning_at' => null,
'feature' => [
'values' => null,
'geometry' => null,
],
],
];

$this->suggestions = Auth::user()->currentTeam->users;

$this->assignees = collect([]);
}

public function create(): Annotation
/**
* Add assigned to list.
*
* @param User $user
* @return void
*/
public function toggleAssigned(User $user): void
{
$this->assignees->push($user);
$this->displayAssignees = true;

// Update suggestions list state
$this->suggestions = $this->suggestions
->except($this->assignees->pluck('id')->toArray());
}

/**
* Remove assigned from list.
*
* @param User $user
* @return void
*/
public function removeAssigned(User $user): void
{
$this->assignees = $this->assignees->filter(
fn($item) => $item['id'] !== $user->id
);

$this->suggestions->push($user);

if ($this->assignees->count() === 0) {
$this->displayAssignees = false;
}
}

/**
* Render the component.
* Store a newly created resource in storage.
*
* @return View
* @return Annotation
*/
public function render(): View
public function store(): Annotation
{
return view('livewire.annotations');
$this->state['custom_properties']['assignees'] = $this->assignees->toArray();

$this->resetErrorBag();

$this->validate();

$annotation = $this->model->annotation()->create($this->state);

$this->resetState();

$this->displayAnnotationCreationForm = false;

$this->dispatchBrowserEvent('alert', [
'type' => 'success',
'message' => __('Successfully saved annotation.')
]);

return $annotation;
}
}
35 changes: 33 additions & 2 deletions app/Models/Annotation.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,16 @@

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use App\Models\User;
use App\Traits\CustomProperties;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\Relations\MorphTo;
use Illuminate\Database\Eloquent\Relations\BelongsTo;
use Illuminate\Database\Eloquent\Factories\HasFactory;

class Annotation extends Model
{
use HasFactory;
use HasFactory, CustomProperties;

/**
* The table associated with the model.
Expand All @@ -28,6 +31,24 @@ class Annotation extends Model
'custom_properties',
];

/**
* The attributes that should be cast to native types.
*
* @var array
*/
protected $casts = [
'custom_properties' => 'array',
];

/**
* The attributes that should be mutated to dates.
*
* @var array
*/
protected $dates = [
'custom_properties->commissioning_at',
];

/**
* Annotations related models.
*
Expand All @@ -37,4 +58,14 @@ public function annotable(): MorphTo
{
return $this->morphTo();
}

/**
* User related mode.
*
* @return BelongsTo
*/
public function user(): BelongsTo
{
return $this->belongsTo(User::class, 'user_id');
}
}

0 comments on commit 91a1dd4

Please sign in to comment.