Building Ecommerce Website With PHP Lumen Laravel And Nuxtjs

Building Ecommerce Website With Lumen Laravel And Nuxtjs 18: Shipping Addresses

In this post we will prepare and implement the shipping addresses module in the user profile, as we will need this when working on the checkout process.

 

 

 

Shipping Address CRUD

Let’s start implementing the CRUD process in the Lumen project, this is a simple CRUD controller you already saw similar controllers like that so i won’t go into much details in explaining it.

 

In the lumen project create a new controller ShippingAddressesController.php

  app/Http/Controllers/ShippingAddressesController.php

<?php


namespace App\Http\Controllers;


use App\Models\ShippingAddress;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Validator;

class ShippingAddressesController extends Controller
{
    public function __construct()
    {
    }

    public function index()
    {
        $addresses = ShippingAddress::where('user_id', Auth::user()->id)->orderBy('is_primary', 'DESC')->get();

        return response()->json(['shipping_addresses' => $addresses], 200);
    }

    public function store(Request $request)
    {
        $validator = Validator::make($request->all(), [
           "address" => "required",
           "mobile" => "required",
           "country" => "required",
            "city" => "required",
            "postal_code" => "required"
        ]);

        if($validator->fails()) {
            return response()->json(['success' => 0, 'message' => 'Required or incorrect fields', 'errors' => $validator->errors()], 500);
        }

        $user = Auth::user();

        $shippingAddress = new ShippingAddress();
        $shippingAddress->address = $request->input("address");
        $shippingAddress->country = $request->input("country");
        $shippingAddress->city = $request->input("city");
        $shippingAddress->postal_code = $request->input("postal_code");
        $shippingAddress->mobile = $request->input("mobile");
        $shippingAddress->user_id = $user->id;

        if($request->has('is_primary')) {
            $this->revertShippingAddresses();

            $shippingAddress->is_primary = 1;
        } else {
            if (ShippingAddress::where('user_id', $user->id)->count() == 0) {
                $shippingAddress->is_primary = 1;
            } else {
                $shippingAddress->is_primary = 0;
            }
        }

        $shippingAddress->save();

        return response()->json(['success' => 1, 'message' => 'Address saved successfully', 'shipping_address' => $shippingAddress], 200);
    }

    public function show($id)
    {
        $address = ShippingAddress::where('id', $id)->where('user_id', Auth::user()->id)->first();

        return response()->json(['shipping_address' => $address]);
    }

    public function update(Request $request, $id)
    {
        $validator = Validator::make($request->all(), [
            "address" => "required",
            "mobile" => "required",
            "country" => "required",
            "city" => "required",
            "postal_code" => "required"
        ]);

        if($validator->fails()) {
            return response()->json(['success' => 0, 'message' => 'Required or incorrect fields', 'errors' => $validator->errors()], 500);
        }

        $user = Auth::user();

        $shippingAddress = ShippingAddress::where('id', $id)->where('user_id', Auth::user()->id)->first();

        if(!$shippingAddress) {
            return response()->json(['success' => 0, 'message' => 'address not found'], 404);
        }

        if($request->has('is_primary')) {
            $this->revertShippingAddresses();
        }

        $shippingAddress->address = $request->input("address");
        $shippingAddress->country = $request->input("country");
        $shippingAddress->city = $request->input("city");
        $shippingAddress->postal_code = $request->input("postal_code");
        $shippingAddress->mobile = $request->input("mobile");
        $shippingAddress->user_id = $user->id;

        if($request->has('is_primary')) {

            $shippingAddress->is_primary = 1;
        } else {
            $shippingAddress->is_primary = 0;
        }

        $shippingAddress->save();

        return response()->json(['success' => 1, 'message' => 'Address updated successfully', 'shipping_address' => $shippingAddress], 200);
    }

    public function destroy($id)
    {
        $shippingAddress = ShippingAddress::where('id', $id)->where('user_id', Auth::user()->id)->first();

        if(!$shippingAddress) {
            return response()->json(['success' => 0, 'message' => 'address not found'], 404);
        }

        $is_primary = $shippingAddress->is_primary;

        $shippingAddress->delete();

        // check if there are existing addresses then update first as primary
        if($is_primary) {
            $shippingAddress = ShippingAddress::where('user_id', Auth::user()->id)->first();

            if ($shippingAddress) {
                $shippingAddress->is_primary = 1;
                $shippingAddress->save();
            }
        }

        return response()->json(['success' => 1, 'message' => 'Address deleted successfully'], 200);
    }

    private function revertShippingAddresses()
    {
        foreach (ShippingAddress::where('user_id',Auth::user()->id)->get() as $address) {
            $address->is_primary = 0;
            $address->save();
        }
    }
}

