Skip to content

Commit

Permalink
Refactor (#14)
Browse files Browse the repository at this point in the history
  • Loading branch information
Roene-JustBetter authored Feb 27, 2024
1 parent 237195c commit a7652e0
Show file tree
Hide file tree
Showing 57 changed files with 853 additions and 607 deletions.
14 changes: 5 additions & 9 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,11 @@
composer require rapidez/multiple-wishlists
```

If you haven't published the Rapidez views yet, publish them with:
It's not recommended to publish every view, rather you should overwrite only the files necessary. However, you can still publish all of the views with the following command:
```
php artisan vendor:publish --provider="Rapidez\MultipleWishlist\MultipleWishlistServiceProvider" --tag=views
```

Then add the js dependency at the top of your app.js:
```
require('Vendor/rapidez/multiple-wishlists/resources/js/app.js');
```

You also should probably add a new "wishlists" button to the Rapidez account menu, if you use it in your project (which is in `rapidez/account/resources/views/partials/menu.blade.php`)

## API endpoints
Expand All @@ -23,12 +18,13 @@ The API uses mostly Laravel apiResource endpoints. All of the exposed endpoints
| Endpoint | Parameters | Description |
| --- | --- | --- |
| GET /wishlists/ | None | Gets a list of all the customer's wishlists |
| GET /wishlists/all | None | Gets a list of all the customer's wishlists + all of the items |
| GET /wishlists/{id} | None | Gets a specific wishlist |
| POST /wishlists/ | <ul><li>title</li></ul> | Creates a new wishlist with the given title |
| PATCH /wishlists/{id} | <ul><li>title(str, max 255)</li><li>description(str, max 65535)</li><li>share(bool)</li></ul> | Updates the data of a wishlist |
| DELETE /wishlists/{id} | None | Deletes a wishlist |
| GET /wishlists/shared/{token} | None | Gets a shared wishlist |

| Endpoint | Parameters | Description |
| --- | --- | --- |
| POST /wishlists/item | <ul><li>wishlist_id(int)</li><li>product_id(int)</li><li>qty(int)</li></ul> | Adds a new item to the given wishlist |
| PATCH /wishlists/item/{id} | <ul><li>description(str, max 255)</li><li>qty(int)</li></ul> | Updates the data of an item |
| DELETE /wishlists/item/{id} | None | Deletes an item |
| GET /wishlists/shared/{token} | None | Gets a shared wishlist |
274 changes: 113 additions & 161 deletions resources/js/Wishlist.vue
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>
Loading

0 comments on commit a7652e0

Please sign in to comment.