Skip to content

Commit

Permalink
refactoring filament pages & update composer dependencies
Browse files Browse the repository at this point in the history
  • Loading branch information
fordiquez committed Nov 22, 2023
1 parent ac430c7 commit a711114
Show file tree
Hide file tree
Showing 71 changed files with 1,550 additions and 1,330 deletions.
32 changes: 18 additions & 14 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -101,67 +101,71 @@ STRIPE_WEBHOOK_SECRET=

### 4. Install all composer & npm dependencies

```
```bash
composer install
```

```
```bash
npm install
```

### 5. Docker settings

```
```bash
docker-compose up --build -d
```

```
```bash
docker exec -it brandford_app /bin/bash
```

### 6. Run artisan commands

```
```bash
php artisan key:generate
```

```
```bash
php artisan storage:link
```

```
```bash
php artisan migrate:fresh --seed
```

```
```bash
php artisan shield:install --fresh
```

```
```bash
php artisan optimize:clear
```

### 7. Run application server & SSR rendering

```
```bash
npm run dev
```

```
```bash
php artisan inertia:start-ssr
```

```bash
php artisan serve --host=brandford.local.io
```

### 8. Run stripe webhook

```
```bash
stripe login
```

```
```bash
stripe listen --forward-to brandford.local.io/stripe/webhook
```

```
```bash
stripe trigger payment_intent.succeeded
```

Expand Down
81 changes: 42 additions & 39 deletions app/Filament/Pages/Addresses.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,22 @@

use App\Models\Country;
use App\Models\State;
use App\Models\UserAddress;
use Filament\Actions\Action;
use Filament\Forms\Components\Grid;
use Filament\Forms\Components\Hidden;
use Filament\Forms\Components\Repeater;
use Filament\Forms\Components\Section;
use Filament\Forms\Components\Select;
use Filament\Forms\Components\TextInput;
use Filament\Forms\Components\Toggle;
use Filament\Forms\Concerns\InteractsWithForms;
use Filament\Forms\Contracts\HasForms;
use Filament\Forms\Form;
use Filament\Forms\Get;
use Filament\Forms\Set;
use Filament\Notifications\Notification;
use Filament\Pages\Page;
use Illuminate\Support\Arr;

class Addresses extends Page implements HasForms
{
Expand All @@ -29,7 +33,7 @@ class Addresses extends Page implements HasForms

protected static string $view = 'filament.pages.addresses';

public ?array $addresses = null;
public ?array $addresses = [];

public function getCancelButtonUrlProperty(): string
{
Expand All @@ -50,68 +54,67 @@ public function mount(): void
]);
}

public function submit()
public function save(): void
{
$this->form->getState();

$addresses = $this->form->getState()['addresses'];
$addressesKeys = Arr::pluck($addresses, 'id');
$deletedAddresses = auth()->user()->addresses()->whereNotIn('id', $addressesKeys)->get();
$deletedAddresses->each(fn ($item) => $item->delete());

if (count($addresses)) {
foreach ($addresses as $address) {
auth()->user()->addresses()->updateOrCreate([
'id' => $address['id'],
'user_id' => auth()->user()->id,
], [
'title' => $address['title'],
'is_main' => $address['is_main'],
'country_id' => $address['country_id'],
'state_id' => $address['state_id'],
'city_id' => $address['city_id'],
'street_id' => $address['street_id'],
'house' => $address['house'],
'flat' => $address['flat'],
'postal_code' => $address['postal_code'],
]);
}
}
$addresses = collect($this->form->getState()['addresses']);

UserAddress::whereNotIn('id', $addresses->pluck('id'))->each(fn (UserAddress $address) => $address->delete());

$addresses->filter(fn (array $address) => !empty($address['id']))->each(fn (array $address) => UserAddress::find($address['id'])->update($address));

$addresses->filter(fn (array $address) => empty($address['id']))->each(fn (array $address) => UserAddress::create($address));

Notification::make()->title('Your addresses has been updated.')->success()->send();
}

protected function getFormSchema(): array
protected function getFormActions(): array
{
return [
Action::make('save')
->label(__('filament-panels::resources/pages/edit-record.form.actions.save.label'))
->submit('save'),
];
}

public function form(Form $form): Form
{
return $form->schema([
Section::make()->schema([
Repeater::make('addresses')
->schema([
TextInput::make('title')->required()->maxLength(255),
Toggle::make('is_main')->inline()->default(false),
Hidden::make('id'),
Hidden::make('user_id')->default(auth()->id()),
Select::make('country_id')
->options(Country::all()->pluck('name', 'id')->toArray())
->required()
->reactive()
->searchable()
->afterStateUpdated(fn (callable $set) => $set('state_id', null)),
Select::make('state_id')
->options(fn (Get $get) => $get('country_id') ? Country::find($get('country_id'))->states->pluck('name', 'id') : [])
->options(fn (Get $get) => Country::find($get('country_id'))?->states->pluck('name', 'id'))
->required()
->reactive()
->disabled(fn (Get $get) => !$get('country_id'))
->searchable()
->hidden(fn (Get $get) => !$get('country_id'))
->afterStateUpdated(fn (Set $set) => $set('city_id', null)),
Select::make('city_id')
->options(fn (Get $get) => $get('state_id') ? State::find($get('state_id'))->cities->pluck('name', 'id') : [])
->options(fn (Get $get) => $get('state_id') ? State::find($get('state_id'))?->cities->pluck('name', 'id') : [])
->required()
->reactive()
->disabled(fn (Get $get) => !$get('state_id')),
TextInput::make('house')->required(),
TextInput::make('flat')->nullable(),
TextInput::make('postal_code')->numeric()->nullable(),
->searchable()
->hidden(fn (Get $get) => !$get('state_id')),
TextInput::make('street')->required()->maxLength(50),
Grid::make()->schema([
TextInput::make('house')->required()->maxLength(10),
TextInput::make('flat')->nullable()->maxLength(10),
TextInput::make('postal_code')->nullable()->numeric()->maxLength(10),
])->columns(3),
Toggle::make('is_main')->inline()->default(false),
])->columns()
->lazy()
->maxItems(10)
->itemLabel(fn (array $state): ?string => $state['title'] ?? null),
]),
];
])->statePath('addresses');
}
}
Loading

0 comments on commit a711114

Please sign in to comment.