From 49ef22ddc5f8420add2a91eb65af64f8e2d22df1 Mon Sep 17 00:00:00 2001 From: soufiane Date: Tue, 28 Mar 2023 15:35:14 +0200 Subject: [PATCH] =?utf8?q?wip=20#5788=20@7:30=20valeur=20par=20defaut=20ad?= =?utf8?q?resse=20si=20champ=20vide,=20fix=20disparition=20form=20mdp,=20f?= =?utf8?q?ix=20erreurs=20form=20sporadique,=20ajout=20champ=20company=20da?= =?utf8?q?ns=20les=20adresses,=20inversement=20nom/prenom,=20ajout=20#=20d?= =?utf8?q?ans=20l'url=20pour=20conserver=20l'=C3=A9tat=20dans=20page=20mon?= =?utf8?q?=20compte,=20suppression=20de=20addresse=20dans=20le=20sous=20me?= =?utf8?q?nu=20header?= MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit --- app/Http/Controllers/AjaxController.php | 37 ++++++++-- app/SubForms/Address.php | 6 ++ resources/js/app.js | 71 +++++++++++-------- .../views/components/address-form.blade.php | 22 +++--- resources/views/pages/my_account.blade.php | 20 +++--- resources/views/pages/sign_in.blade.php | 14 ++-- .../views/partials/nav-account.blade.php | 4 +- 7 files changed, 113 insertions(+), 61 deletions(-) diff --git a/app/Http/Controllers/AjaxController.php b/app/Http/Controllers/AjaxController.php index bb226e7..f8507b5 100644 --- a/app/Http/Controllers/AjaxController.php +++ b/app/Http/Controllers/AjaxController.php @@ -13,6 +13,7 @@ use Carbon\Carbon; use Cubist\Backpack\app\Http\Controllers\CubistFrontController; use Cubist\Backpack\app\Magic\PageData; use Cubist\Backpack\app\Magic\Search; +//use Cubist\Util\ObjectUtil; use Illuminate\Validation\ValidationException; use Illuminate\Http\Request; use Illuminate\Support\Facades\Mail; @@ -362,13 +363,35 @@ class AjaxController extends CubistFrontController ]; $data = $this->validation_form($request, $validation); - $data['status'] = 0; - $data['password'] = Hash::make($data['password']); if (!strstr($data['vat'], $data['siren']) || substr($data['vat'], '-9') !== $data['siren']) { throw ValidationException::withMessages(['siren' => __('Le numero de siren est incorrect')]); } + /** + * + * The following block of code format address data with good format of database [{'...','...'}] + * to make it same in the database, readable and editable later in front + * + */ + $address = '['; + $counter = 0; + foreach ($data['address'] as $key => $value) { + $counter++; + $address .= json_encode($data['address'][$key]); + if($counter !== sizeof($data['address'])){ + $address .= ','; + } + } + $address .= ']'; + /** + * End of block + */ + + $data['address'] = $address; + $data['status'] = 0; + $data['password'] = Hash::make($data['password']); + $client = new Client($data); $client->save(); @@ -484,21 +507,25 @@ class AjaxController extends CubistFrontController $address = Auth::guard('web-clients')->user()->address; $addressToArray = json_decode($address); + unset($addressToArray[$index]); $addressToString = json_encode($addressToArray); + $email = Auth::guard('web-clients')->user()->email; $client = Client::where('email', $email)->update(['address' => $addressToString]); + + return $addressToString; } public function getuser() { $user = Auth::guard('web-clients')->user(); if($user) { - $addressToArray = json_decode($user->address); + $addressToArray = json_decode($user->address, true); $checkBillingAddress = array_filter($addressToArray, function ($n) { - return $n->billing_address; + return $n['billing_address']; }); $checkDeliveryAddress = array_filter($addressToArray, function ($n) { - return $n->delivery_address; + return $n['delivery_address']; }); $data = [ diff --git a/app/SubForms/Address.php b/app/SubForms/Address.php index 24c6d1b..21f70f2 100644 --- a/app/SubForms/Address.php +++ b/app/SubForms/Address.php @@ -15,6 +15,12 @@ class Address extends SubForm { 'type' => 'Text' ]); + $this->addField([ + 'name' => 'company', + 'label' => "Société", + 'type' => 'Text' + ]); + $this->addField([ 'name' => 'address', 'label' => "Adresse", diff --git a/resources/js/app.js b/resources/js/app.js index 597d816..54d806a 100644 --- a/resources/js/app.js +++ b/resources/js/app.js @@ -4,7 +4,7 @@ * building robust, powerful web applications using Vue and Laravel. */ -import {forEach} from "../../public/vendor/adminlte/bower_components/mocha/mocha"; +import {forEach, indexOf} from "../../public/vendor/adminlte/bower_components/mocha/mocha"; require('./bootstrap'); require('./menu'); @@ -104,6 +104,12 @@ const app = new Vue({ }); }, + watch: { + errorsForm: function(newValue, old) { + this.errorsForm = newValue ??= old + } + }, + computed: { cartItemCount() { // Todo: See if this should count just number of items or make a sum of all quantities? What is more useful? The sum of quantities can be found using map/reduce functions but this needs to be adapted for the object structure using Object.keys as the source. @@ -160,14 +166,18 @@ const app = new Vue({ * The following functions are used for login,register and account page * */ - resetUrlAccount() { - // Remove hash to url if exist to prevent to stay in same tab + lockTab(tab) { + // Add hash to url to stay in same tab when page is reloading // It used in account page when we navigate between different tab - let hash = window.location.hash, - fullUrl = window.location.origin+window.location.pathname + let fullUrl = window.location.origin+window.location.pathname + history.pushState("", "", fullUrl+'#'+tab); + }, + activeTab(tab) { + this.tab = tab + this.lockTab(tab) - if(hash) - history.pushState("", "", fullUrl); + this.errorsForm = {} + this.removeErrorsForm() }, toggleType(el) { this.type[el] = this.type[el] === "password" ? "text" : "password" @@ -182,14 +192,13 @@ const app = new Vue({ errorInput[i].classList.remove('error'); } }, - errorHandling(data, root){ + errorHandling(data, root, form){ if (data.response) { - let errors = data.response.data.errors + let errors = { 'errors': data.response.data.errors, 'id': form } root.errorsForm = errors - root.removeErrorsForm() - for (let k in errors) { + for (let k in errors['errors']) { if(k.indexOf('.')){ let keys = k.split('.') for(let i = 0; i < keys.length; i++){ @@ -201,6 +210,10 @@ const app = new Vue({ } } let el = document.querySelector('[name*="' + k + '"]') + + if(form) + el = document.querySelector(form+' [name*="' + k + '"]') + el.classList.add('error') } } @@ -274,22 +287,25 @@ const app = new Vue({ } ) }, - activeTab(tab) { - this.tab = tab - this.errorsForm = {} - this.removeErrorsForm() - this.resetUrlAccount() - }, getUser() { let root = this axios.post('/ajax/getuser') .then(function (response) { - // root.user = response.data['user'] root.addresses = JSON.parse(root.user.address) - root.default_billing_address = 'billing'+response.data['address_billing'] - root.default_delivery_address = 'delivery'+response.data['address_delivery'] + + // Assign default values to each address, if the field is empty + // It useful during the first connexion to his account + for(let i = 0; i < root.addresses.length; i++) { + root.addresses[i]['name'] = root.addresses[i]['name'] ?? 'Adresse '+root.addresses[i]['city'] + root.addresses[i]['company'] = root.addresses[i]['company'] ?? root.user['company'] + root.addresses[i]['firstname'] = root.addresses[i]['firstname'] ?? root.user['firstname'] + root.addresses[i]['lastname'] = root.addresses[i]['lastname'] ?? root.user['lastname'] + } + + root.default_billing_address = 'billing' + response.data['address_billing'] + root.default_delivery_address = 'delivery' + response.data['address_delivery'] }) .catch(function (error) { } @@ -303,7 +319,7 @@ const app = new Vue({ this.addresses.push(newAddress) - // Disabled the click to add address at most once + // Disabled the click, to add address, at most once let target = event.target target.setAttribute('disabled', 'disabled') @@ -330,7 +346,6 @@ const app = new Vue({ axios.post('/ajax/update', data) .then(function (response) { - // root.removeErrorsForm() root.validateForm = true root.form[id] = response.data @@ -343,6 +358,7 @@ const app = new Vue({ updateAddress(id) { let root = this, form = document.getElementById(id), + formId = '#'+form.getAttribute('id'), data = new FormData(form) axios.post('/ajax/updateAddress', data) @@ -351,20 +367,20 @@ const app = new Vue({ root.validateForm = true }) .catch(function (error) { - root.errorHandling(error, root) + root.errorHandling(error, root, formId) } ) }, deleteAddress(index, form) { let root = this, form_ = document.getElementById(form), - data = { - index: index - } + data = { index: index } axios.post('/ajax/deleteaddress', data) .then(function (response) { - // + if(response.data) + root.addresses = response.data + form_.animate([ { opacity: 1 }, { opacity: 0 } @@ -378,7 +394,6 @@ const app = new Vue({ }, 1000) }) .catch(function (error) { - } ) } diff --git a/resources/views/components/address-form.blade.php b/resources/views/components/address-form.blade.php index 07633ab..b339064 100644 --- a/resources/views/components/address-form.blade.php +++ b/resources/views/components/address-form.blade.php @@ -1,7 +1,7 @@ -
-
+ +
    -
  • +
  • @{{ errorName[0] }}
@@ -16,14 +16,14 @@ {{ __('Société') }}* - +