From a078887a9f3281279fed1f9bca8d67e603230feb Mon Sep 17 00:00:00 2001 From: Josh Torres Date: Sat, 30 Sep 2023 13:44:26 -0700 Subject: [PATCH 1/3] - upgrade dispatchBrowserEvent to dispatch for v3 components --- src/Livewire/v3/WithCachedRows.php | 41 +++++++++++++ src/Livewire/v3/WithInlineInput.php | 36 +++++++++++ src/Livewire/v3/WithLayoutSwitcher.php | 35 +++++++++++ src/Livewire/v3/WithMap.php | 44 ++++++++++++++ src/Livewire/v3/WithModal.php | 16 +++++ src/Livewire/v3/WithNotification.php | 38 ++++++++++++ src/Livewire/v3/WithPlace.php | 56 ++++++++++++++++++ src/Livewire/v3/WithSorting.php | 31 ++++++++++ src/Livewire/v3/WithStepWizard.php | 79 +++++++++++++++++++++++++ src/Livewire/v3/WithStripe.php | 49 +++++++++++++++ src/Livewire/v3/WithValidationFails.php | 50 ++++++++++++++++ 11 files changed, 475 insertions(+) create mode 100644 src/Livewire/v3/WithCachedRows.php create mode 100644 src/Livewire/v3/WithInlineInput.php create mode 100644 src/Livewire/v3/WithLayoutSwitcher.php create mode 100644 src/Livewire/v3/WithMap.php create mode 100644 src/Livewire/v3/WithModal.php create mode 100644 src/Livewire/v3/WithNotification.php create mode 100644 src/Livewire/v3/WithPlace.php create mode 100644 src/Livewire/v3/WithSorting.php create mode 100644 src/Livewire/v3/WithStepWizard.php create mode 100644 src/Livewire/v3/WithStripe.php create mode 100644 src/Livewire/v3/WithValidationFails.php diff --git a/src/Livewire/v3/WithCachedRows.php b/src/Livewire/v3/WithCachedRows.php new file mode 100644 index 0000000..63e72e2 --- /dev/null +++ b/src/Livewire/v3/WithCachedRows.php @@ -0,0 +1,41 @@ +useCache = true; + } + + public function getCacheKey() + { + return $this->cacheKey ?? $this->id; + } + + public function getCacheTtl() + { + return $this->cacheTtl; + } + + public function cache($callback) + { + $cacheKey = $this->getCacheKey(); + + if ($this->useCache && cache()->has($cacheKey)) { + return cache()->get($cacheKey); + } + + $result = $callback(); + + cache()->put($cacheKey, $result, $this->getCacheTtl()); + + return $result; + } +} diff --git a/src/Livewire/v3/WithInlineInput.php b/src/Livewire/v3/WithInlineInput.php new file mode 100644 index 0000000..798250a --- /dev/null +++ b/src/Livewire/v3/WithInlineInput.php @@ -0,0 +1,36 @@ +editMode = true; + + $this->dispatch('edit-mode', + id: $this->model()->getKey(), + ); + } + + public function submitInlineData() + { + $this->saveInlineData(); + + $this->editMode = false; + } +} diff --git a/src/Livewire/v3/WithLayoutSwitcher.php b/src/Livewire/v3/WithLayoutSwitcher.php new file mode 100644 index 0000000..4ca517f --- /dev/null +++ b/src/Livewire/v3/WithLayoutSwitcher.php @@ -0,0 +1,35 @@ +layoutSwitcher = session()->get($this->layoutSwitcherSessionKey(), $this->layoutSwitcher); + } + + public function switchLayout(string $value) + { + session()->put($this->layoutSwitcherSessionKey(), $value); + + $this->layoutSwitcher = $value; + } + + public function isUsingGridLayout(): bool + { + return $this->layoutSwitcher === 'grid'; + } + + public function isUsingListLayout(): bool + { + return $this->layoutSwitcher === 'list'; + } + + protected function layoutSwitcherSessionKey(): string + { + return 'layout-switcher'; + } +} diff --git a/src/Livewire/v3/WithMap.php b/src/Livewire/v3/WithMap.php new file mode 100644 index 0000000..68036f4 --- /dev/null +++ b/src/Livewire/v3/WithMap.php @@ -0,0 +1,44 @@ +dispatch($mapId, + type:'loadPlaces', + places:$this->formatPlaces($places), + ); + } + + public function flyTo(string $mapId, string $lng, string $lat) + { + $this->dispatch($mapId, + type:'fly', + coordinate:[$lng, $lat], + ); + } + + public function formatPlaces(array $places): array + { + return collect($places)->map(function ($place) { + return [ + 'type' => 'Feature', + 'properties' => [ + 'id' => $place['id'], + 'description' => $place['description'] ?? $place['name'], + 'icon' => $place['icon'] ?? 'rocket-15', + ], + 'geometry' => [ + 'type' => 'Point', + 'coordinates' => [$place['lng'], $place['lat']], + ], + ]; + })->all(); + } + + public function showPlaceDetail($placeId) + { + } +} diff --git a/src/Livewire/v3/WithModal.php b/src/Livewire/v3/WithModal.php new file mode 100644 index 0000000..2f9a209 --- /dev/null +++ b/src/Livewire/v3/WithModal.php @@ -0,0 +1,16 @@ +dispatch($modalId, type: 'close'); + } + + public function openModal(string $modalId) + { + $this->dispatch($modalId, type: 'open'); + } +} diff --git a/src/Livewire/v3/WithNotification.php b/src/Livewire/v3/WithNotification.php new file mode 100644 index 0000000..08aecdd --- /dev/null +++ b/src/Livewire/v3/WithNotification.php @@ -0,0 +1,38 @@ +dispatch('notify', + message: $message, + type:'success', + action:$action, + ); + } + + public function error(string $message, ?array $action = null) + { + $this->dispatch('notify', + message: $message, + type:'error', + action:$action, + ); + } + + public function info(string $message, ?array $action = null) + { + $this->dispatch('notify', + message: $message, + type:'info', + action:$action, + ); + } + + public function alertInvalidInput(?string $message = null) + { + $this->error($message ?? 'Please make sure all fields are input correctly.'); + } +} diff --git a/src/Livewire/v3/WithPlace.php b/src/Livewire/v3/WithPlace.php new file mode 100644 index 0000000..95fab81 --- /dev/null +++ b/src/Livewire/v3/WithPlace.php @@ -0,0 +1,56 @@ + $input, + 'key' => $this->getPlaceApiKey(), + ]); + + $response = Http::get('https://maps.googleapis.com/maps/api/place/autocomplete/json', $params); + + return $response->throw()->json(); + } + + public function findPlace(array $options = []): Place + { + $params = array_merge($options, [ + 'place_id' => $this->placeId, + 'key' => $this->getPlaceApiKey(), + ]); + + $response = Http::get('https://maps.googleapis.com/maps/api/place/details/json', $params) + ->throw() + ->json(); + + if ($response['status'] !== 'OK') { + throw new \Exception($response['error_message'] ?? 'Cannot find place!'); + } + + return new Place($response['result']['address_components'], $response['result']['geometry']['location']); + } + + protected function getPlaceApiKey(): mixed + { + return $this->placeApiKey() ?? config('library.place.google.api_key'); + } +} diff --git a/src/Livewire/v3/WithSorting.php b/src/Livewire/v3/WithSorting.php new file mode 100644 index 0000000..7f53401 --- /dev/null +++ b/src/Livewire/v3/WithSorting.php @@ -0,0 +1,31 @@ +sortField = $sortField; + $this->sortDirection = $sortDirection; + } + + public function sortBy($field) + { + $this->sortDirection = $this->sortField === $field + ? $this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc' + : 'asc'; + + $this->sortField = $field; + } + + public function applySorting(Builder $query): Builder + { + return $query->orderBy($this->sortField, $this->sortDirection); + } +} diff --git a/src/Livewire/v3/WithStepWizard.php b/src/Livewire/v3/WithStepWizard.php new file mode 100644 index 0000000..3404cd4 --- /dev/null +++ b/src/Livewire/v3/WithStepWizard.php @@ -0,0 +1,79 @@ +enableStepSessionData() && $this->enableAutoFillSessionData()) { + $this->fillSessionData(); + } + } + + public function submit(): string + { + $stepSessionData = $this->handle() ?? []; + + // Save session data of the current step component. + if ($this->enableStepSessionData()) { + $allSessionData = session()->get($this->getSessionDataKey(), []); + + session()->put($this->getSessionDataKey(), array_merge($allSessionData, $stepSessionData)); + } + + return $this->id; + } + + public function next(): void + { + $livewireComponentId = $this->submit(); + + $this->dispatch('next-step-emitted', $livewireComponentId); + } + + public function back(): void + { + $this->dispatch('previous-step-emitted'); + } + + protected function fillSessionData() + { + foreach ($this->getStepSessionData(default: []) as $key => $value) { + if (property_exists($this, $key)) { + $this->{$key} = $value; + } + } + } + + protected function getStepSessionData(?string $key = null, mixed $default = null): mixed + { + if (is_null($key)) { + return session()->get($this->getSessionDataKey(), $default); + } + + return Arr::get(session()->get($this->getSessionDataKey()), $key, $default); + } + + public function resetStepSessionData(): void + { + session()->forget($this->getSessionDataKey()); + } + + protected function enableStepSessionData(): bool + { + return true; + } + + protected function enableAutoFillSessionData(): bool + { + return true; + } + + protected function getSessionDataKey(): string + { + return 'step-session-data'; + } +} diff --git a/src/Livewire/v3/WithStripe.php b/src/Livewire/v3/WithStripe.php new file mode 100644 index 0000000..b1d9d2d --- /dev/null +++ b/src/Livewire/v3/WithStripe.php @@ -0,0 +1,49 @@ +validate(['stripeToken' => 'required|string|regex:/^pm/']); + + $billable = $this->stripeBillable(); + + $billable->updateDefaultPaymentMethod($this->stripeToken); + + // Let Livewire knows Stripe payment method updated. + $this->emitSelf('stripePaymentMethodUpdated', $billable); + + // Let Alpinejs now Stripe payment method updated. + $this->dispatch('stripe-payment-method-updated'); + + // Notify payment method was updated. + $this->dispatch(empty($event) ? 'notify' : $event, + type: 'success', + message: empty($message) ? 'Your payment method was updated!' : $message, + ); + + return [ + 'card_brand' => $billable->card_brand, + 'card_last_four' => $billable->card_last_four, + ]; + } +} diff --git a/src/Livewire/v3/WithValidationFails.php b/src/Livewire/v3/WithValidationFails.php new file mode 100644 index 0000000..4055e6d --- /dev/null +++ b/src/Livewire/v3/WithValidationFails.php @@ -0,0 +1,50 @@ +whenFails = $closure; + + return $this; + } + + public function validate($rules = null, $messages = [], $attributes = []) + { + [$rules, $messages, $attributes] = $this->providedOrGlobalRulesMessagesAndAttributes($rules, $messages, $attributes); + + $data = $this->prepareForValidation( + $this->getDataForValidation($rules) + ); + + $ruleKeysToShorten = $this->getModelAttributeRuleKeysToShorten($data, $rules); + + $data = $this->unwrapDataForValidation($data); + + $validator = Validator::make($data, $rules, $messages, $attributes); + + $this->shortenModelAttributesInsideValidator($ruleKeysToShorten, $validator); + + $validator->fails() && $this->handleFails($validator); + + $validatedData = $validator->validate(); + + $this->resetErrorBag(); + + return $validatedData; + } + + private function handleFails($validator) + { + $this->whenFails && ($this->whenFails)($validator); + + throw new ValidationException($validator); + } +} From 1c3878c0372ba148029501a81efe861c1bcf8dd2 Mon Sep 17 00:00:00 2001 From: Josh Torres Date: Sat, 30 Sep 2023 13:45:27 -0700 Subject: [PATCH 2/3] - upgrade dispatchBrowserEvent to dispatch for v3 components --- src/Livewire/WithInlineInput.php | 6 +- src/Livewire/WithMap.php | 16 ++--- src/Livewire/WithModal.php | 4 +- src/Livewire/WithNotification.php | 30 +++++----- src/Livewire/WithStepWizard.php | 4 +- src/Livewire/WithStripe.php | 10 ++-- src/Livewire/v3/WithCachedRows.php | 41 ------------- src/Livewire/v3/WithInlineInput.php | 36 ----------- src/Livewire/v3/WithLayoutSwitcher.php | 35 ----------- src/Livewire/v3/WithMap.php | 44 -------------- src/Livewire/v3/WithModal.php | 16 ----- src/Livewire/v3/WithNotification.php | 38 ------------ src/Livewire/v3/WithPlace.php | 56 ------------------ src/Livewire/v3/WithSorting.php | 31 ---------- src/Livewire/v3/WithStepWizard.php | 79 ------------------------- src/Livewire/v3/WithStripe.php | 49 --------------- src/Livewire/v3/WithValidationFails.php | 50 ---------------- 17 files changed, 35 insertions(+), 510 deletions(-) delete mode 100644 src/Livewire/v3/WithCachedRows.php delete mode 100644 src/Livewire/v3/WithInlineInput.php delete mode 100644 src/Livewire/v3/WithLayoutSwitcher.php delete mode 100644 src/Livewire/v3/WithMap.php delete mode 100644 src/Livewire/v3/WithModal.php delete mode 100644 src/Livewire/v3/WithNotification.php delete mode 100644 src/Livewire/v3/WithPlace.php delete mode 100644 src/Livewire/v3/WithSorting.php delete mode 100644 src/Livewire/v3/WithStepWizard.php delete mode 100644 src/Livewire/v3/WithStripe.php delete mode 100644 src/Livewire/v3/WithValidationFails.php diff --git a/src/Livewire/WithInlineInput.php b/src/Livewire/WithInlineInput.php index 18ede33..6d9b681 100644 --- a/src/Livewire/WithInlineInput.php +++ b/src/Livewire/WithInlineInput.php @@ -22,9 +22,9 @@ public function setEditMode() { $this->editMode = true; - $this->dispatchBrowserEvent('edit-mode', [ - 'id' => $this->model()->getKey(), - ]); + $this->dispatch('edit-mode', + id: $this->model()->getKey(), + ); } public function submitInlineData() diff --git a/src/Livewire/WithMap.php b/src/Livewire/WithMap.php index de7566f..d760305 100644 --- a/src/Livewire/WithMap.php +++ b/src/Livewire/WithMap.php @@ -6,18 +6,18 @@ trait WithMap { public function addPlaces(string $mapId, array $places) { - $this->dispatchBrowserEvent($mapId, [ - 'type' => 'loadPlaces', - 'places' => $this->formatPlaces($places), - ]); + $this->dispatch($mapId, + type:'loadPlaces', + places:$this->formatPlaces($places), + ); } public function flyTo(string $mapId, string $lng, string $lat) { - $this->dispatchBrowserEvent($mapId, [ - 'type' => 'fly', - 'coordinate' => [$lng, $lat], - ]); + $this->dispatch($mapId, + type:'fly', + coordinate:[$lng, $lat], + ); } public function formatPlaces(array $places): array diff --git a/src/Livewire/WithModal.php b/src/Livewire/WithModal.php index 7e09cc7..68bf6db 100644 --- a/src/Livewire/WithModal.php +++ b/src/Livewire/WithModal.php @@ -6,11 +6,11 @@ trait WithModal { public function closeModal(string $modalId) { - $this->dispatchBrowserEvent($modalId, ['type' => 'close']); + $this->dispatch($modalId, type: 'close'); } public function openModal(string $modalId) { - $this->dispatchBrowserEvent($modalId, ['type' => 'open']); + $this->dispatch($modalId, type: 'open'); } } diff --git a/src/Livewire/WithNotification.php b/src/Livewire/WithNotification.php index ec1ee43..98e6430 100644 --- a/src/Livewire/WithNotification.php +++ b/src/Livewire/WithNotification.php @@ -6,29 +6,29 @@ trait WithNotification { public function success(string $message, ?array $action = null) { - $this->dispatchBrowserEvent('notify', [ - 'message' => $message, - 'type' => 'success', - 'action' => $action, - ]); + $this->dispatch('notify', + message: $message, + type:'success', + action:$action, + ); } public function error(string $message, ?array $action = null) { - $this->dispatchBrowserEvent('notify', [ - 'message' => $message, - 'type' => 'error', - 'action' => $action, - ]); + $this->dispatch('notify', + message: $message, + type:'error', + action:$action, + ); } public function info(string $message, ?array $action = null) { - $this->dispatchBrowserEvent('notify', [ - 'message' => $message, - 'type' => 'info', - 'action' => $action, - ]); + $this->dispatch('notify', + message: $message, + type:'info', + action:$action, + ); } public function alertInvalidInput(?string $message = null) diff --git a/src/Livewire/WithStepWizard.php b/src/Livewire/WithStepWizard.php index 1909f60..319f0c6 100644 --- a/src/Livewire/WithStepWizard.php +++ b/src/Livewire/WithStepWizard.php @@ -31,12 +31,12 @@ public function next(): void { $livewireComponentId = $this->submit(); - $this->dispatchBrowserEvent('next-step-emitted', $livewireComponentId); + $this->dispatch('next-step-emitted', $livewireComponentId); } public function back(): void { - $this->dispatchBrowserEvent('previous-step-emitted'); + $this->dispatch('previous-step-emitted'); } protected function fillSessionData() diff --git a/src/Livewire/WithStripe.php b/src/Livewire/WithStripe.php index 0d655e8..6a8bfda 100644 --- a/src/Livewire/WithStripe.php +++ b/src/Livewire/WithStripe.php @@ -33,13 +33,13 @@ public function updateStripePaymentMethod(string $event, string $message) $this->emitSelf('stripePaymentMethodUpdated', $billable); // Let Alpinejs now Stripe payment method updated. - $this->dispatchBrowserEvent('stripe-payment-method-updated'); + $this->dispatch('stripe-payment-method-updated'); // Notify payment method was updated. - $this->dispatchBrowserEvent(empty($event) ? 'notify' : $event, [ - 'type' => 'success', - 'message' => empty($message) ? 'Your payment method was updated!' : $message, - ]); + $this->dispatch(empty($event) ? 'notify' : $event, + type: 'success', + message: empty($message) ? 'Your payment method was updated!' : $message, + ); return [ 'card_brand' => $billable->card_brand, diff --git a/src/Livewire/v3/WithCachedRows.php b/src/Livewire/v3/WithCachedRows.php deleted file mode 100644 index 63e72e2..0000000 --- a/src/Livewire/v3/WithCachedRows.php +++ /dev/null @@ -1,41 +0,0 @@ -useCache = true; - } - - public function getCacheKey() - { - return $this->cacheKey ?? $this->id; - } - - public function getCacheTtl() - { - return $this->cacheTtl; - } - - public function cache($callback) - { - $cacheKey = $this->getCacheKey(); - - if ($this->useCache && cache()->has($cacheKey)) { - return cache()->get($cacheKey); - } - - $result = $callback(); - - cache()->put($cacheKey, $result, $this->getCacheTtl()); - - return $result; - } -} diff --git a/src/Livewire/v3/WithInlineInput.php b/src/Livewire/v3/WithInlineInput.php deleted file mode 100644 index 798250a..0000000 --- a/src/Livewire/v3/WithInlineInput.php +++ /dev/null @@ -1,36 +0,0 @@ -editMode = true; - - $this->dispatch('edit-mode', - id: $this->model()->getKey(), - ); - } - - public function submitInlineData() - { - $this->saveInlineData(); - - $this->editMode = false; - } -} diff --git a/src/Livewire/v3/WithLayoutSwitcher.php b/src/Livewire/v3/WithLayoutSwitcher.php deleted file mode 100644 index 4ca517f..0000000 --- a/src/Livewire/v3/WithLayoutSwitcher.php +++ /dev/null @@ -1,35 +0,0 @@ -layoutSwitcher = session()->get($this->layoutSwitcherSessionKey(), $this->layoutSwitcher); - } - - public function switchLayout(string $value) - { - session()->put($this->layoutSwitcherSessionKey(), $value); - - $this->layoutSwitcher = $value; - } - - public function isUsingGridLayout(): bool - { - return $this->layoutSwitcher === 'grid'; - } - - public function isUsingListLayout(): bool - { - return $this->layoutSwitcher === 'list'; - } - - protected function layoutSwitcherSessionKey(): string - { - return 'layout-switcher'; - } -} diff --git a/src/Livewire/v3/WithMap.php b/src/Livewire/v3/WithMap.php deleted file mode 100644 index 68036f4..0000000 --- a/src/Livewire/v3/WithMap.php +++ /dev/null @@ -1,44 +0,0 @@ -dispatch($mapId, - type:'loadPlaces', - places:$this->formatPlaces($places), - ); - } - - public function flyTo(string $mapId, string $lng, string $lat) - { - $this->dispatch($mapId, - type:'fly', - coordinate:[$lng, $lat], - ); - } - - public function formatPlaces(array $places): array - { - return collect($places)->map(function ($place) { - return [ - 'type' => 'Feature', - 'properties' => [ - 'id' => $place['id'], - 'description' => $place['description'] ?? $place['name'], - 'icon' => $place['icon'] ?? 'rocket-15', - ], - 'geometry' => [ - 'type' => 'Point', - 'coordinates' => [$place['lng'], $place['lat']], - ], - ]; - })->all(); - } - - public function showPlaceDetail($placeId) - { - } -} diff --git a/src/Livewire/v3/WithModal.php b/src/Livewire/v3/WithModal.php deleted file mode 100644 index 2f9a209..0000000 --- a/src/Livewire/v3/WithModal.php +++ /dev/null @@ -1,16 +0,0 @@ -dispatch($modalId, type: 'close'); - } - - public function openModal(string $modalId) - { - $this->dispatch($modalId, type: 'open'); - } -} diff --git a/src/Livewire/v3/WithNotification.php b/src/Livewire/v3/WithNotification.php deleted file mode 100644 index 08aecdd..0000000 --- a/src/Livewire/v3/WithNotification.php +++ /dev/null @@ -1,38 +0,0 @@ -dispatch('notify', - message: $message, - type:'success', - action:$action, - ); - } - - public function error(string $message, ?array $action = null) - { - $this->dispatch('notify', - message: $message, - type:'error', - action:$action, - ); - } - - public function info(string $message, ?array $action = null) - { - $this->dispatch('notify', - message: $message, - type:'info', - action:$action, - ); - } - - public function alertInvalidInput(?string $message = null) - { - $this->error($message ?? 'Please make sure all fields are input correctly.'); - } -} diff --git a/src/Livewire/v3/WithPlace.php b/src/Livewire/v3/WithPlace.php deleted file mode 100644 index 95fab81..0000000 --- a/src/Livewire/v3/WithPlace.php +++ /dev/null @@ -1,56 +0,0 @@ - $input, - 'key' => $this->getPlaceApiKey(), - ]); - - $response = Http::get('https://maps.googleapis.com/maps/api/place/autocomplete/json', $params); - - return $response->throw()->json(); - } - - public function findPlace(array $options = []): Place - { - $params = array_merge($options, [ - 'place_id' => $this->placeId, - 'key' => $this->getPlaceApiKey(), - ]); - - $response = Http::get('https://maps.googleapis.com/maps/api/place/details/json', $params) - ->throw() - ->json(); - - if ($response['status'] !== 'OK') { - throw new \Exception($response['error_message'] ?? 'Cannot find place!'); - } - - return new Place($response['result']['address_components'], $response['result']['geometry']['location']); - } - - protected function getPlaceApiKey(): mixed - { - return $this->placeApiKey() ?? config('library.place.google.api_key'); - } -} diff --git a/src/Livewire/v3/WithSorting.php b/src/Livewire/v3/WithSorting.php deleted file mode 100644 index 7f53401..0000000 --- a/src/Livewire/v3/WithSorting.php +++ /dev/null @@ -1,31 +0,0 @@ -sortField = $sortField; - $this->sortDirection = $sortDirection; - } - - public function sortBy($field) - { - $this->sortDirection = $this->sortField === $field - ? $this->sortDirection = $this->sortDirection === 'asc' ? 'desc' : 'asc' - : 'asc'; - - $this->sortField = $field; - } - - public function applySorting(Builder $query): Builder - { - return $query->orderBy($this->sortField, $this->sortDirection); - } -} diff --git a/src/Livewire/v3/WithStepWizard.php b/src/Livewire/v3/WithStepWizard.php deleted file mode 100644 index 3404cd4..0000000 --- a/src/Livewire/v3/WithStepWizard.php +++ /dev/null @@ -1,79 +0,0 @@ -enableStepSessionData() && $this->enableAutoFillSessionData()) { - $this->fillSessionData(); - } - } - - public function submit(): string - { - $stepSessionData = $this->handle() ?? []; - - // Save session data of the current step component. - if ($this->enableStepSessionData()) { - $allSessionData = session()->get($this->getSessionDataKey(), []); - - session()->put($this->getSessionDataKey(), array_merge($allSessionData, $stepSessionData)); - } - - return $this->id; - } - - public function next(): void - { - $livewireComponentId = $this->submit(); - - $this->dispatch('next-step-emitted', $livewireComponentId); - } - - public function back(): void - { - $this->dispatch('previous-step-emitted'); - } - - protected function fillSessionData() - { - foreach ($this->getStepSessionData(default: []) as $key => $value) { - if (property_exists($this, $key)) { - $this->{$key} = $value; - } - } - } - - protected function getStepSessionData(?string $key = null, mixed $default = null): mixed - { - if (is_null($key)) { - return session()->get($this->getSessionDataKey(), $default); - } - - return Arr::get(session()->get($this->getSessionDataKey()), $key, $default); - } - - public function resetStepSessionData(): void - { - session()->forget($this->getSessionDataKey()); - } - - protected function enableStepSessionData(): bool - { - return true; - } - - protected function enableAutoFillSessionData(): bool - { - return true; - } - - protected function getSessionDataKey(): string - { - return 'step-session-data'; - } -} diff --git a/src/Livewire/v3/WithStripe.php b/src/Livewire/v3/WithStripe.php deleted file mode 100644 index b1d9d2d..0000000 --- a/src/Livewire/v3/WithStripe.php +++ /dev/null @@ -1,49 +0,0 @@ -validate(['stripeToken' => 'required|string|regex:/^pm/']); - - $billable = $this->stripeBillable(); - - $billable->updateDefaultPaymentMethod($this->stripeToken); - - // Let Livewire knows Stripe payment method updated. - $this->emitSelf('stripePaymentMethodUpdated', $billable); - - // Let Alpinejs now Stripe payment method updated. - $this->dispatch('stripe-payment-method-updated'); - - // Notify payment method was updated. - $this->dispatch(empty($event) ? 'notify' : $event, - type: 'success', - message: empty($message) ? 'Your payment method was updated!' : $message, - ); - - return [ - 'card_brand' => $billable->card_brand, - 'card_last_four' => $billable->card_last_four, - ]; - } -} diff --git a/src/Livewire/v3/WithValidationFails.php b/src/Livewire/v3/WithValidationFails.php deleted file mode 100644 index 4055e6d..0000000 --- a/src/Livewire/v3/WithValidationFails.php +++ /dev/null @@ -1,50 +0,0 @@ -whenFails = $closure; - - return $this; - } - - public function validate($rules = null, $messages = [], $attributes = []) - { - [$rules, $messages, $attributes] = $this->providedOrGlobalRulesMessagesAndAttributes($rules, $messages, $attributes); - - $data = $this->prepareForValidation( - $this->getDataForValidation($rules) - ); - - $ruleKeysToShorten = $this->getModelAttributeRuleKeysToShorten($data, $rules); - - $data = $this->unwrapDataForValidation($data); - - $validator = Validator::make($data, $rules, $messages, $attributes); - - $this->shortenModelAttributesInsideValidator($ruleKeysToShorten, $validator); - - $validator->fails() && $this->handleFails($validator); - - $validatedData = $validator->validate(); - - $this->resetErrorBag(); - - return $validatedData; - } - - private function handleFails($validator) - { - $this->whenFails && ($this->whenFails)($validator); - - throw new ValidationException($validator); - } -} From cd61f5f1d0c96c638724211de155344d5525176c Mon Sep 17 00:00:00 2001 From: joshtorres Date: Sat, 30 Sep 2023 20:45:53 +0000 Subject: [PATCH 3/3] Fix styling --- src/Livewire/WithInlineInput.php | 3 ++- src/Livewire/WithMap.php | 6 ++++-- src/Livewire/WithNotification.php | 9 ++++++--- src/Livewire/WithStripe.php | 3 ++- 4 files changed, 14 insertions(+), 7 deletions(-) diff --git a/src/Livewire/WithInlineInput.php b/src/Livewire/WithInlineInput.php index 6d9b681..060df4d 100644 --- a/src/Livewire/WithInlineInput.php +++ b/src/Livewire/WithInlineInput.php @@ -22,7 +22,8 @@ public function setEditMode() { $this->editMode = true; - $this->dispatch('edit-mode', + $this->dispatch( + 'edit-mode', id: $this->model()->getKey(), ); } diff --git a/src/Livewire/WithMap.php b/src/Livewire/WithMap.php index d760305..ae2f26c 100644 --- a/src/Livewire/WithMap.php +++ b/src/Livewire/WithMap.php @@ -6,7 +6,8 @@ trait WithMap { public function addPlaces(string $mapId, array $places) { - $this->dispatch($mapId, + $this->dispatch( + $mapId, type:'loadPlaces', places:$this->formatPlaces($places), ); @@ -14,7 +15,8 @@ public function addPlaces(string $mapId, array $places) public function flyTo(string $mapId, string $lng, string $lat) { - $this->dispatch($mapId, + $this->dispatch( + $mapId, type:'fly', coordinate:[$lng, $lat], ); diff --git a/src/Livewire/WithNotification.php b/src/Livewire/WithNotification.php index 98e6430..1758591 100644 --- a/src/Livewire/WithNotification.php +++ b/src/Livewire/WithNotification.php @@ -6,7 +6,8 @@ trait WithNotification { public function success(string $message, ?array $action = null) { - $this->dispatch('notify', + $this->dispatch( + 'notify', message: $message, type:'success', action:$action, @@ -15,7 +16,8 @@ public function success(string $message, ?array $action = null) public function error(string $message, ?array $action = null) { - $this->dispatch('notify', + $this->dispatch( + 'notify', message: $message, type:'error', action:$action, @@ -24,7 +26,8 @@ public function error(string $message, ?array $action = null) public function info(string $message, ?array $action = null) { - $this->dispatch('notify', + $this->dispatch( + 'notify', message: $message, type:'info', action:$action, diff --git a/src/Livewire/WithStripe.php b/src/Livewire/WithStripe.php index 6a8bfda..db22aa1 100644 --- a/src/Livewire/WithStripe.php +++ b/src/Livewire/WithStripe.php @@ -36,7 +36,8 @@ public function updateStripePaymentMethod(string $event, string $message) $this->dispatch('stripe-payment-method-updated'); // Notify payment method was updated. - $this->dispatch(empty($event) ? 'notify' : $event, + $this->dispatch( + empty($event) ? 'notify' : $event, type: 'success', message: empty($message) ? 'Your payment method was updated!' : $message, );