-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
237195c
commit a7652e0
Showing
57 changed files
with
853 additions
and
607 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,218 +1,170 @@ | ||
<script> | ||
import { useShare } from '@vueuse/core' | ||
import { wishlists, create, remove, update, addItem, removeItem } from './stores/useWishlists' | ||
import { refresh as refreshCart } from 'Vendor/rapidez/core/resources/js/stores/useCart' | ||
import { mask as cartMask } from 'Vendor/rapidez/core/resources/js/stores/useMask' | ||
import wishlistItem from './WishlistItem.vue' | ||
Vue.component('wishlist-item', wishlistItem) | ||
export default { | ||
props: { | ||
wishlistId: { | ||
type: Number, | ||
default: 0 | ||
}, | ||
sharedId: String | ||
sharedId: String, | ||
}, | ||
data() { | ||
return { | ||
wishlists: [], | ||
wishlist: null | ||
mounted() { | ||
if(this.sharedId) { | ||
this.fetchShared() | ||
} | ||
}, | ||
watch: { | ||
wishlists: { | ||
handler: function() { | ||
localStorage.wishlists = JSON.stringify(this.wishlists); | ||
}, deep: true | ||
} | ||
const { share, isSupported } = useShare() | ||
this.shareFn = share | ||
this.isSupported = isSupported | ||
}, | ||
mounted() { | ||
this.$root.$on('wishlists-loaded', this.fetchFromLocalStorage); | ||
if(this.sharedId) { | ||
this.fetchShared(); | ||
} else { | ||
this.fetchWishlists(); | ||
data() { | ||
return { | ||
adding: false, | ||
added: false, | ||
editing: null, | ||
sharedWishlist: null, | ||
shareFn: null, | ||
isSupported: null, | ||
} | ||
}, | ||
methods: { | ||
async fetchWishlists() { | ||
if (!this.$root.user) { | ||
return; | ||
} | ||
if(this.$root.wishlistsLoading) { | ||
return; | ||
} | ||
if (!localStorage.wishlists) { | ||
this.$root.wishlistsLoading = true; | ||
await this.$root.apiRequest('get', '/api/wishlists/all', {}, function(response) { | ||
if(response.status < 200 || response.status >= 300) { | ||
localStorage.wishlists = '[]'; | ||
} else { | ||
localStorage.wishlists = JSON.stringify(response.data); | ||
} | ||
}, null) | ||
this.$root.wishlistsLoading = false; | ||
this.$root.$emit('wishlists-loaded'); | ||
} | ||
this.fetchFromLocalStorage(); | ||
isWishlisted(productId) { | ||
return this.wishlists.some(e => this.findItem(e, productId)) | ||
}, | ||
fetchFromLocalStorage() { | ||
this.wishlists = JSON.parse(localStorage.wishlists); | ||
findItem(wishlist, productId) { | ||
return wishlist.items.find(e => e.product_id == productId) ?? null | ||
}, | ||
if(this.wishlistId) { | ||
this.wishlist = this.wishlists.find(e => e.id == this.wishlistId); | ||
toggleItem(wishlist, productId) { | ||
let item = this.findItem(wishlist, productId) | ||
if (item) { | ||
removeItem(wishlist.id, item.wishlist_item_id) | ||
} else { | ||
addItem(wishlist.id, productId) | ||
} | ||
}, | ||
async fetchShared() { | ||
this.wishlist = await this.$root.apiRequest('get', '/api/wishlists/shared/' + this.sharedId); | ||
let response = await axios({ | ||
method: 'GET', | ||
url: window.url('/api/wishlists/shared/' + this.sharedId), | ||
headers: { Store: window.config.store_code }, | ||
}) | ||
this.sharedWishlist = response.data | ||
}, | ||
getWishlist(id) { | ||
var wishlist = this.wishlists.find(e => e.id == id); | ||
if(!wishlist) return null; | ||
if(!wishlist.title) wishlist.title = ""; | ||
if(!wishlist.description) wishlist.description = ""; | ||
if(!wishlist.share) wishlist.share = false; | ||
return wishlist; | ||
return this.wishlists.find(e => e.id == id); | ||
}, | ||
findItem(wishlist, productId) { | ||
return wishlist.items.find(e => e.product_id == productId); | ||
addWishlist: create, | ||
async removeWishlist(id, redirect = false) { | ||
await remove(id) | ||
if (redirect) { | ||
Turbo.visit(window.url('/account/wishlists')) | ||
} | ||
}, | ||
update: update, | ||
async addAllToCart() { | ||
this.adding = true | ||
await Promise.allSettled( | ||
this.wishlist.items.map( | ||
(item) => this.addToCart(item) | ||
) | ||
) | ||
await refreshCart() | ||
this.added = true | ||
this.adding = false | ||
setTimeout(() => this.added = false, 3000) | ||
}, | ||
async toggleItem(wishlist, productId, qty = 1, description = '', redirect) { | ||
var item = this.findItem(wishlist, productId); | ||
if (item) { | ||
this.removeItem(wishlist, productId, redirect); | ||
} else { | ||
this.addItem(wishlist, productId, qty, description, redirect); | ||
} | ||
async addToCart(item) { | ||
await this.magentoCart('post', 'items', { | ||
cartItem: { | ||
sku: item.product.sku, | ||
quote_id: cartMask.value, | ||
qty: item.qty, | ||
} | ||
}).catch((error) => { | ||
Notify(error.response.data.message, 'error', error.response.data?.parameters) | ||
}) | ||
}, | ||
async checkMake(id, title, callback) { | ||
var wishlist = this.getWishlist(id); | ||
if(!wishlist) { | ||
wishlist = await this.addWishlist(title); | ||
toggleEdit() { | ||
if (!this.editing) { | ||
this.editing = { | ||
title: this.wishlist.title, | ||
description: this.wishlist.description, | ||
shared: this.wishlist.shared, | ||
} | ||
} else { | ||
this.editing = null | ||
} | ||
}, | ||
if(callback) { | ||
await callback(wishlist); | ||
save() { | ||
if (!this.editing || !this.wishlist) { | ||
return | ||
} | ||
return wishlist; | ||
update(this.wishlist.id, this.editing) | ||
this.toggleEdit() | ||
}, | ||
async addItem(wishlist, productId, qty = 1, description = '', redirect) { | ||
await this.$root.apiRequest('post', '/api/wishlists/item/', { | ||
wishlist_id: wishlist.id, | ||
product_id: productId, | ||
description: description, | ||
qty: qty | ||
}, function (response) { | ||
wishlist.items.push(response.data); | ||
}); | ||
if(redirect) { | ||
Turbo.visit(window.url(redirect)); | ||
share() { | ||
if (!this.isSupported) { | ||
return | ||
} | ||
}, | ||
async removeItem(wishlist, productId, redirect) { | ||
var wishlistItemId = this.findItem(wishlist, productId).wishlist_item_id; | ||
await this.$root.apiRequest('delete', '/api/wishlists/item/' + wishlistItemId, {}, function (response) { | ||
window.Vue.delete(wishlist.items, wishlist.items.findIndex(e => e.wishlist_item_id == wishlistItemId)); | ||
}); | ||
return this.shareFn(Object.fromEntries(Object.entries({ | ||
title: this.wishlist.title, | ||
text: this.wishlist.description, | ||
url: this.shareUrl, | ||
}).filter(([_, v]) => v))) | ||
} | ||
}, | ||
if(redirect) { | ||
Turbo.visit(window.url(redirect)); | ||
} | ||
computed: { | ||
wishlists() { | ||
return wishlists.value | ||
}, | ||
async editItem(wishlist, productId, variables, redirect) { | ||
var wishlistItemId = this.findItem(wishlist, productId).wishlist_item_id; | ||
await this.$root.apiRequest('patch', '/api/wishlists/item/' + wishlistItemId, variables, function (response) { | ||
var item = wishlist.items.find(e => e.wishlist_item_id == wishlistItemId); | ||
item.description = response.data.description; | ||
item.qty = response.data.qty; | ||
}) | ||
if(redirect) { | ||
Turbo.visit(window.url(redirect)); | ||
wishlist() { | ||
if (this.wishlistId) { | ||
return this.getWishlist(this.wishlistId) | ||
} else if (this.sharedWishlist) { | ||
return this.sharedWishlist | ||
} | ||
}, | ||
async addWishlist(title, redirect) { | ||
var wishlists = this.wishlists; | ||
var responseData = null; | ||
await this.$root.apiRequest('post', '/api/wishlists/', { | ||
title: title | ||
}, function (response) { | ||
response.data.items = []; | ||
wishlists.push(response.data); | ||
responseData = response.data; | ||
}); | ||
if(redirect) { | ||
Turbo.visit(window.url(redirect)); | ||
} else { | ||
return responseData; | ||
} | ||
return null | ||
}, | ||
async editWishlist(wishlist, title, description, shared, redirect) { | ||
await this.$root.apiRequest('patch', '/api/wishlists/' + wishlist.id, { | ||
title: title, | ||
description: description ?? '', | ||
shared: shared | ||
}, function (response) { | ||
wishlist.title = response.data.title; | ||
wishlist.description = response.data.description; | ||
wishlist.shared = response.data.shared; | ||
}); | ||
if(redirect) { | ||
Turbo.visit(window.url(redirect)); | ||
shareUrl() { | ||
if (!this.wishlist?.shared) { | ||
return null | ||
} | ||
}, | ||
async removeWishlist(wishlist, redirect) { | ||
var wishlists = this.wishlists; | ||
await this.$root.apiRequest('delete', '/api/wishlists/' + wishlist.id, {}, function (response) { | ||
window.Vue.delete(wishlists, wishlists.findIndex(e => e.id == wishlist.id)); | ||
}) | ||
if(redirect) { | ||
Turbo.visit(window.url(redirect)); | ||
} | ||
return window.url('/wishlists/shared/' + this.wishlist.sharing_token) | ||
}, | ||
}, | ||
render() { | ||
return this.$scopedSlots.default({ | ||
wishlists: this.wishlists, | ||
wishlist: this.wishlist, | ||
addItem: this.addItem, | ||
removeItem: this.removeItem, | ||
editItem: this.editItem, | ||
toggleItem: this.toggleItem, | ||
findItem: this.findItem, | ||
getWishlist: this.getWishlist, | ||
addWishlist: this.addWishlist, | ||
removeWishlist: this.removeWishlist, | ||
editWishlist: this.editWishlist, | ||
tempWishlist: JSON.parse(JSON.stringify(this.wishlist)), | ||
checkMake: this.checkMake | ||
}) | ||
return this.$scopedSlots.default(this) | ||
}, | ||
} | ||
</script> |
Oops, something went wrong.