The store() and update() methods do create and update a shipping address respectively. Each shipping address should have country, city, address, postal_code, mobile. If the shipping address is the first address then we made as the primary address by default. Primary shipping addresses will be shown on the checkout when performing order checkout and payment.

 

Shipping Address Routes

Update routes/web.php add the shipping addresses routes under the cart routes group:

$router->group(['prefix' => 'shippingAddress'], function () use ($router) {
            $router->get('/', 'ShippingAddressesController@index');
            $router->post('/', 'ShippingAddressesController@store');
            $router->get('/{id}', 'ShippingAddressesController@show');
            $router->put('/{id}', 'ShippingAddressesController@update');
            $router->delete('/{id}', 'ShippingAddressesController@destroy');
        });

 

 

Shipping Address Display

The next step to display the shipping addresses in the Nuxt project using the restful apis we have created above. So open the main nuxt project and add a new api file in the api/ directory.

online-shop-frontend/api/shipping-address.js

const ShippingAddressApi = {
  setAuthToken: (axios) => {
    axios.setHeader('Authorization', "Bearer " + localStorage.getItem('auth_token'));
  },
  store: (axios, payload) => {
    ShippingAddressApi.setAuthToken(axios);
    return axios.$post('/api/shippingAddress', payload);
  },
  update: (axios, payload, id) => {
    ShippingAddressApi.setAuthToken(axios);

    const formData = new FormData();
    for(let field in payload) {
      formData.append(field, payload[field]);
    }

    formData.append('_method', 'put');

    return axios.$post('/api/shippingAddress/' + id, formData, {
      headers: {
        'Content-Type': 'application/x-www-form-urlencoded'
      }
    });
  },
  list: (axios) => {
    ShippingAddressApi.setAuthToken(axios);
    return axios.$get('/api/shippingAddress');
  },
  delete: (axios, id) => {
    ShippingAddressApi.setAuthToken(axios);
    return axios.$delete('/api/shippingAddress/' + id);
  },
  show: (axios, id) => {
    ShippingAddressApi.setAuthToken(axios);
    return axios.$get('/api/shippingAddress/' + id);
  }
}

export {ShippingAddressApi};

As you see there five main operations store, update, list, delete, show. In each operation we have to send the auth token for the current user so i created another helper function which is the setAuthToken() which read the auth token from localStorage and update the axios Authorization header.

 

Shipping Address Page

The shipping address page display the user shipping addresses list in a tabular basis, beside each row there are a button to edit and delete the shipping address.In top of the grid there are a button to add a new address, this triggers a modal to open address form.

pages/shipping-addresses.vue

<template>
  <div class="container">
    <div class="bg">
      <div class="row">
        <div class="col-sm-12">
          <h2 class="title text-center">Shipping Addresses</h2>
        </div>
      </div>

      <div class="row">

        <div class="col-md-3">
          <account-sidebar></account-sidebar>
        </div>

        <div class="col-sm-9 col-md-9">
          <button type="button" class="btn btn-primary pull-right" v-on:click.prevent="createAddress()">Add Address</button>
          <div class="well well-sm">
            <div class="row">
              <div class="col-sm-12" style="margin-top: 5px">
                  <p class="alert alert-info" v-if="!this.shipping_addresses.length">No shipping addresses! <a href="#" v-on:click.prevent="createAddress()">Add Address</a> </p>
                  <table class="table table-condensed" v-if="this.shipping_addresses.length > 0">
                    <thead>
                        <tr>
                          <th>Address</th>
                          <th>Country</th>
                          <th>City</th>
                          <th>Postal Code</th>
                          <th class="text-center">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                       <tr v-for="address in this.shipping_addresses" :key="address.id">
                         <td>
                           <address>
                             <strong v-if="address.is_primary == 1">{{address.address}} (Primary)</strong>
                             <span v-if="address.is_primary == 0">{{address.address}}</span>
                             <span style="display: block">{{address.mobile}}</span>
                           </address>
                         </td>
                         <td>{{ address.country }}</td>
                         <td>{{ address.city }}</td>
                         <td>{{ address.postal_code }}</td>
                         <td class="text-center">
                           <a href="#" @click.prevent="showAddress(address.id)" class="btn btn-info btn-sm"><i class="fa fa-edit"></i></a>
                           <a href="#" @click.prevent="deleteAddress(address.id)" class="btn btn-danger btn-sm"><i class="fa fa-trash-o"></i></a>
                         </td>
                       </tr>
                    </tbody>
                  </table>

                  <shipping-address-form v-if="this.showForm" v-on:dismissModal="dismiss()" v-on:submitForm="refresh()" :edit-shipping-address="edit_shipping_address" :is_edit="is_edit"></shipping-address-form>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
    import AccountSidebar from "../components/account-components/AccountSidebar";
    import {ShippingAddressApi} from "../api/shipping-address";
    import ShippingAddressForm from "../components/shipping-address-components/Form";

    export default {
      name: "ShippingAddresses",
      components: {ShippingAddressForm, AccountSidebar},
      middleware: "auth",
      head() {
        return {
          title: 'Online Shop | My Shipping Addresses',
          meta: [
            {
              hid: 'description',
              name: 'description',
              content: 'My Shipping Addresses Page'
            }
          ]
        }
      },
      data() {
        return {
          shipping_addresses: [],
          showForm: false,
          is_edit: false,
          edit_shipping_address: {}
        }
      },
      mounted() {
        this.fetchAddresses();
      },
      methods: {
        fetchAddresses() {
          ShippingAddressApi.list(this.$axios).then(response => {
            this.shipping_addresses = response.shipping_addresses;
          });
        },
        createAddress() {
          
        },
        dismiss() {
          
        },
        refresh() {
          
        },
        showAddress(id) {
          
        },
        deleteAddress(id) {
          
        }
      }
    }
