summaryrefslogtreecommitdiffstats
path: root/sick-fits/frontend/lib
diff options
context:
space:
mode:
authorWes Bos <wesbos@gmail.com>2018-06-14 16:44:21 -0400
committerWes Bos <wesbos@gmail.com>2018-06-14 16:44:21 -0400
commitb1600adec47a04f60e1da07d94a3ed3906ff5aee (patch)
tree828f786da6806d7237ee5cbc5df61e552353b85b /sick-fits/frontend/lib
parenta95e08cd2dd5d7ec0a0412adae02515a5f9cec4f (diff)
starter files
Diffstat (limited to 'sick-fits/frontend/lib')
-rw-r--r--sick-fits/frontend/lib/calcTotalPrice.js6
-rw-r--r--sick-fits/frontend/lib/formatMoney.js11
-rw-r--r--sick-fits/frontend/lib/testUtils.js79
-rw-r--r--sick-fits/frontend/lib/withData.js19
4 files changed, 115 insertions, 0 deletions
diff --git a/sick-fits/frontend/lib/calcTotalPrice.js b/sick-fits/frontend/lib/calcTotalPrice.js
new file mode 100644
index 0000000..4822468
--- /dev/null
+++ b/sick-fits/frontend/lib/calcTotalPrice.js
@@ -0,0 +1,6 @@
+export default function calcTotalPrice(cart) {
+ return cart.reduce((tally, cartItem) => {
+ if (!cartItem.item) return tally;
+ return tally + cartItem.quantity * cartItem.item.price;
+ }, 0);
+}
diff --git a/sick-fits/frontend/lib/formatMoney.js b/sick-fits/frontend/lib/formatMoney.js
new file mode 100644
index 0000000..c001aee
--- /dev/null
+++ b/sick-fits/frontend/lib/formatMoney.js
@@ -0,0 +1,11 @@
+export default function(amount) {
+ const options = {
+ style: 'currency',
+ currency: 'USD',
+ minimumFractionDigits: 2,
+ };
+ // if its a whole, dollar amount, leave off the .00
+ if (amount % 100 === 0) options.minimumFractionDigits = 0;
+ const formatter = new Intl.NumberFormat('en-US', options);
+ return formatter.format(amount / 100);
+}
diff --git a/sick-fits/frontend/lib/testUtils.js b/sick-fits/frontend/lib/testUtils.js
new file mode 100644
index 0000000..42739e9
--- /dev/null
+++ b/sick-fits/frontend/lib/testUtils.js
@@ -0,0 +1,79 @@
+import casual from 'casual';
+
+// seed it so we get consistent results
+casual.seed(777);
+
+const fakeItem = () => ({
+ __typename: 'Item',
+ id: '123',
+ price: 5000,
+ user: null,
+ image: 'dog-small.jpg',
+ title: 'dogs are best',
+ description: 'dogs',
+ largeImage: 'dog.jpg',
+});
+
+const fakeUser = () => ({
+ __typename: 'User',
+ id: '4234',
+ name: casual.name,
+ email: casual.email,
+ permissions: ['ADMIN'],
+ orders: [],
+ cart: [],
+});
+
+const fakeOrderItem = () => ({
+ __typename: 'OrderItem',
+ id: casual.uuid,
+ image: `${casual.word}.jpg`,
+ title: casual.words(),
+ price: 4234,
+ quantity: 1,
+ description: casual.words(),
+});
+
+const fakeOrder = () => ({
+ __typename: 'Order',
+ id: 'ord123',
+ charge: 'ch_123',
+ total: 40000,
+ items: [fakeOrderItem(), fakeOrderItem()],
+ createdAt: '2018-04 - 06T19: 24: 16.000Z',
+ user: fakeUser(),
+});
+
+const fakeCartItem = overrides => ({
+ __typename: 'CartItem',
+ id: 'omg123',
+ quantity: 3,
+ item: fakeItem(),
+ user: fakeUser(),
+ ...overrides,
+});
+
+// Fake LocalStorage
+class LocalStorageMock {
+ constructor() {
+ this.store = {};
+ }
+
+ clear() {
+ this.store = {};
+ }
+
+ getItem(key) {
+ return this.store[key] || null;
+ }
+
+ setItem(key, value) {
+ this.store[key] = value.toString();
+ }
+
+ removeItem(key) {
+ delete this.store[key];
+ }
+}
+
+export { LocalStorageMock, fakeItem, fakeUser, fakeCartItem, fakeOrder, fakeOrderItem };
diff --git a/sick-fits/frontend/lib/withData.js b/sick-fits/frontend/lib/withData.js
new file mode 100644
index 0000000..755f4c1
--- /dev/null
+++ b/sick-fits/frontend/lib/withData.js
@@ -0,0 +1,19 @@
+import withApollo from 'next-with-apollo';
+import ApolloClient from 'apollo-boost';
+import { endpoint } from '../config';
+
+function createClient({ headers }) {
+ return new ApolloClient({
+ uri: process.env.NODE_ENV === 'development' ? endpoint : endpoint,
+ request: operation => {
+ operation.setContext({
+ fetchOptions: {
+ credentials: 'include',
+ },
+ headers,
+ });
+ },
+ });
+}
+
+export default withApollo(createClient);