Files
investbrain/resources/views/livewire/share-portfolio-form.blade.php
T

255 lines
7.9 KiB
PHP
Raw Normal View History

2024-10-21 22:23:20 -05:00
<?php
use App\Models\Portfolio;
use App\Models\User;
use Livewire\Attributes\Rule;
use Livewire\Volt\Component;
use Illuminate\Support\Collection;
use Mary\Traits\Toast;
2024-10-22 20:29:54 -05:00
use App\Notifications\InvitedOnboardingNotification;
2024-10-21 22:23:20 -05:00
new class extends Component {
use Toast;
// props
public ?Portfolio $portfolio = null;
#[Rule('required|string|email')]
public string $emailAddress;
#[Rule('sometimes|boolean')]
2024-10-21 22:33:02 -05:00
public int $fullAccess = 0;
2024-10-21 22:23:20 -05:00
public array $permissions;
2024-10-22 16:48:53 -05:00
public bool $confirmingAccessDeletion = false;
public ?string $deletingAccessFor = null;
2024-10-21 22:23:20 -05:00
// methods
public function mount()
{
if (!$this->portfolio) {
$this->permissions = [
auth()->user()->id => [
'owner' => true,
'full_access' => false
]
];
} else {
$this->permissions = collect($this->portfolio->users)->mapWithKeys(function ($user) {
return [
$user->id => [
'owner' => $user->pivot->owner ?? 0,
'full_access' => $user->pivot->full_access ?? 0
]
];
})->toArray();
}
}
public function updatedPermissions()
{
$this->authorize('fullAccess', $this->portfolio);
$this->portfolio->users()->sync($this->permissions);
$this->portfolio->refresh();
$this->success(__('Updated user\'s access permission to portfolio'));
}
2024-10-22 16:48:53 -05:00
public function deleteUser(string $userId, bool $confirmed = false)
2024-10-21 22:23:20 -05:00
{
$this->authorize('fullAccess', $this->portfolio);
2024-10-22 16:48:53 -05:00
if (!$confirmed) {
$this->deletingAccessFor = $userId;
$this->confirmingAccessDeletion = true;
return;
}
2024-10-21 22:23:20 -05:00
unset($this->permissions[$userId]);
$this->portfolio->users()->sync($this->permissions);
$this->portfolio->refresh();
$this->success(__('Removed user\'s access to portfolio'));
2024-10-22 16:48:53 -05:00
// reset
$this->confirmingAccessDeletion = false;
$this->deletingAccessFor = null;
2024-10-21 22:23:20 -05:00
}
public function addUser()
{
$this->authorize('fullAccess', $this->portfolio);
$this->validate();
$user = User::firstOrCreate([
'email' => $this->emailAddress
], [
'name' => Str::title(Str::before($this->emailAddress, '@'))
]);
$this->permissions[$user->id] = [
'full_access' => $this->fullAccess
];
2024-10-22 17:39:42 -05:00
$sync = $this->portfolio->users()->sync($this->permissions);
if (!empty($sync['attached'])) {
foreach($sync['attached'] as $newUserId) {
2024-10-22 20:29:54 -05:00
User::find($newUserId)->notify(new InvitedOnboardingNotification($this->portfolio, auth()->user()));
2024-10-22 17:39:42 -05:00
};
}
2024-10-21 22:23:20 -05:00
$this->success(__('Shared portfolio with user'));
$this->portfolio->refresh();
$this->dispatch('toggle-add-user-modal');
$this->emailAddress = '';
$this->fullAccess = false;
}
}; ?>
<div class="">
<label class="pt-0 label label-text font-semibold">
<span>{{ __('People with access') }}</span>
</label>
<div class="border-primary border rounded-sm px-2 py-5 mb-2">
@if ($portfolio->owner)
2024-10-21 22:23:20 -05:00
<x-list-item
2024-10-22 16:48:53 -05:00
:item="$portfolio->owner"
2024-10-21 22:23:20 -05:00
avatar="profile_photo_url"
no-separator
no-hover
class="!-my-2 rounded"
>
<x-slot:value>
2024-10-22 16:48:53 -05:00
{{ $portfolio->owner->name }}
2024-10-21 22:23:20 -05:00
2024-10-22 16:48:53 -05:00
@if (auth()->user()->id == $portfolio->owner->id)
2024-10-21 22:23:20 -05:00
({{ __('you') }})
@endif
</x-slot:value>
<x-slot:sub-value>
{{ __('Owner') }}
</x-slot:sub-value>
</x-list-item>
@endif
2024-10-21 22:23:20 -05:00
@foreach (collect($this->portfolio?->users)->where('pivot.owner', '!=', 1) as $user)
<x-list-item
:item="$user"
avatar="profile_photo_url"
no-separator
class="!-my-2 rounded"
x-data="{ loading: false, timeout: null }"
>
2024-10-22 20:29:54 -05:00
<x-slot:value>
{{ $user->name }}
@if (auth()->user()->id == $user->id)
({{ __('you') }})
@endif
</x-slot:value>
2024-10-21 22:23:20 -05:00
<x-slot:sub-value>
{{ $user->email }}
2024-10-22 20:29:54 -05:00
2024-10-21 22:23:20 -05:00
</x-slot:sub-value>
<x-slot:actions>
2024-10-22 20:29:54 -05:00
@if (auth()->user()->id != $user->id)
2024-10-21 22:23:20 -05:00
<x-select
class="select select-ghost border-none focus:outline-none focus:ring-0"
:options="[['id' => 0, 'name' => __('Read only')], ['id' => 1, 'name' => __('Full access')]]"
wire:model.live.number="permissions.{{ $user->id }}.full_access"
/>
2024-10-22 20:29:54 -05:00
2024-10-21 22:23:20 -05:00
<x-button
class="btn-sm btn-ghost btn-circle"
wire:click="deleteUser('{{ $user->id }}')"
2024-10-22 20:29:54 -05:00
spinner="deleteUser('{{ $user->id }}')"
2024-10-21 22:23:20 -05:00
>
<x-icon name="o-x-mark" class="w-4" />
2024-10-22 20:29:54 -05:00
</x-button>
2024-10-22 16:48:53 -05:00
@endif
2024-10-21 22:23:20 -05:00
</x-slot:actions>
</x-list-item>
@endforeach
2024-10-22 16:48:53 -05:00
<x-confirmation-modal wire:model.live="confirmingAccessDeletion">
<x-slot:title>
{{ __('Remove Access') }}
</x-slot:title>
<x-slot name="content">
{{ __('By removing this person\'s access, they will no longer be able to view this portfolio. They will lose access immediately.') }}
</x-slot>
<x-slot name="footer">
<x-button class="btn-outline" wire:click="$toggle('confirmingAccessDeletion')" wire:loading.attr="disabled">
{{ __('Cancel') }}
</x-secondary-button>
<x-button class="ms-3 btn-error text-white" wire:click="deleteUser('{{ $this->deletingAccessFor }}', true)" spinner="deleteUser" wire:loading.attr="disabled">
{{ __('Remove Access') }}
</x-button>
</x-slot>
</x-confirmation-modal>
<x-ib-alpine-modal
2024-10-21 22:23:20 -05:00
key="add-user-modal"
2024-10-22 16:48:53 -05:00
title="{{ __('Share Portfolio') }}"
2024-10-21 22:23:20 -05:00
>
<div class="" x-data="{ }">
<x-ib-form wire:submit="addUser" class="">
<x-input
label="Email"
icon="o-envelope"
placeholder="{{ __('Type an email address to share portfolio') }}"
wire:model="emailAddress"
2024-10-22 16:48:53 -05:00
required
2024-10-21 22:23:20 -05:00
/>
<x-toggle
class="mt-2"
label="{{ __('Grant full access') }}"
wire:model="fullAccess"
hint="{{ __('Allow this user to manage portfolio details and create or update transactions') }}"
right
/>
<x-slot:actions>
<x-button
label="{{ __('Share') }}"
2024-10-22 16:48:53 -05:00
title="{{ __('Share Portfolio') }}"
2024-10-21 22:23:20 -05:00
type="submit"
icon="o-paper-airplane"
class="btn-primary"
spinner="addUser"
/>
</x-slot:actions>
</x-ib-form>
</div>
2024-10-22 16:48:53 -05:00
</x-ib-alpine-modal>
2024-10-21 22:23:20 -05:00
<x-button class="btn-sm block mt-4" @click="$dispatch('toggle-add-user-modal')">
2024-10-22 16:48:53 -05:00
{{ __('Add People') }}
2024-10-21 22:23:20 -05:00
</x-button>
</div>
</div>