</script>

The above page displays the shipping addresses list. In the data() method i have added several keys like shipping_addresses array, showForm flag which will be used to trigger the create and update address modal, is_edit flag to specify that this is an edit or create, edit_shipping_address object to contain specific address details to edit.

In the mounted() hook we fetch the addresses by invoking fetchAddresses() method which makes an axios request to list the addresses, at this point the page will display no addresses as you don’t have any addresses yet. Now let’s create the <shipping-address-form /> component.

 

components/shipping-address-components/Form.vue

<template>
  <div class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" aria-label="Close" @click="dismiss()"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title">{{ this.is_edit ? "Edit" : "Add" }} Shipping Address</h4>
        </div>
        <div class="modal-body">
          <div class="alert alert-success" v-if="this.success_message != ''">{{ this.success_message }}</div>
          <div class="alert alert-danger" v-if="this.error_message != ''">{{ this.error_message }}</div>

            <ul class="sufee-alert alert with-close alert-danger alert-dismissible" v-if="this.validation_errors.length">
              <li v-for="(error, index) in this.validation_errors" :key="index">{{ error }}</li>
            </ul>

          <form class="form-horizontal">
            <div class="form-group">
              <label>Country</label>
              <Countries :country="country" @countryUpdated="setCountry"></Countries>
            </div>

            <div class="form-group">
              <label>City</label>
              <input type="text" placeholder="City" name="city" class="form-control" v-model="city" />
            </div>

            <div class="form-group">
              <label>Address</label>
              <input type="text" placeholder="Address" name="address" class="form-control" v-model="address" />
            </div>

            <div class="form-group">
              <label>Postal Code</label>
              <input type="text" placeholder="Postal Code" name="postal_code" class="form-control" v-model="postal_code" />
            </div>

            <div class="form-group">
              <label>Mobile</label>
              <input type="text" placeholder="Mobile" name="mobile" class="form-control" v-model="mobile" />
            </div>

            <div class="form-group">
              <label>Is Primary</label>
              <input type="checkbox" name="is_primary" v-model="is_primary" />
            </div>

          </form>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" @click="dismiss()">Close</button>
          <button type="button" class="btn btn-primary" @click="saveAddress()">Save</button>
        </div>
      </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
  </div>
</template>

<script>
    import {ShippingAddressApi} from "../../api/shipping-address";
    import Countries from "./Countries";

    export default {
        name: "ShippingAddressForm",
      components: {Countries},
      props: ["is_edit", "editShippingAddress"],
        data() {
          return {
            address: "",
            country: "",
            city: "",
            postal_code: "",
            mobile: "",
            is_primary: false,
            success_message: true,
            error_message: false,
            validation_errors: []
          }
        },
        methods: {
          dismiss() {
            this.$emit('dismissModal');
          },
          setCountry(event) {
            this.country = event;
          },
          saveAddress() {
           
          },
          resetForm() {
            
          }
        },
        mounted() {
          
        }
    }
</script>

<style scoped>
  div.modal {
    display: inline-block !important;
  }
</style>

components/shipping-address-components/Countries.vue

