index.html 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8" />
  5. <title>Vue App</title>
  6. <script src="https://cdn.jsdelivr.net/npm/vue/dist/vue.js"></script>
  7. <script src="../devkit/ui-devkit.js"></script>
  8. <link rel="stylesheet" href="https://unpkg.com/@clr/ui/clr-ui.min.css" />
  9. </head>
  10. <body>
  11. <div id="app" style="padding: 1rem;">
  12. <div>
  13. <h1>Hello from Vue.js!</h1>
  14. <button v-on:click="getProducts" class="btn btn-primary">Get products</button>
  15. <h3>Products</h3>
  16. <ol>
  17. <li v-for="product in products">
  18. {{ product.name }}
  19. <span class="label label-success" v-if="product.enabled">Enabled</span>
  20. <span class="label label-danger" v-else>Disabled</span>
  21. <button
  22. v-on:click="toggleEnabled(product.id, !product.enabled)"
  23. class="btn btn-sm btn-secondary"
  24. >
  25. toggle
  26. </button>
  27. </li>
  28. </ol>
  29. </div>
  30. </div>
  31. <script>
  32. var app = new Vue({
  33. el: '#app',
  34. data: {
  35. message: 'Hello Vue!',
  36. products: [],
  37. },
  38. methods: {
  39. getProducts: function() {
  40. const query = `
  41. query GetProducts($skip: Int, $take: Int) {
  42. products(options: { skip: $skip, take: $take }) {
  43. items { id, name, enabled },
  44. totalItems
  45. }
  46. }`;
  47. this.sub = VendureUiDevkit.graphQlQuery(query, {
  48. skip: 0,
  49. take: 10,
  50. }).stream.subscribe(
  51. val => {
  52. this.products = val.products.items;
  53. },
  54. err => console.error(err),
  55. () => console.log('completed products stream'),
  56. );
  57. },
  58. toggleEnabled: function(id, enabled) {
  59. const mutation = `
  60. mutation ToggleEnabled($id: ID!, $enabled: Boolean!) {
  61. updateProduct(input: { id: $id, enabled: $enabled }) {
  62. id
  63. enabled
  64. }
  65. }
  66. `;
  67. VendureUiDevkit.graphQlMutation(mutation, { id, enabled }).then(val => {
  68. VendureUiDevkit.notify({
  69. message: 'Updated Product',
  70. });
  71. });
  72. },
  73. },
  74. beforeDestroy: function() {
  75. console.log('destroying!');
  76. this.sub.unsubscribe();
  77. },
  78. });
  79. </script>
  80. </body>
  81. </html>