aboutsummaryrefslogtreecommitdiff
path: root/src/actions
diff options
context:
space:
mode:
authorMichael Foiani <mfoiani2019@communityschoolnaples.org>2018-07-25 19:38:43 -0400
committerMichael Foiani <mfoiani2019@communityschoolnaples.org>2018-07-25 19:38:43 -0400
commit3c09a0a91488e182f521b0cd39017cb5bc781a83 (patch)
treebc26d401b97f13169f3becdfe03bba6fd0f34353 /src/actions
Initial commit. Added pwa starter kit to project for the application.
Diffstat (limited to 'src/actions')
-rw-r--r--src/actions/app.js93
-rw-r--r--src/actions/counter.js24
-rw-r--r--src/actions/shop.js79
3 files changed, 196 insertions, 0 deletions
diff --git a/src/actions/app.js b/src/actions/app.js
new file mode 100644
index 0000000..50d1529
--- /dev/null
+++ b/src/actions/app.js
@@ -0,0 +1,93 @@
+/**
+@license
+Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
+This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
+The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
+The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
+Code distributed by Google as part of the polymer project is also
+subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
+*/
+
+export const UPDATE_PAGE = 'UPDATE_PAGE';
+export const UPDATE_OFFLINE = 'UPDATE_OFFLINE';
+export const UPDATE_DRAWER_STATE = 'UPDATE_DRAWER_STATE';
+export const OPEN_SNACKBAR = 'OPEN_SNACKBAR';
+export const CLOSE_SNACKBAR = 'CLOSE_SNACKBAR';
+
+export const navigate = (path) => (dispatch) => {
+ // Extract the page name from path.
+ const page = path === '/' ? 'view1' : path.slice(1);
+
+ // Any other info you might want to extract from the path (like page type),
+ // you can do here
+ dispatch(loadPage(page));
+
+ // Close the drawer - in case the *path* change came from a link in the drawer.
+ dispatch(updateDrawerState(false));
+};
+
+const loadPage = (page) => (dispatch) => {
+ switch(page) {
+ case 'view1':
+ import('../components/my-view1.js').then((module) => {
+ // Put code in here that you want to run every time when
+ // navigating to view1 after my-view1.js is loaded.
+ });
+ break;
+ case 'view2':
+ import('../components/my-view2.js');
+ break;
+ case 'view3':
+ import('../components/my-view3.js');
+ break;
+ default:
+ page = 'view404';
+ import('../components/my-view404.js');
+ }
+
+ dispatch(updatePage(page));
+};
+
+const updatePage = (page) => {
+ return {
+ type: UPDATE_PAGE,
+ page
+ };
+};
+
+let snackbarTimer;
+
+export const showSnackbar = () => (dispatch) => {
+ dispatch({
+ type: OPEN_SNACKBAR
+ });
+ clearTimeout(snackbarTimer);
+ snackbarTimer = setTimeout(() =>
+ dispatch({ type: CLOSE_SNACKBAR }), 3000);
+};
+
+export const updateOffline = (offline) => (dispatch, getState) => {
+ // Show the snackbar, unless this is the first load of the page.
+ if (getState().app.offline !== undefined) {
+ dispatch(showSnackbar());
+ }
+ dispatch({
+ type: UPDATE_OFFLINE,
+ offline
+ });
+};
+
+export const updateLayout = (wide) => (dispatch, getState) => {
+ if (getState().app.drawerOpened) {
+ dispatch(updateDrawerState(false));
+ }
+};
+
+export const updateDrawerState = (opened) => (dispatch, getState) => {
+ if (getState().app.drawerOpened !== opened) {
+ dispatch({
+ type: UPDATE_DRAWER_STATE,
+ opened
+ });
+ }
+};
diff --git a/src/actions/counter.js b/src/actions/counter.js
new file mode 100644
index 0000000..4ca5254
--- /dev/null
+++ b/src/actions/counter.js
@@ -0,0 +1,24 @@
+/**
+@license
+Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
+This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
+The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
+The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
+Code distributed by Google as part of the polymer project is also
+subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
+*/
+
+export const INCREMENT = 'INCREMENT';
+export const DECREMENT = 'DECREMENT';
+
+export const increment = () => {
+ return {
+ type: INCREMENT
+ };
+};
+
+export const decrement = () => {
+ return {
+ type: DECREMENT
+ };
+};
diff --git a/src/actions/shop.js b/src/actions/shop.js
new file mode 100644
index 0000000..4542186
--- /dev/null
+++ b/src/actions/shop.js
@@ -0,0 +1,79 @@
+/**
+@license
+Copyright (c) 2018 The Polymer Project Authors. All rights reserved.
+This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
+The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
+The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
+Code distributed by Google as part of the polymer project is also
+subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
+*/
+
+export const GET_PRODUCTS = 'GET_PRODUCTS';
+export const ADD_TO_CART = 'ADD_TO_CART';
+export const REMOVE_FROM_CART = 'REMOVE_FROM_CART';
+export const CHECKOUT_SUCCESS = 'CHECKOUT_SUCCESS';
+export const CHECKOUT_FAILURE = 'CHECKOUT_FAILURE';
+
+const PRODUCT_LIST = [
+ {"id": 1, "title": "Cabot Creamery Extra Sharp Cheddar Cheese", "price": 10.99, "inventory": 2},
+ {"id": 2, "title": "Cowgirl Creamery Mt. Tam Cheese", "price": 29.99, "inventory": 10},
+ {"id": 3, "title": "Tillamook Medium Cheddar Cheese", "price": 8.99, "inventory": 5},
+ {"id": 4, "title": "Point Reyes Bay Blue Cheese", "price": 24.99, "inventory": 7},
+ {"id": 5, "title": "Shepherd's Halloumi Cheese", "price": 11.99, "inventory": 3}
+];
+
+export const getAllProducts = () => (dispatch, getState) => {
+ // Here you would normally get the data from the server. We're simulating
+ // that by dispatching an async action (that you would dispatch when you
+ // succesfully got the data back)
+
+ // You could reformat the data in the right format as well:
+ const products = PRODUCT_LIST.reduce((obj, product) => {
+ obj[product.id] = product
+ return obj
+ }, {});
+
+ dispatch({
+ type: GET_PRODUCTS,
+ products: products
+ });
+};
+
+export const checkout = (productId) => (dispatch) => {
+ // Here you could do things like credit card validation, etc.
+ // If that fails, dispatch CHECKOUT_FAILURE. We're simulating that
+ // by flipping a coin :)
+ const flip = Math.floor(Math.random() * 2);
+ if (flip === 0) {
+ dispatch({
+ type: CHECKOUT_FAILURE
+ });
+ } else {
+ dispatch({
+ type: CHECKOUT_SUCCESS
+ });
+ }
+};
+
+export const addToCart = (productId) => (dispatch, getState) =>{
+ const state = getState();
+ // Just because the UI thinks you can add this to the cart
+ // doesn't mean it's in the inventory (user could've fixed it);
+ if (state.shop.products[productId].inventory > 0) {
+ dispatch(addToCartUnsafe(productId));
+ }
+};
+
+export const removeFromCart = (productId) => {
+ return {
+ type: REMOVE_FROM_CART,
+ productId
+ };
+};
+
+export const addToCartUnsafe = (productId) => {
+ return {
+ type: ADD_TO_CART,
+ productId
+ };
+};