<template>
  <select name="country" class="form-control" :value="country" @change="setCountry">
    <option value="">None</option>
    <option value="AF">Afghanistan</option>
    <option value="AX">Åland Islands</option>
    <option value="AL">Albania</option>
    <option value="DZ">Algeria</option>
    <option value="AS">American Samoa</option>
    <option value="AD">Andorra</option>
    <option value="AO">Angola</option>
    <option value="AI">Anguilla</option>
    <option value="AQ">Antarctica</option>
    <option value="AG">Antigua and Barbuda</option>
    <option value="AR">Argentina</option>
    <option value="AM">Armenia</option>
    <option value="AW">Aruba</option>
    <option value="AU">Australia</option>
    <option value="AT">Austria</option>
    <option value="AZ">Azerbaijan</option>
    <option value="BS">Bahamas</option>
    <option value="BH">Bahrain</option>
    <option value="BD">Bangladesh</option>
    <option value="BB">Barbados</option>
    <option value="BY">Belarus</option>
    <option value="BE">Belgium</option>
    <option value="BZ">Belize</option>
    <option value="BJ">Benin</option>
    <option value="BM">Bermuda</option>
    <option value="BT">Bhutan</option>
    <option value="BO">Bolivia, Plurinational State of</option>
    <option value="BQ">Bonaire, Sint Eustatius and Saba</option>
    <option value="BA">Bosnia and Herzegovina</option>
    <option value="BW">Botswana</option>
    <option value="BV">Bouvet Island</option>
    <option value="BR">Brazil</option>
    <option value="IO">British Indian Ocean Territory</option>
    <option value="BN">Brunei Darussalam</option>
    <option value="BG">Bulgaria</option>
    <option value="BF">Burkina Faso</option>
    <option value="BI">Burundi</option>
    <option value="KH">Cambodia</option>
    <option value="CM">Cameroon</option>
    <option value="CA">Canada</option>
    <option value="CV">Cape Verde</option>
    <option value="KY">Cayman Islands</option>
    <option value="CF">Central African Republic</option>
    <option value="TD">Chad</option>
    <option value="CL">Chile</option>
    <option value="CN">China</option>
    <option value="CX">Christmas Island</option>
    <option value="CC">Cocos (Keeling) Islands</option>
    <option value="CO">Colombia</option>
    <option value="KM">Comoros</option>
    <option value="CG">Congo</option>
    <option value="CD">Congo, the Democratic Republic of the</option>
    <option value="CK">Cook Islands</option>
    <option value="CR">Costa Rica</option>
    <option value="CI">Côte d'Ivoire</option>
    <option value="HR">Croatia</option>
    <option value="CU">Cuba</option>
    <option value="CW">Curaçao</option>
    <option value="CY">Cyprus</option>
    <option value="CZ">Czech Republic</option>
    <option value="DK">Denmark</option>
    <option value="DJ">Djibouti</option>
    <option value="DM">Dominica</option>
    <option value="DO">Dominican Republic</option>
    <option value="EC">Ecuador</option>
    <option value="EG">Egypt</option>
    <option value="SV">El Salvador</option>
    <option value="GQ">Equatorial Guinea</option>
    <option value="ER">Eritrea</option>
    <option value="EE">Estonia</option>
    <option value="ET">Ethiopia</option>
    <option value="FK">Falkland Islands (Malvinas)</option>
    <option value="FO">Faroe Islands</option>
    <option value="FJ">Fiji</option>
    <option value="FI">Finland</option>
    <option value="FR">France</option>
    <option value="GF">French Guiana</option>
    <option value="PF">French Polynesia</option>
    <option value="TF">French Southern Territories</option>
    <option value="GA">Gabon</option>
    <option value="GM">Gambia</option>
    <option value="GE">Georgia</option>
    <option value="DE">Germany</option>
    <option value="GH">Ghana</option>
    <option value="GI">Gibraltar</option>
    <option value="GR">Greece</option>
    <option value="GL">Greenland</option>
    <option value="GD">Grenada</option>
    <option value="GP">Guadeloupe</option>
    <option value="GU">Guam</option>
    <option value="GT">Guatemala</option>
    <option value="GG">Guernsey</option>
    <option value="GN">Guinea</option>
    <option value="GW">Guinea-Bissau</option>
    <option value="GY">Guyana</option>
    <option value="HT">Haiti</option>
    <option value="HM">Heard Island and McDonald Islands</option>
    <option value="VA">Holy See (Vatican City State)</option>
    <option value="HN">Honduras</option>
    <option value="HK">Hong Kong</option>
    <option value="HU">Hungary</option>
    <option value="IS">Iceland</option>
    <option value="IN">India</option>
    <option value="ID">Indonesia</option>
    <option value="IR">Iran, Islamic Republic of</option>
    <option value="IQ">Iraq</option>
    <option value="IE">Ireland</option>
    <option value="IM">Isle of Man</option>
    <option value="IL">Israel</option>
    <option value="IT">Italy</option>
    <option value="JM">Jamaica</option>
    <option value="JP">Japan</option>
    <option value="JE">Jersey</option>
    <option value="JO">Jordan</option>
    <option value="KZ">Kazakhstan</option>
    <option value="KE">Kenya</option>
    <option value="KI">Kiribati</option>
    <option value="KP">Korea, Democratic People's Republic of</option>
    <option value="KR">Korea, Republic of</option>
    <option value="KW">Kuwait</option>
    <option value="KG">Kyrgyzstan</option>
    <option value="LA">Lao People's Democratic Republic</option>
    <option value="LV">Latvia</option>
    <option value="LB">Lebanon</option>
    <option value="LS">Lesotho</option>
    <option value="LR">Liberia</option>
    <option value="LY">Libya</option>
    <option value="LI">Liechtenstein</option>
    <option value="LT">Lithuania</option>
    <option value="LU">Luxembourg</option>
    <option value="MO">Macao</option>
    <option value="MK">Macedonia, the former Yugoslav Republic of</option>
    <option value="MG">Madagascar</option>
    <option value="MW">Malawi</option>
    <option value="MY">Malaysia</option>
    <option value="MV">Maldives</option>
    <option value="ML">Mali</option>
    <option value="MT">Malta</option>
    <option value="MH">Marshall Islands</option>
    <option value="MQ">Martinique</option>
    <option value="MR">Mauritania</option>
    <option value="MU">Mauritius</option>
    <option value="YT">Mayotte</option>
    <option value="MX">Mexico</option>
    <option value="FM">Micronesia, Federated States of</option>
    <option value="MD">Moldova, Republic of</option>
    <option value="MC">Monaco</option>
    <option value="MN">Mongolia</option>
    <option value="ME">Montenegro</option>
    <option value="MS">Montserrat</option>
    <option value="MA">Morocco</option>
    <option value="MZ">Mozambique</option>
    <option value="MM">Myanmar</option>
    <option value="NA">Namibia</option>
    <option value="NR">Nauru</option>
    <option value="NP">Nepal</option>
    <option value="NL">Netherlands</option>
    <option value="NC">New Caledonia</option>
    <option value="NZ">New Zealand</option>
    <option value="NI">Nicaragua</option>
    <option value="NE">Niger</option>
    <option value="NG">Nigeria</option>
    <option value="NU">Niue</option>
    <option value="NF">Norfolk Island</option>
    <option value="MP">Northern Mariana Islands</option>
    <option value="NO">Norway</option>
    <option value="OM">Oman</option>
    <option value="PK">Pakistan</option>
    <option value="PW">Palau</option>
    <option value="PS">Palestinian Territory, Occupied</option>
    <option value="PA">Panama</option>
    <option value="PG">Papua New Guinea</option>
    <option value="PY">Paraguay</option>
    <option value="PE">Peru</option>
    <option value="PH">Philippines</option>
    <option value="PN">Pitcairn</option>
    <option value="PL">Poland</option>
    <option value="PT">Portugal</option>
    <option value="PR">Puerto Rico</option>
    <option value="QA">Qatar</option>
    <option value="RE">Réunion</option>
    <option value="RO">Romania</option>
    <option value="RU">Russian Federation</option>
    <option value="RW">Rwanda</option>
    <option value="BL">Saint Barthélemy</option>
    <option value="SH">Saint Helena, Ascension and Tristan da Cunha</option>
    <option value="KN">Saint Kitts and Nevis</option>
    <option value="LC">Saint Lucia</option>
    <option value="MF">Saint Martin (French part)</option>
    <option value="PM">Saint Pierre and Miquelon</option>
    <option value="VC">Saint Vincent and the Grenadines</option>
    <option value="WS">Samoa</option>
    <option value="SM">San Marino</option>
    <option value="ST">Sao Tome and Principe</option>
    <option value="SA">Saudi Arabia</option>
    <option value="SN">Senegal</option>
    <option value="RS">Serbia</option>
    <option value="SC">Seychelles</option>
    <option value="SL">Sierra Leone</option>
    <option value="SG">Singapore</option>
    <option value="SX">Sint Maarten (Dutch part)</option>
    <option value="SK">Slovakia</option>
    <option value="SI">Slovenia</option>
    <option value="SB">Solomon Islands</option>
    <option value="SO">Somalia</option>
    <option value="ZA">South Africa</option>
    <option value="GS">South Georgia and the South Sandwich Islands</option>
    <option value="SS">South Sudan</option>
    <option value="ES">Spain</option>
    <option value="LK">Sri Lanka</option>
    <option value="SD">Sudan</option>
    <option value="SR">Suriname</option>
    <option value="SJ">Svalbard and Jan Mayen</option>
    <option value="SZ">Swaziland</option>
    <option value="SE">Sweden</option>
    <option value="CH">Switzerland</option>
    <option value="SY">Syrian Arab Republic</option>
    <option value="TW">Taiwan, Province of China</option>
    <option value="TJ">Tajikistan</option>
    <option value="TZ">Tanzania, United Republic of</option>
    <option value="TH">Thailand</option>
    <option value="TL">Timor-Leste</option>
    <option value="TG">Togo</option>
    <option value="TK">Tokelau</option>
    <option value="TO">Tonga</option>
    <option value="TT">Trinidad and Tobago</option>
    <option value="TN">Tunisia</option>
    <option value="TR">Turkey</option>
    <option value="TM">Turkmenistan</option>
    <option value="TC">Turks and Caicos Islands</option>
    <option value="TV">Tuvalu</option>
    <option value="UG">Uganda</option>
    <option value="UA">Ukraine</option>
    <option value="AE">United Arab Emirates</option>
    <option value="GB">United Kingdom</option>
    <option value="US">United States</option>
    <option value="UM">United States Minor Outlying Islands</option>
    <option value="UY">Uruguay</option>
    <option value="UZ">Uzbekistan</option>
    <option value="VU">Vanuatu</option>
    <option value="VE">Venezuela, Bolivarian Republic of</option>
    <option value="VN">Viet Nam</option>
    <option value="VG">Virgin Islands, British</option>
    <option value="VI">Virgin Islands, U.S.</option>
    <option value="WF">Wallis and Futuna</option>
    <option value="EH">Western Sahara</option>
    <option value="YE">Yemen</option>
    <option value="ZM">Zambia</option>
    <option value="ZW">Zimbabwe</option>
  </select>
