]> _ Git - pmi.git/commitdiff
wip #5788 @7:30 valeur par defaut adresse si champ vide, fix disparition form mdp...
authorsoufiane <soufiane@cubedesigners.com>
Tue, 28 Mar 2023 13:35:14 +0000 (15:35 +0200)
committersoufiane <soufiane@cubedesigners.com>
Tue, 28 Mar 2023 13:35:14 +0000 (15:35 +0200)
app/Http/Controllers/AjaxController.php
app/SubForms/Address.php
resources/js/app.js
resources/views/components/address-form.blade.php
resources/views/pages/my_account.blade.php
resources/views/pages/sign_in.blade.php
resources/views/partials/nav-account.blade.php

index bb226e759d19500fc7c6a01a335c3c48eca5558f..f8507b5c2256853faf7883ca1e2beb07d5b6b579 100644 (file)
@@ -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 = [
index 24c6d1b3135d7a916811a5678541747ea6a9b35e..21f70f26b898a065ef6cc13f46c59f75ce9594fc 100644 (file)
@@ -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",
index 597d8168a1644775f4239a2c4b6e801ccc8caac2..54d806a3d93df64d46b8bedc05c2ef5e0eaa5368 100644 (file)
@@ -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) {
-
                 }
             )
         }
index 07633ab4d45d0d8043ed840f3f75be66043b26e6..b339064479060ffd11acc9a450029855d0cc39a8 100644 (file)
@@ -1,7 +1,7 @@
-<form :id="'update-address-'+key" class="form-portal max-w-half-form" @submit.prevent="updateAddress('update-address-'+key)">
-    <div class="form-errors mb-10" v-if="Object.keys(errorsForm).length > 0">
+<form :id="'update-address-'+index" class="form-portal max-w-half-form" @submit.prevent="updateAddress('update-address-'+index)">
+    <div class="form-errors mb-10" v-if="errorsForm['id'] === '#update-address-'+index">
         <ul class="list-disc list-inside text-red">
-            <li class="leading-5" v-for="(errorName,errorKey) in errorsForm" :key="errorKey">
+            <li class="leading-5" v-for="(errorName,errorKey) in errorsForm['errors']" :key="errorKey">
                 @{{ errorName[0] }}
             </li>
         </ul>
                 {{ __('Société') }}<span>*</span>
                 <input class="py-3 mt-3" type="text" v-model="address.company" required="required" name="address[company]" />
             </label>
-            <label class="form-input half text-navy">
-                {{ __('Nom') }}<span>*</span>
-                <input class="py-3 mt-3" type="text" v-model="address.lastname" required="required" name="address[lastname]" />
-            </label>
             <label class="form-input half text-navy">
                 {{ __('Prénom') }}<span>*</span>
                 <input class="py-3 mt-3" type="text" v-model="address.firstname" required="required" name="address[firstname]" />
             </label>
+            <label class="form-input half text-navy">
+                {{ __('Nom') }}<span>*</span>
+                <input class="py-3 mt-3" type="text" v-model="address.lastname" required="required" name="address[lastname]" />
+            </label>
             <label class="form-input text-navy">
                 {{ __('Adresse') }}<span>*</span>
                 <textarea class="py-3 mt-3" required="required" v-model="address.address" name="address[address]">
     </div>
     <div class="form-group">
         <label class="label-checkbox flex">
-            <input type="radio" :checked="default_billing_address !== 'billing'+key ? false : true" class="w-4 h-4" name="address[billing_address]" @click="default_billing_address = 'billing'+key" />
+            <input type="radio" :checked="default_billing_address !== 'billing'+index ? false : true" class="w-4 h-4" name="address[billing_address]" @click="default_billing_address = 'billing'+index" />
             <span class="ml-4">{{ __('Adresse de facturation par défaut') }}</span>
         </label>
         <label class="label-checkbox flex mb-6">
-            <input type="radio" :checked="default_delivery_address !== 'delivery'+key ? false : true" class="w-4 h-4" name="address[delivery_address]" @click="default_delivery_address = 'delivery'+key" />
+            <input type="radio" :checked="default_delivery_address !== 'delivery'+index ? false : true" class="w-4 h-4" name="address[delivery_address]" @click="default_delivery_address = 'delivery'+index" />
             <span class="ml-4">{{ __('Adresse de livraison par défaut') }}</span>
         </label>
     </div>
-    <input type="hidden" :value="key" name="index">
+    <input type="hidden" :value="index" name="index">
     <div class="form-footer flex flex-wrap-reverse justify-between items-center">
         <span class="form-required-legend inline-block my-4 mr-3 text-grey-dark xs:self-start xs:mt-5">
             *{{ __('Champs obligatoires') }}
@@ -58,7 +58,7 @@
             {{ __('Enregistrer les modifications') }}
         </button>
     </div>
-    <button class="flex items-center mt-12" @click.prevent="deleteAddress(key, 'update-address-'+key)" v-if="addresses.length > 1">
+    <button class="flex items-center mt-12" @click.prevent="deleteAddress(index, 'update-address-'+index)" v-if="addresses.length > 1">
         @svg('icon-trash')
         <span class="ml-3">Supprimer cette adresse</span>
     </button>
index 55b070c06fcc8f14910c0b1d3bd0543832774b23..24f52b7f2a40d7d83b71b15725d38106086c6783 100644 (file)
                         <div class="ajax-form flex flex-col pt-12">
                             <h2 class="text-2xl m-0">{{ __('Mes coordonnées') }}</h2>
                             <form id="update-details" class="form-portal max-w-half-form" @submit.prevent="update('update-details')">
