From 7217d945a3dd61bf16d3782d5f11c786fa78b15d Mon Sep 17 00:00:00 2001 From: Valentin Lab Date: Sat, 4 Oct 2025 11:30:24 +0200 Subject: [PATCH] fix: make the address appear when added --- .../Controllers/Shop/CustomerController.php | 60 ++++++++++++ .../Customers/partials/address_item.blade.php | 23 +++++ .../Customers/partials/addresses.blade.php | 97 ++++++++++++++----- routes/Shop/Customers.php | 1 + 4 files changed, 155 insertions(+), 26 deletions(-) create mode 100644 resources/views/Shop/Customers/partials/address_item.blade.php diff --git a/app/Http/Controllers/Shop/CustomerController.php b/app/Http/Controllers/Shop/CustomerController.php index 35e1ab53..0f156486 100644 --- a/app/Http/Controllers/Shop/CustomerController.php +++ b/app/Http/Controllers/Shop/CustomerController.php @@ -5,6 +5,7 @@ namespace App\Http\Controllers\Shop; use App\Repositories\Shop\CustomerAddresses; use App\Repositories\Shop\Customers; use Illuminate\Http\Request; +use Illuminate\Support\Facades\Validator; class CustomerController extends Controller { @@ -56,6 +57,65 @@ class CustomerController extends Controller return redirect()->route('Shop.Customers.edit'); } + public function storeAddress(Request $request) + { + if (Customers::isNotConnected()) { + return response()->json(['message' => __('Authentification requise.')], 403); + } + + $prefix = $request->input('prefix'); + $types = ['deliveries' => 1, 'invoices' => 2]; + + if (! array_key_exists($prefix, $types)) { + return response()->json(['message' => __('Type d\'adresse inconnu.')], 422); + } + + $addressData = $request->input($prefix, []); + + $validator = Validator::make($addressData, [ + 'name' => ['nullable', 'string', 'max:150'], + 'address' => ['required', 'string', 'max:255'], + 'address2' => ['nullable', 'string', 'max:255'], + 'zipcode' => ['required', 'string', 'max:30'], + 'city' => ['required', 'string', 'max:255'], + ], [ + 'address.required' => __('Merci de renseigner l\'adresse.'), + 'zipcode.required' => __('Merci de renseigner le code postal.'), + 'city.required' => __('Merci de renseigner la ville.'), + ]); + + if ($validator->fails()) { + return response()->json([ + 'message' => __('Merci de vérifier les informations saisies.'), + 'errors' => $validator->errors(), + ], 422); + } + + $data = $validator->validated(); + $customerId = Customers::getId(); + $data['customer_id'] = $customerId; + $data['type'] = $types[$prefix]; + + if (empty($data['name'])) { + $data['name'] = Customers::getName($customerId); + } + + $address = CustomerAddresses::store($data); + + $html = view('Shop.Customers.partials.address_item', [ + 'address' => $address->toArray(), + 'prefix' => $prefix, + 'with_name' => true, + ])->render(); + + return response()->json([ + 'success' => true, + 'html' => $html, + 'message' => __('Adresse enregistrée.'), + 'id' => $address->id, + ]); + } + public function delete_address($id) { $address = CustomerAddresses::get($id); diff --git a/resources/views/Shop/Customers/partials/address_item.blade.php b/resources/views/Shop/Customers/partials/address_item.blade.php new file mode 100644 index 00000000..a8352fa1 --- /dev/null +++ b/resources/views/Shop/Customers/partials/address_item.blade.php @@ -0,0 +1,23 @@ +
+
+ @php + $inputName = isset($prefix) && $prefix ? $prefix.'[address_id]' : 'address_id'; + @endphp + +
+
+ @if ($with_name ?? false) + {{ $address['name'] ?? '' }}
+ @endif + {{ $address['address'] }}
+ @if (! empty($address['address2'])) + {{ $address['address2'] }}
+ @endif + {{ $address['zipcode'] }} {{ $address['city'] }} +
+
+ + + +
+
diff --git a/resources/views/Shop/Customers/partials/addresses.blade.php b/resources/views/Shop/Customers/partials/addresses.blade.php index 86a2c385..57d32f93 100644 --- a/resources/views/Shop/Customers/partials/addresses.blade.php +++ b/resources/views/Shop/Customers/partials/addresses.blade.php @@ -1,26 +1,12 @@ -@foreach ($addresses ?? [] as $address) -
-
- -
-
- @if ($with_name ?? false) - {{ $address['name'] }}
- @endif - {{ $address['address'] }}
- @if ($address['address2']) - {{ $address['address2'] }}
- @endif - {{ $address['zipcode'] }} {{ $address['city'] }} -
-
- - - -
-
-@endforeach +
+ @foreach ($addresses ?? [] as $address) + @include('Shop.Customers.partials.address_item', [ + 'address' => $address, + 'prefix' => $prefix ?? null, + 'with_name' => $with_name ?? false, + ]) + @endforeach +
@@ -31,6 +17,18 @@ 'label' => 'Adresse', 'customer' => [], ]) +
+
+ +
+
+ +
+
@@ -43,8 +41,55 @@ @push('js') @endpush diff --git a/routes/Shop/Customers.php b/routes/Shop/Customers.php index 1e1e5a99..54e59b92 100644 --- a/routes/Shop/Customers.php +++ b/routes/Shop/Customers.php @@ -7,5 +7,6 @@ Route::prefix('Clients')->name('Customers.')->group(function () { Route::get('edit', 'CustomerController@edit')->name('edit'); Route::post('storeProfileAjax', 'CustomerController@storeProfileAjax')->name('storeProfileAjax'); Route::post('store', 'CustomerController@store')->name('store'); + Route::post('address', 'CustomerController@storeAddress')->name('address.store'); Route::get('delete_address/{id}', 'CustomerController@delete_address')->name('delete_address'); });