</template>

<script>
    export default {
        name: "Countries",
        props: ["country"],
        methods: {
          setCountry(event) {
            this.$emit("countryUpdated", event.target.value);
          }
        }
    }
</script>

Now if run the app and navigate to /shipping-addresses you will the page, when clicking on the Add Address button it will do nothing as we haven’t trigger the modal yet.

To achieve this update the createAddress() and dismiss() methods in shipping-addresses page.

createAddress() {
          this.showForm = true;
          this.is_edit = false;
        },
        dismiss() {
          this.showForm = false;
        }

As you see the process is simple i set the showForm=true and is_edit=false, when dismissing i set showForm back to false. By doing this you will see the modal now.

Let’s update the modal methods to make the actual save or update. Update saveAddress() , resetForm() and mounted() hook in <shipping-address-form /> component.

saveAddress() {
            this.success_message = "";
            this.error_message = "";
            this.validation_errors = [];

            const data = {
              address: this.address,
              mobile: this.mobile,
              country: this.country,
              city: this.city,
              postal_code: this.postal_code
            }

            if(this.is_primary) {
              data.is_primary = 1;
            }

            let api;

            if(!this.is_edit) {
              api = ShippingAddressApi.store(this.$axios, data);
            } else {
              api = ShippingAddressApi.update(this.$axios, data, this.editShippingAddress.id);
            }

            api.then(response => {
              if(response.success) {
                this.success_message = response.message;
                this.$emit('submitForm');

                setTimeout(() => {
                  this.resetForm();
                  this.dismiss();
                }, 1000);
              }
            }).catch(err => {
                this.error_message = err.response.data.message;
              if(err.response.data.errors) {
                let errors = [];
                for(let key in err.response.data.errors) {
                  errors.push(err.response.data.errors[key][0]);
                }

                this.validation_errors = errors;
              }
            });
          },
          resetForm() {
            this.address = "";
            this.mobile = "";
            this.country = "";
            this.city = "";
            this.postal_code = "";
            this.is_primary = false;

            this.success_message = "";
            this.error_message = "";
            this.validation_errors = [];
          }
        }
