summaryrefslogtreecommitdiff
path: root/iridescence/src
diff options
context:
space:
mode:
authorAdam T. Carpenter <atc@53hor.net>2020-11-07 20:31:03 -0500
committerAdam T. Carpenter <atc@53hor.net>2020-11-07 20:31:03 -0500
commit7dd36c0e699a1154c7163f25bf488fbd63edeafe (patch)
tree1a0d76539acba7b77f1abe9fb443ac98523ed7e5 /iridescence/src
parent2c408648c70614a88c0412faf6d3a00d147379a2 (diff)
downloadtheglassyladies-7dd36c0e699a1154c7163f25bf488fbd63edeafe.tar.xz
theglassyladies-7dd36c0e699a1154c7163f25bf488fbd63edeafe.zip
updated photo sets to not return original, added models to front,
implemented front-to-back requests
Diffstat (limited to 'iridescence/src')
-rw-r--r--iridescence/src/api/dichroism.js122
-rw-r--r--iridescence/src/api/error.js6
-rw-r--r--iridescence/src/models/photo_set.js10
-rw-r--r--iridescence/src/models/product.js16
-rw-r--r--iridescence/src/store/index.js6
5 files changed, 77 insertions, 83 deletions
diff --git a/iridescence/src/api/dichroism.js b/iridescence/src/api/dichroism.js
index 7821eb3..e3b08a8 100644
--- a/iridescence/src/api/dichroism.js
+++ b/iridescence/src/api/dichroism.js
@@ -1,93 +1,55 @@
+import Product from "../models/product";
+import PhotoSet from "../models/photo_set";
+import ApiError from "./error";
+
export default class Dichroism {
- constructor() {
- this.products = [
- {
- id: 1,
- name: "Beach Box",
- quantity: 0,
- cents: 1100,
- imgPath: "/beach_box.jpg",
- description: "This is a beach box.",
- featured: false,
- categories: ["Fused Glass", "Beachy"]
- },
- {
- id: 2,
- name: "Wind Chime",
- quantity: 0,
- cents: 4500,
- imgPath: "/wind-chime.jpg",
- description: "Makes noise when the wind blows.",
- featured: false,
- categories: ["Fused Glass", "Beachy"]
- },
- {
- id: 3,
- name: "Beach Box",
- quantity: 5,
- cents: 1100,
- imgPath: "/beach_box.jpg",
- description: "This is a beach box.",
- featured: false,
- categories: ["Stained Glass", "Christmas"]
- },
- {
- id: 4,
- name: "Wind Chime",
- quantity: 2,
- cents: 4500,
- imgPath: "/wind-chime.jpg",
- description: "Makes noise when the wind blows.",
- featured: false,
- categories: ["Fused Glass", "Kiln-y"]
- },
- {
- id: 5,
- name: "Beach Box (New!)",
- quantity: 5,
- cents: 1100,
- imgPath: "/beach_box.jpg",
- description: "This is a beach box.",
- featured: true,
- categories: ["Stained Glass", "Christmas"]
- }
- ];
+ _base_addr = "http://localhost:8000/";
+
+ async createPhoto(file) {
+ const fd = new FormData();
+ fd.append(file.name, file);
+
+ const options = {
+ method: "POST",
+ body: fd
+ };
+
+ const photos = await self._sendRequest("photos", options);
+ return photos.map(p => new PhotoSet(p));
}
+ async getProducts() {
+ const products = await self._sendRequest("products", null);
+ return products.map(p => new Product(p));
+ }
- async uploadPhoto(file) {
- const fd = new FormData();
- fd.append(file.name, file);
+ async updateProduct(fieldDiff) {
+ const options = {
+ method: "PATCH",
+ body: fieldDiff
+ };
- return fetch("http://localhost:8000/photos", {
- method: "POST",
- body: fd
- });
- }
+ const product = await self._sendRequest("products", options);
+ return new Product(product);
+ }
+ async createProduct(newProduct) {
+ const options = {
+ method: "POST",
+ body: newProduct
+ };
- getProducts() {
- return this.products.slice(0);
+ const product = await self._sendRequest("products", options);
+ return new Product(product);
}
- updateProduct(newProduct) {
- if (!newProduct) {
- return;
- }
+ async _sendRequest(endpoint, options) {
+ const response = await fetch(self.base_addr + endpoint, options);
- for (let i = 0; i < this.products.length; i++) {
- if (newProduct.id == this.products[i].id) {
- let currentProduct = this.products[i];
- currentProduct.id = newProduct.id;
- currentProduct.name = newProduct.name;
- currentProduct.quantity = newProduct.quantity;
- currentProduct.cents = newProduct.cents;
- currentProduct.imgPath = newProduct.imgPath;
- currentProduct.description = newProduct.description;
- currentProduct.featured = newProduct.featured;
- currentProduct.categories = newProduct.categories.slice(0);
- return;
- }
+ if (response.ok) {
+ return await response.json();
+ } else {
+ return new ApiError(await response.text());
}
}
}
diff --git a/iridescence/src/api/error.js b/iridescence/src/api/error.js
new file mode 100644
index 0000000..7c9320d
--- /dev/null
+++ b/iridescence/src/api/error.js
@@ -0,0 +1,6 @@
+export default class ApiError extends Error {
+ constructor(message) {
+ super(message);
+ this.name = "ApiError";
+ }
+}
diff --git a/iridescence/src/models/photo_set.js b/iridescence/src/models/photo_set.js
new file mode 100644
index 0000000..7d7213c
--- /dev/null
+++ b/iridescence/src/models/photo_set.js
@@ -0,0 +1,10 @@
+export default class PhotoSet {
+ id = 0;
+ fullsize = "";
+ thumbnail = "";
+ base = "";
+
+ constructor(json) {
+ Object.assign(this, json);
+ }
+}
diff --git a/iridescence/src/models/product.js b/iridescence/src/models/product.js
new file mode 100644
index 0000000..c826073
--- /dev/null
+++ b/iridescence/src/models/product.js
@@ -0,0 +1,16 @@
+export default class Product {
+ id = 0;
+ name = "";
+ description = "";
+ cents = 0;
+ quantity = 0;
+ featured = false;
+ photo_base = "";
+ photo_fullsize = "";
+ photo_thumbnail = "";
+ category = "";
+
+ constructor(json) {
+ Object.assign(this, json);
+ }
+}
diff --git a/iridescence/src/store/index.js b/iridescence/src/store/index.js
index 1a83c01..2b24816 100644
--- a/iridescence/src/store/index.js
+++ b/iridescence/src/store/index.js
@@ -4,7 +4,7 @@ import Dichroism from "@/api/dichroism.js";
Vue.use(Vuex);
-let dichroismApi = new Dichroism();
+let dichroism = new Dichroism();
export default new Vuex.Store({
state: {
@@ -34,10 +34,10 @@ export default new Vuex.Store({
},
actions: {
refreshProducts(context) {
- context.commit("setProducts", dichroismApi.getProducts());
+ context.commit("setProducts", dichroism.getProducts());
},
updateProduct(context, product) {
- dichroismApi.updateProduct(product);
+ dichroism.updateProduct(product);
context.dispatch("refreshProducts");
}
},