Skip to content

Commit

Permalink
Enhancements:
Browse files Browse the repository at this point in the history
- in edit product, show price notification fields only when url is available with message.

Took 26 minutes
  • Loading branch information
Cybrarist committed Jan 29, 2025
1 parent 75c2a74 commit b094349
Show file tree
Hide file tree
Showing 3 changed files with 166 additions and 151 deletions.
32 changes: 21 additions & 11 deletions app/Filament/Resources/ProductResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,27 +66,37 @@ public static function form(Form $form): Form
->afterStateUpdated(function ($state) {
if ($state) {
$url = new URLHelper($state);

if ($url->store) {
StoreHelper::is_unique($url);
}
}
}),


Select::make('status')
->options(StatusEnum::class)
->default(StatusEnum::Published)
->preload()
->native(false),

TextInput::make('notify_price')
->nullable()
->numeric(),
Forms\Components\Fieldset::make()
->label('These field is for new store addition')
->columnSpan(1)
->hidden(fn ($operation, $get) => $operation == "edit" && ! $get('url'))
->schema([
TextInput::make('notify_price')
->hint('')
->nullable()
->numeric(),

TextInput::make('notify_percentage')
->nullable()
->hintIcon("heroicon-o-information-circle", "Get notified when price drops below specified percentage")
->suffix('%')
->numeric(),
]),

TextInput::make('notify_percentage')
->nullable()
->hintIcon("heroicon-o-information-circle", "Get notified when price drops below specified percentage")
->suffix('%')
->numeric(),

Select::make('categories')
->relationship('categories', 'name')
Expand Down Expand Up @@ -348,10 +358,10 @@ public static function table(Table $table): Table

Filter::make('lowest_within')
->form([
TextInput::make('lowest_within_x')
->label('Price is lowest in X Days'),
TextInput::make('lowest_within_x')
->label('Price is lowest in X Days'),

])
])
->query(function (Builder $query, $data) {

if (! $data["lowest_within_x"]) {
Expand Down
58 changes: 25 additions & 33 deletions app/Filament/Resources/ProductResource/Pages/EditProduct.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,67 +10,60 @@
use App\Models\ProductStore;
use App\Models\Store;
use Filament\Actions;
use Filament\Notifications\Notification;
use Filament\Resources\Pages\EditRecord;
use Illuminate\Support\Arr;
use Livewire\Attributes\On;

class EditProduct extends EditRecord
{

protected static string $resource = ProductResource::class;

protected static ?string $title="Edit Product";
protected static ?string $title = "Edit Product";

protected function getHeaderActions(): array
{
return [
return [
Actions\DeleteAction::make(),

Actions\Action::make('Fetch')
->color('primary')
->action(fn()=> StoreHelper::fetch_product($this->record))
->after(fn ($livewire) => $livewire->dispatch('refresh'))
->action(fn () => StoreHelper::fetch_product($this->record))
->after(fn ($livewire) => $livewire->dispatch('refresh')),
];
}

protected function mutateFormDataBeforeSave(array $data) : array{

$extra_data=[
'notify_price'=>$data['notify_price'] ?? null,
'notify_percentage'=>$data['notify_percentage'] ?? null,
protected function mutateFormDataBeforeSave(array $data): array
{
$extra_data = [
'notify_price' => $data['notify_price'] ?? null,
'notify_percentage' => $data['notify_percentage'] ?? null,
];

if ($data['url']) {
$new_url = new URLHelper($data['url']);

if ($data['url']){
$new_url=new URLHelper($data['url']);
$extra_data['key'] = $new_url->product_unique_key;

// if product across multi region store has the same id, then user can just add the domain
// and the code will add the other store.

$extra_data['key']=$new_url->product_unique_key;

//if product across multi region store has the same id, then user can just add the domain
// and the code will add the other store.


if (!$new_url->product_unique_key){
$get_same_store_product= ProductStore::where("product_id", $this->record->id)
->whereIn("store_id" , Store::where('domain' , 'like' , $new_url->top_host.'%')->pluck("id")->toArray())
if (! $new_url->product_unique_key) {
$get_same_store_product = ProductStore::where("product_id", $this->record->id)
->whereIn("store_id", Store::where('domain', 'like', $new_url->top_host.'%')->pluck("id")->toArray())
->first();


$extra_data['key']=$get_same_store_product->key;
}
elseif( !StoreHelper::is_unique($new_url))
$extra_data['key'] = $get_same_store_product->key;
} elseif (! StoreHelper::is_unique($new_url)) {
$this->halt();
}

StoreTemplate::insert_other_store(domain: $new_url->domain , product_id: $this->record->id, extra_data: $extra_data);

StoreTemplate::insert_other_store(domain: $new_url->domain, product_id: $this->record->id, extra_data: $extra_data);

$this->dispatch('refresh_products_relation');
}



return Arr::except($data , ["url"]);
return Arr::except($data, ["url"]);
}

protected function getFooterWidgets(): array
Expand All @@ -80,12 +73,11 @@ protected function getFooterWidgets(): array
];
}


#[On('refresh')]
public function refresh_the_form(): void {
public function refresh_the_form(): void
{
$this->refreshFormData(
$this->record->getFillable()
);
}

}
Loading

0 comments on commit b094349

Please sign in to comment.