mounted() {
          this.resetForm();

          if(this.is_edit) {
            this.address = this.editShippingAddress.address;
            this.mobile = this.editShippingAddress.mobile;
            this.country = this.editShippingAddress.country;
            this.city = this.editShippingAddress.city;
            this.postal_code = this.editShippingAddress.postal_code;
            this.is_primary = this.editShippingAddress.is_primary ? true : false;
          }
        }

The saveAddress() do save or update a shipping address, at first we empty the success and error messages  then i prepare the data that will be sent to the store or update apis this is identified by the is_edit prop.

On successful saving we reset the form by calling resetForm() method which clear the form fields and close the modal by calling the dismiss() method. It’s important when making save or update to emit an event “submitForm” which refresh the list of shipping addresses.

On case of an error we display the errors by catching them on .catch() listener of the axios callback.

The mounted() hook reset the form and check if this is an edit thereby setting the form data from the editable shipping address details.

 

Now let’s complete the buzzle by updating the remaining methods code in shipping-addresses page, update refresh(), showAddress(), and deleteAddress() methods:

refresh() {
          this.fetchAddresses();
        },
        showAddress(id) {
          this.is_edit = true;
          ShippingAddressApi.show(this.$axios, id).then(response => {
              this.edit_shipping_address = response.shipping_address;
              this.showForm = true;
          });
        },
        deleteAddress(id) {
          if(confirm("Are your sure?")) {
            ShippingAddressApi.delete(this.$axios, id).then(response => {
              if (response.success) {
                this.fetchAddresses();
              }
            });
          }
        }

