Skip to content

Commit

Permalink
Support multiple stores on one instance
Browse files Browse the repository at this point in the history
  • Loading branch information
MrNaif2018 committed Jan 23, 2021
1 parent 998c53b commit 0425322
Show file tree
Hide file tree
Showing 9 changed files with 59 additions and 8 deletions.
16 changes: 14 additions & 2 deletions components/Header.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,14 @@
aria-label="main navigation")
.container.is-flex-touch
.navbar-brand
nuxt-link.navbar-item(exact, :to="{name: 'index'}")
nuxt-link.navbar-item(exact, :to="getHomeURL")
strong
i {{$store.state.store.name}}
.navbar-end.is-flex-touch
.navbar-item
.field
p.control
nuxt-link.button.is-light(exact, :to="{name: 'cart'}")
nuxt-link.button.is-light(exact, :to="getCartURL")
span.icon.cartitem
.cartcount(v-if="total > 0") {{ total }}
i.fa.fa-shopping-cart
Expand All @@ -36,6 +36,18 @@ export default {
isIndexRoute() {
return this.$route.name === "index"
},
getHomeURL() {
const storeID = this.$route.params.id
return storeID
? { name: "store-id", params: { id: storeID } }
: { name: "index" }
},
getCartURL() {
const storeID = this.$route.params.id
return storeID
? { name: "store-id-cart", params: { id: storeID } }
: { name: "cart" }
},
},
}
</script>
Expand Down
13 changes: 11 additions & 2 deletions components/ProductListItem.vue
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
<template lang="pug">
.card.is-radius
.card-image
nuxt-link(exact, :to="{name: 'products-slug', params: { slug: `${slug}` } }")
nuxt-link(exact, :to="productRedirectURL")
picture.image.image-preview
img.lazyload(:data-srcset="`${productURL(item.image)}`",
:alt="`Image of ${item.name}`")
.card-content
.media
.media-content
nuxt-link(exact, :to="{name: 'products-slug', params: { slug: `${slug}` } }")
nuxt-link(exact, :to="productRedirectURL")
p.title.is-5 {{ item.name }}
p.item-price {{ decimalStr(item.price) }} {{ currency }}
.media-right
Expand Down Expand Up @@ -42,6 +42,15 @@ export default {
currency() {
return this.$store.state.store.default_currency
},
productRedirectURL() {
const storeID = this.$route.params.id
return storeID
? {
name: "store-id-products-slug",
params: { id: storeID, slug: `${this.slug}` },
}
: { name: "products-slug", params: { slug: `${this.slug}` } }
},
},
methods: {
...mapActions(["addItem"]),
Expand Down
3 changes: 3 additions & 0 deletions middleware/storeRedirect.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
export default function ({ store, params }) {
if (params.id) store.commit("storeID", params.id)
}
10 changes: 8 additions & 2 deletions pages/cart.vue
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@

.empty.has-text-centered(v-else-if="!total && !success")
h3 Your cart is empty.
nuxt-link(exact to="/")
nuxt-link(exact :to="getHomeURL")
button.button Fill er up!

.has-text-centered(v-else)
h2 Success!
p Your order has been processed, it will be delivered shortly.
nuxt-link(exact to="/")
nuxt-link(exact :to="getHomeURL")
button.button Fill again your cart
</template>

Expand Down Expand Up @@ -64,6 +64,12 @@ export default {
currency() {
return this.$store.state.store.default_currency
},
getHomeURL() {
const storeID = this.$route.params.id
return storeID
? { name: "store-id", params: { id: storeID } }
: { name: "index" }
},
},
beforeDestroy() {
this.success && this.setSuccess(false)
Expand Down
2 changes: 1 addition & 1 deletion pages/products/_slug.vue
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ export default {
return error({ statusCode: 404, text: "Product not found" })
}
return app.$axios
.get(`/products/${productId}`)
.get(`/products/${productId}?store=${store.state.storeID}`)
.then((r) => store.commit("product/SET_PRODUCTS", [r.data]))
.catch((e) => {
error({ statusCode: 404, text: "Product not found" })
Expand Down
7 changes: 7 additions & 0 deletions pages/store/_id/cart.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
import Cart from "@/pages/cart.vue"
export default {
...Cart,
middleware: "storeRedirect",
}
</script>
7 changes: 7 additions & 0 deletions pages/store/_id/index.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
import Index from "@/pages/index.vue"
export default {
...Index,
middleware: "storeRedirect",
}
</script>
7 changes: 7 additions & 0 deletions pages/store/_id/products/_slug.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
<script>
import Product from "@/pages/products/_slug.vue"
export default {
...Product,
middleware: "storeRedirect",
}
</script>
2 changes: 1 addition & 1 deletion version.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
const VERSION = "0.2.0.2"
const VERSION = "0.2.1.0"

export default VERSION

0 comments on commit 0425322

Please sign in to comment.