]> _ Git - pmi.git/commitdiff
Cart and quote functionality. WIP #2769 @7
authorStephen Cameron <stephen@cubedesigners.com>
Mon, 12 Aug 2019 15:34:29 +0000 (17:34 +0200)
committerStephen Cameron <stephen@cubedesigners.com>
Mon, 12 Aug 2019 15:34:29 +0000 (17:34 +0200)
app/Templates/Cart.php [new file with mode: 0644]
resources/js/app.js
resources/js/components/Cart.vue
resources/js/components/CartItem.vue
resources/styles/components/cart.styl
resources/views/layouts/app.blade.php
resources/views/pages/cart.blade.php [new file with mode: 0644]
resources/views/partials/cart.blade.php
resources/views/partials/form.blade.php
resources/views/partials/header.blade.php
tailwind.config.js

diff --git a/app/Templates/Cart.php b/app/Templates/Cart.php
new file mode 100644 (file)
index 0000000..753a749
--- /dev/null
@@ -0,0 +1,28 @@
+<?php
+
+
+namespace App\Templates;
+
+
+class Cart extends Base
+{
+    public function getName()
+    {
+        return 'Ma sélection';
+    }
+
+    public function init()
+    {
+        parent::init();
+
+        $this->removeField('intro');
+
+        $this->addField(['name'  => 'page_heading',
+                         'type'  => 'Text',
+                         'label' => 'Titre de la page',
+                         'tab'   => 'Contenus']);
+
+        // Enable form
+        $this->addForm();
+    }
+}
index 5b49d61daa4025bb73cb6a698c6902faacf525dc..104b03a32dc2d4306eca4590583d28704182e715 100644 (file)
@@ -9,6 +9,7 @@ require('./bootstrap');
 require('./menu');
 
 window.Vue = require('vue');
+window.eventBus = new Vue();
 
 import PortalVue from 'portal-vue';
 Vue.use(PortalVue);