The showAddress() set is_edit=true, then we call the show() api retreive the address details then we make showForm=true to show the modal. The deleteAddress() method delete a shipping address and refetch the addresses again.

At this point we have a working shipping addresses module.

 

The Full Code

pages/shipping-addresses.vue

<template>
  <div class="container">
    <div class="bg">
      <div class="row">
        <div class="col-sm-12">
          <h2 class="title text-center">Shipping Addresses</h2>
        </div>
      </div>

      <div class="row">

        <div class="col-md-3">
          <account-sidebar></account-sidebar>
        </div>

        <div class="col-sm-9 col-md-9">
          <button type="button" class="btn btn-primary pull-right" v-on:click.prevent="createAddress()">Add Address</button>
          <div class="well well-sm">
            <div class="row">
              <div class="col-sm-12" style="margin-top: 5px">
                  <p class="alert alert-info" v-if="!this.shipping_addresses.length">No shipping addresses! <a href="#" v-on:click.prevent="createAddress()">Add Address</a> </p>
                  <table class="table table-condensed" v-if="this.shipping_addresses.length > 0">
                    <thead>
                        <tr>
                          <th>Address</th>
                          <th>Country</th>
                          <th>City</th>
                          <th>Postal Code</th>
                          <th class="text-center">Actions</th>
                        </tr>
                    </thead>
                    <tbody>
                       <tr v-for="address in this.shipping_addresses" :key="address.id">
                         <td>
                           <address>
                             <strong v-if="address.is_primary == 1">{{address.address}} (Primary)</strong>
                             <span v-if="address.is_primary == 0">{{address.address}}</span>
                             <span style="display: block">{{address.mobile}}</span>
                           </address>
                         </td>
                         <td>{{ address.country }}</td>
                         <td>{{ address.city }}</td>
                         <td>{{ address.postal_code }}</td>
                         <td class="text-center">
                           <a href="#" @click.prevent="showAddress(address.id)" class="btn btn-info btn-sm"><i class="fa fa-edit"></i></a>
                           <a href="#" @click.prevent="deleteAddress(address.id)" class="btn btn-danger btn-sm"><i class="fa fa-trash-o"></i></a>
                         </td>
                       </tr>
                    </tbody>
                  </table>

                  <shipping-address-form v-if="this.showForm" v-on:dismissModal="dismiss()" v-on:submitForm="refresh()" :edit-shipping-address="edit_shipping_address" :is_edit="is_edit"></shipping-address-form>
              </div>
            </div>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
    import AccountSidebar from "../components/account-components/AccountSidebar";
    import {ShippingAddressApi} from "../api/shipping-address";
    import ShippingAddressForm from "../components/shipping-address-components/Form";

    export default {
      name: "ShippingAddresses",
      components: {ShippingAddressForm, AccountSidebar},
      middleware: "auth",
      head() {
        return {
          title: 'Online Shop | My Shipping Addresses',
          meta: [
            {
              hid: 'description',
              name: 'description',
              content: 'My Shipping Addresses Page'
            }
          ]
        }
      },
      data() {
        return {
          shipping_addresses: [],
          showForm: false,
          is_edit: false,
          edit_shipping_address: {}
        }
      },
      mounted() {
        this.fetchAddresses();
      },
      methods: {
        fetchAddresses() {
          ShippingAddressApi.list(this.$axios).then(response => {
            this.shipping_addresses = response.shipping_addresses;
          });
        },
        createAddress() {
          this.showForm = true;
          this.is_edit = false;
        },
        dismiss() {
          this.showForm = false;
        },
        refresh() {
          this.fetchAddresses();
        },
        showAddress(id) {
          this.is_edit = true;
          ShippingAddressApi.show(this.$axios, id).then(response => {
              this.edit_shipping_address = response.shipping_address;
              this.showForm = true;
          });
        },
        deleteAddress(id) {
          if(confirm("Are your sure?")) {
            ShippingAddressApi.delete(this.$axios, id).then(response => {
              if (response.success) {
                this.fetchAddresses();
              }
            });
          }
        }
      }
    }