-                                <div class="form-errors my-10" v-if="Object.keys(errorsForm).length > 0">
+
+                                <div class="form-errors my-10" v-if="errorsForm['errors']">
                                     <ul class="list-disc list-inside text-red">
-                                        <li class="leading-5" v-for="(errorName,errorKey) in errorsForm" :key="errorKey">
+                                        <li class="leading-5" v-for="(errorName,errorKey) in errorsForm['errors']" :key="errorKey">
                                             @{{ errorName[0] }}
                                         </li>
                                     </ul>
                                 </div>
+
                                 <div class="form-group fields grid">
-                                    <label class="form-input half text-navy">
-                                        {{ __('Nom') }}<span>*</span>
-                                        <input class="py-3 mt-3" type="text"  v-model="user.lastname" required="required" name="lastname" />
-                                    </label>
                                     <label class="form-input half text-navy">
                                         {{ __('Prénom') }}<span>*</span>
                                         <input class="py-3 mt-3" type="text" v-model="user.firstname" required="required" name="firstname" />
                                     </label>
+                                    <label class="form-input half text-navy">
+                                        {{ __('Nom') }}<span>*</span>
+                                        <input class="py-3 mt-3" type="text"  v-model="user.lastname" required="required" name="lastname" />
+                                    </label>
                                 </div>
                                 <div class="form-group">
                                     <label class="form-input half text-navy">
@@ -78,7 +80,7 @@
                                 @svg('icon-white-add')
                             </button>
                         </div>
-                        <div class="ajax-form flex flex-col pt-12" v-for="(address, key, index) in addresses" :key="key">
+                        <div class="ajax-form flex flex-col pt-12" v-for="(address, index) in addresses" :key="index">
                             <address-form />
                         </div>
                     </div>
@@ -87,9 +89,9 @@
                         <div class="pb-12 border-b border-b-light-b">
                             <h2 class="text-2xl m-0">{{ __('Modifier mon mot de passe') }}</h2>
                         </div>
-                        <div class="signup-form-errors mb-10" v-if="Object.keys(errorsForm).length > 0">
+                        <div class="signup-form-errors mb-10" v-if="errorsForm['errors']">
                             <ul class="list-disc list-inside text-red">
-                                <li class="leading-5" v-for="(errorName,errorKey) in errorsForm" :key="errorKey">
+                                <li class="leading-5" v-for="(errorName,errorKey) in errorsForm['errors']" :key="errorKey">
                                     @{{ errorName[0] }}
                                 </li>
                             </ul>
index 359a0dd2b64bb8ccb3b20bad31d73e8ab95b60d9..4fcc151c3367ea7c0e29f888ae8cad00edc3b7fd 100644 (file)
@@ -7,9 +7,9 @@
                 <div class="form-info text-navy mb-10">
                     <h1 class="text-4xl m-0">Se connecter</h1>
                 </div>
-                <div class="signup-form-errors mb-10" v-if="Object.keys(errorsForm).length > 0">
+                <div class="form-errors mb-10" v-cloak v-if="errorsForm['errors']">
                     <ul class="list-disc list-inside text-red">
-                        <li class="leading-5" v-for="(errorName,errorKey) in errorsForm" :key="errorKey">
+                        <li class="leading-5" v-for="(errorName,errorKey) in errorsForm['errors']" :key="errorKey">
                             @{{ errorName[0] }}
                         </li>
                     </ul>
                 </div>
 
                 <form id="signin-form" class="form-portal" @submit.prevent="signin">
-                    <div class="form-errors mb-10" v-if="Object.keys(errorsForm).length > 0">
+
+                    <div class="form-errors mb-10" v-cloak v-if="errorsForm['errors']">
                         <ul class="list-disc list-inside text-red">
-                            <li class="leading-5" v-for="(errorName,errorKey) in errorsForm" :key="errorKey">
+                            <li class="leading-5" v-for="(errorName,errorKey) in errorsForm['errors']" :key="errorKey">
                                 @{{ errorName[0] }}
                             </li>
                         </ul>
                     </div>
+
                     @csrf
                     <div class="form-group mb-6">
                         <label class="form-input text-navy">
@@ -93,9 +95,9 @@
                     <h1 class="text-4xl m-0">{{ __("Créer un compte") }}</h1>
                 </div>
 
-                <div class="signup-form-errors mb-10" v-if="Object.keys(errorsForm).length > 0">
+                <div class="form-errors mb-10" v-if="errorsForm['errors']">
                     <ul class="list-disc list-inside text-red">
-                        <li class="leading-5" v-for="(errorName,errorKey) in errorsForm" :key="errorKey">
+                        <li class="leading-5" v-for="(errorName,errorKey) in errorsForm['errors']" :key="errorKey">
                             @{{ errorName[0] }}
                         </li>
                     </ul>
index 935e3d15b415dbca4506273c11a9f5377536536a..1684789d5cbcf30280484413566dcffd07ee2dda 100644 (file)
@@ -4,13 +4,13 @@
             <a href="/mon-compte">
                 <span>{{ __('Mon compte') }}</span>
             </a>
-            <ul>
+            <!--<ul>
                 <li :class="{ active : tab === 'address' }">
                     <a href="/mon-compte#address" @click.stop="tab = 'address'">
                         <span>{{ __('Mes adresses') }}</span>
                     </a>
                 </li>
-            </ul>
+            </ul>-->
         </li>
         <li class="{{ Request::is('mes-commandes') ? "active" : '' }}">
             <a href="">