@@ -37,10 +38,22 @@ const app = new Vue({
       items: {}, // Populated from data attribute on root element so we can pass data from PHP
     },
 
-    beforeMount: function () {
+    beforeMount() {
       this.items = JSON.parse(this.$el.dataset.cartItems);
     },
 
+    mounted() {
+        eventBus.$on('update-item-quantity', update => {
+            const index = this.items.findIndex((item) => item.id == update.id);
+            this.items[index].quantity = update.quantity;
+        });
+
+        eventBus.$on('delete-item', id => {
+            const index = this.items.findIndex((item) => item.id == id);
+            this.items.splice(index, 1);
+        });
+    },
+
     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.
@@ -49,16 +62,13 @@ const app = new Vue({
     },
 
     methods: {
-      // Todo: find a better way to structure this with Vue
+
       openCart() {
         document.body.classList.add('cart-open');
       },
       closeCart() {
         document.body.classList.remove('cart-open');
       },
-      updateItemQuantity(update) {
-        this.items[update.id].quantity = update.quantity;
-      }
 
     }
 
index 6e3ff2efff33fc43004a589a7239d870006a068b..81b6bf15237e341ec084ed344970b0a17dca309a 100644 (file)
@@ -1,6 +1,6 @@
 <template>
     <div class="cart-wrapper">
-        <cart-item v-for="item in items" :key="item.id" :item="item" @update-item-quantity="updateItemQuantity"></cart-item>
+        <cart-item v-for="item in items" :key="item.id" :item="item"></cart-item>
     </div>
 </template>
 
         },
 
         methods: {
-            // Todo: consider event bus to avoid having to pass this up through all the levels?
-            updateItemQuantity(update) {
-                this.$emit('update-item-quantity', update)
-            },
         }
     }
 </script>
index 244258639edec69a93a61d02f00d105cefdb14f8..d9e347b0d7491c901a4191372f06e9d36bcc2074 100644 (file)
@@ -9,11 +9,11 @@
                 <br>
                 {{ item.name }}
             </div>
-            <div class="bg-grey-100 py-1 pl-3 my-2 flex items-center justify-between">
+            <div class="cart-quantity bg-grey-100 py-1 pl-3 my-2 flex items-center justify-between">
                 <span class="mr-2">Quantité</span>
                 <number-input :value="item.quantity" :min="1" inline center controls @change="updateQuantity"></number-input>
             </div>
-            <a href="#" class="cart-delete-item text-red" @click.prevent="deleteItem">
+            <a href="#" class="cart-delete-item text-red block" @click.prevent="deleteItem">
                 Supprimer
             </a>
         </div>
 
         methods: {
             updateQuantity(newValue, oldValue) {
-                this.$emit('update-item-quantity', {
+                eventBus.$emit('update-item-quantity', {
                     id: this.item.id,
                     quantity: newValue,
                 });
             },
 
             deleteItem() {
-                this.$emit('deleteItem', this.item.id);
+                eventBus.$emit('delete-item', this.item.id);
             }
         }
     }
index a6966fe676ed08da6b7c1737b58ba5fd4853e676..24b7b0237c85e4e0afc40f1e0b737d9e0645391c 100644 (file)
@@ -50,3 +50,8 @@
     z-index: 15
     background-color: theme('colors.navy')
     opacity: 0.1
+
+// Main cart page view
+.cart-page
+  .cart-quantity
+    @apply bg-white inline-flex
index 2588ab29a40d955425324cc27124133fb369bd68..1766fa30037b72cdf8583894a771f77bd77b56f4 100644 (file)
@@ -16,9 +16,9 @@
     if (config('features.quote')) {
         //#### Generate temporary cart data
         $cart_items = [];
-        for ($i = 1; $i <= 6; $i++) {
+        for ($i = 0; $i < 6; $i++) {
             $cart_items[$i] = [
-                'id' => $i,
+                'id' => $i + 1,
                 'quantity' => rand(1, 15),
                 'name' => 'Modèle '. rand(1000, 1500),
                 'category' => 'Capteur de force',
diff --git a/resources/views/pages/cart.blade.php b/resources/views/pages/cart.blade.php
new file mode 100644 (file)
index 0000000..e17e213
--- /dev/null
@@ -0,0 +1,26 @@
+@extends('layouts/app')
+
+@section('content')
+
+    <content class="pb-3v">
+        <text-block :title="$page->page_heading" />
+
+        {{-- Columns set to auto because cart will disappear when quote form is sent, leaving just the confirmation message. --}}
+        <grid cols="auto">
+
+            {{-- Nested divs to allow grey backgrounds of columns to match the height of their content instead of total height --}}
+            <div v-if="cartItemCount > 0">
+                <cart :items='items' class="cart-page bg-grey-100 p-1v"></cart>
+            </div>
+
+            <div>
+                <div class="bg-grey-100 p-1v">
+                    @form
+                </div>
+            </div>
+
+        </grid>
+
+    </content>
+
+@endsection
index 164e5eb7e9ba19a85094f822f7604fcadb4bd562..236a6f31fc286591164892812af632e4c7eb7411 100644 (file)
@@ -1,2 +1,2 @@
-<span class="cart-header-title">My Selection</span>
+<span class="cart-header-title">{{ __('Ma sélection') }}</span>
 <span class="cart-header-icon" v-html="cartItemCount"></span>
index cb01ad8753f2de4f5056ab107802c8144882c9d9..1628bed1bb5fa3a8e1f480e7770c18fa215588dd 100644 (file)
@@ -5,7 +5,7 @@
     @endpush
 
 <div id="contact-form">
-    <div class="ajax-form flex flex-col">
+    <div class="ajax-form flex flex-col max-w-half">
         @if($page->get('form_intro'))
             <p class="form-info text-navy">{{$page->get('form_intro')}}</p>
         @endif
index 85b8b3145a20b7c086f3f3251946a275b43dae8a..b4d55746f7bdac25362d519ade0567a1eee84380 100644 (file)
                     </a>
                 </div>
                 <div class="cart-header-popout-content text-navy font-body p-1v pb-0">
-                    <cart :items='items' @update-item-quantity="updateItemQuantity"></cart>
+                    <cart :items='items'></cart>
+                    <div v-if="cartItemCount == 0" class="pb-1v">
+                        {{ __("Vous n'avez encore rien sélectionné") }}
+                    </div>
                 </div>
-                <div class="cart-header-popout-footer bg-grey-100 p-1v">
-                    <link-button class="block text-center">Obtenir un devis</link-button>
+                <div class="cart-header-popout-footer bg-grey-100 p-1v" v-if="cartItemCount > 0">
+                    <link-button class="block text-center" :href="$nav->getHrefByName('cart')">
+                        {{ __('Obtenir un devis') }}
+                    </link-button>
                 </div>
             </div>
         @endif
index e44e51735d65792f6b1a15e986b4c45068a853fe..331b67a566ca531906ea85d485ea0fc36c136e97 100644 (file)
@@ -36,6 +36,7 @@ module.exports = {
                 'body': ['Muli', 'sans-serif'], // Main blocks of text
             },
             maxWidth: {
+                'half': '768px', // Half a full-width content area
                 'text': '480px' // Or 30rem?
             },
             padding: {