</script>

components/shipping-address-components/Form.vue

<template>
  <div class="modal" tabindex="-1" role="dialog">
    <div class="modal-dialog" role="document">
      <div class="modal-content">
        <div class="modal-header">
          <button type="button" class="close" aria-label="Close" @click="dismiss()"><span aria-hidden="true">&times;</span></button>
          <h4 class="modal-title">{{ this.is_edit ? "Edit" : "Add" }} Shipping Address</h4>
        </div>
        <div class="modal-body">
          <div class="alert alert-success" v-if="this.success_message != ''">{{ this.success_message }}</div>
          <div class="alert alert-danger" v-if="this.error_message != ''">{{ this.error_message }}</div>

            <ul class="sufee-alert alert with-close alert-danger alert-dismissible" v-if="this.validation_errors.length">
              <li v-for="(error, index) in this.validation_errors" :key="index">{{ error }}</li>
            </ul>

          <form class="form-horizontal">
            <div class="form-group">
              <label>Country</label>
              <Countries :country="country" @countryUpdated="setCountry"></Countries>
            </div>

            <div class="form-group">
              <label>City</label>
              <input type="text" placeholder="City" name="city" class="form-control" v-model="city" />
            </div>

            <div class="form-group">
              <label>Address</label>
              <input type="text" placeholder="Address" name="address" class="form-control" v-model="address" />
            </div>

            <div class="form-group">
              <label>Postal Code</label>
              <input type="text" placeholder="Postal Code" name="postal_code" class="form-control" v-model="postal_code" />
            </div>

            <div class="form-group">
              <label>Mobile</label>
              <input type="text" placeholder="Mobile" name="mobile" class="form-control" v-model="mobile" />
            </div>

            <div class="form-group">
              <label>Is Primary</label>
              <input type="checkbox" name="is_primary" v-model="is_primary" />
            </div>

          </form>
        </div>
        <div class="modal-footer">
          <button type="button" class="btn btn-default" @click="dismiss()">Close</button>
          <button type="button" class="btn btn-primary" @click="saveAddress()">Save</button>
        </div>
      </div><!-- /.modal-content -->
    </div><!-- /.modal-dialog -->
  </div>
</template>

<script>
    import {ShippingAddressApi} from "../../api/shipping-address";
    import Countries from "./Countries";

    export default {
        name: "ShippingAddressForm",
      components: {Countries},
      props: ["is_edit", "editShippingAddress"],
        data() {
          return {
            address: "",
            country: "",
            city: "",
            postal_code: "",
            mobile: "",
            is_primary: false,
            success_message: true,
            error_message: false,
            validation_errors: []
          }
        },
        methods: {
          dismiss() {
            this.$emit('dismissModal');
          },
          setCountry(event) {
            this.country = event;
          },
          saveAddress() {
            this.success_message = "";
            this.error_message = "";
            this.validation_errors = [];

            const data = {
              address: this.address,
              mobile: this.mobile,
              country: this.country,
              city: this.city,
              postal_code: this.postal_code
            }

            if(this.is_primary) {
              data.is_primary = 1;
            }

            let api;

            if(!this.is_edit) {
              api = ShippingAddressApi.store(this.$axios, data);
            } else {
              api = ShippingAddressApi.update(this.$axios, data, this.editShippingAddress.id);
            }

            api.then(response => {
              if(response.success) {
                this.success_message = response.message;
                this.$emit('submitForm');

                setTimeout(() => {
                  this.resetForm();
                  this.dismiss();
                }, 1000);
              }
            }).catch(err => {
                this.error_message = err.response.data.message;
              if(err.response.data.errors) {
                let errors = [];
                for(let key in err.response.data.errors) {
                  errors.push(err.response.data.errors[key][0]);
                }

                this.validation_errors = errors;
              }
            });
          },
          resetForm() {
            this.address = "";
            this.mobile = "";
            this.country = "";
            this.city = "";
            this.postal_code = "";
            this.is_primary = false;

            this.success_message = "";
            this.error_message = "";
            this.validation_errors = [];
          }
        },
        mounted() {
          this.resetForm();

          if(this.is_edit) {
            this.address = this.editShippingAddress.address;
            this.mobile = this.editShippingAddress.mobile;
            this.country = this.editShippingAddress.country;
            this.city = this.editShippingAddress.city;
            this.postal_code = this.editShippingAddress.postal_code;
            this.is_primary = this.editShippingAddress.is_primary ? true : false;
          }
        }
    }
</script>

<style scoped>
  div.modal {
    display: inline-block !important;
  }
</style>

 

Continue to Part19: Checkout

 

0 0 vote
Article Rating
Share this: