Anda di halaman 1dari 15

# redux-saga-sandbox

A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`

`npm start`

Visit `http://localhost:8082` using Chrome

# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`

`npm start`

Visit `http://localhost:8082` using Chrome

# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`

`npm start`

Visit `http://localhost:8082` using Chrome


# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`

`npm start`

Visit `http://localhost:8082` using Chrome

# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`

`npm start`

Visit `http://localhost:8082` using Chrome

# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`

`npm start`
Visit `http://localhost:8082` using Chrome

# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`

`npm start`

Visit `http://localhost:8082` using Chrome

# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`

`npm start`

Visit `http://localhost:8082` using Chrome

# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`
`npm start`

Visit `http://localhost:8082` using Chrome


# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`

`npm start`

Visit `http://localhost:8082` using Chrome


# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`

`npm start`

Visit `http://localhost:8082` using Chrome


# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`

`npm start`

Visit `http://localhost:8082` using Chrome


# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`

`npm start`

Visit `http://localhost:8082` using Chrome


# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`

`npm start`

Visit `http://localhost:8082` using Chrome


# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`

`npm start`

Visit `http://localhost:8082` using Chrome


# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.
## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`
# Redux Saga Shopping Cart Back-end Server

## About
This server is meant to mock the functionality of a real shopping application's
back-end services. It comes ready-made and complete, but with no front-end
application to consume its APIs. That task is left to you.

### Demographics
This application is meant to be used by, but not limited to, students completing
Daniel Stern's Redux Saga course on Pluralsight.

### Usage
1. Install dependencies: `npm install`
2. Run server. `npm start`
3. Navigate to the instructions page: `http://localhost:8081` (You must not already
have a different application running on this port)
# Redux Saga Shopping Cart Back-end Server

## About
This server is meant to mock the functionality of a real shopping application's
back-end services. It comes ready-made and complete, but with no front-end
application to consume its APIs. That task is left to you.

### Demographics
This application is meant to be used by, but not limited to, students completing
Daniel Stern's Redux Saga course on Pluralsight.

### Usage
1. Install dependencies: `npm install`
2. Run server. `npm start`
3. Navigate to the instructions page: `http://localhost:8081` (You must not already
have a different application running on this port)
# Redux Saga Shopping Cart Back-end Server

## About
This server is meant to mock the functionality of a real shopping application's
back-end services. It comes ready-made and complete, but with no front-end
application to consume its APIs. That task is left to you.

### Demographics
This application is meant to be used by, but not limited to, students completing
Daniel Stern's Redux Saga course on Pluralsight.

### Usage
1. Install dependencies: `npm install`
2. Run server. `npm start`
3. Navigate to the instructions page: `http://localhost:8081` (You must not already
have a different application running on this port)

`npm start`
Visit `http://localhost:8082` using Chrome

users:
- id: U10000
name: J.R.R Hemingway
country: CAD
address1: 555 La Floridita Way
phone: 15551234567

- id: U20000
name: George R. R. Rowling
country: USD
address1: 14 Diagon Alley

cards:
- id: C10000
owner: U10000
number: 4500123456787777
availableFunds: 500

carts:
- owner: U10000
items:
- id: I10000
quantity: 2
- id: I20000
quantity: 1
- id: I40000
quantity: 3

- owner: U20000
items:
- id: I50000
quantity: 3

items:
- id: I10000
name: Velvet Mousepad
description: Your mouse never had it so good.
usd: 129.95
cad: 175.85
img: velvet-mousepad.png
quantityAvailable: 100000
weight: 0.8

- id: I20000
name: Wood-Grain USB Cable
description: This cable matches any mahogany or oak-based computer chassis.
usd: 35.50
cad: 55.60
img: usb-cable.png
quantityAvailable: 3
weight: 0.2

- id: I30000
name: 1 Year Subscription - JavaScript Enthusiast Magazine
description: Finally, JavaScript news, delivered right to your front door!
usd: 119.15
cad: 119.15
quantityAvailable: 99999
weight: 0

- id: I40000
name: Teaching Computers to Golf, 1st Edition
description: Don't try to teach your Mac or PC how to play golf without it.
usd: 15.95
cad: 22.95
quantityAvailable: 5
weight: 0.5

- id: I50000
name: I <3 Yield Pin
description: Show your friends how much you love JavaScript's newest keyword!
usd: 1.95
cad: 2.95
quantityAvailable: 0
weight: 0.1

taxRates:
- symbol: USD
rate: 0.15
- symbol: CAD
rate: 0.19

const express = require('express');


const cors = require('cors');
const port = 8081;
const app = new express();
YAML = require('yamljs');

const serverDelayConstant = 100;


// Simulate a small amount of delay to demonstrate app's async features
app.use((req, res, next) => {
const delay = (Math.random() * 15 + 5) * serverDelayConstant;
setTimeout(next, delay);
});

app.use(express.static('public'));

nativeObject = YAML.load('database.yml', (database) => {

const makeCartAdjustmentRoute = (shouldAdd = true) => (req, res) => {


const { owner, itemID } = req.params;
const cart = database.carts.find(cart => cart.owner === owner);
if (!cart) {
return res
.status(500)
.json({
error: 'No cart found with the specified ID',
owner
});

const item = database.items.find(item => item.id === itemID);


if (!item) {
return res
.status(500)
.json({
error: 'No item found with the specified ID',
itemID
});
}

const existingItem = cart.items.find(cartItem => cartItem.id === itemID);


if (existingItem) {
if (shouldAdd && parseInt(existingItem.quantity) >=
parseInt(item.quantityAvailable)) {
return res.status(503)
.json({
error: 'An insufficient quantity of items remains.',
itemID,
quantityAvailable: item.quantityAvailable
});
}
existingItem.quantity += (shouldAdd ? 1 : -1);
if (existingItem.quantity === 0) {
cart.items = cart.items.filter(item => item.id !== itemID);
}
} else {
if (shouldAdd) {
cart.items.push({
quantity: 1,
id: itemID
});
} else {
return res.status(500)
.json({
error: 'No item with the specified ID exists in the cart to be
removed',
owner,
itemID
});
}

}
res
.status(200)
.send(cart);
};

app.get('/cart/add/:owner/:itemID', makeCartAdjustmentRoute(true));
app.get('/cart/remove/:owner/:itemID', makeCartAdjustmentRoute(false));

app.get('/user/:id', (req, res) => {


const id = req.params.id;
const user = database.users.find(user => user.id === id);
if (!user) {
return res
.status(500)
.json({
error: 'No user with the specified ID',
id
});
} else {
res
.status(200)
.json(user);
}
});

app.use([ '/cart/validate/:owner', '/cart/:owner', '/card/charge/:owner' ], (req,


res, next) => {
const { owner } = req.params;
const cart = database.carts.find(cart => cart.owner === owner);
if (!cart) {
return res
.status(404)
.json({ error: 'No cart with the specified owner', owner });
} else {
req.cart = cart;
next();
}
});

app.get('/cart/validate/:owner', (req, res) => {


const { items } = req.cart;
let validated = true;
let error = null;
items.forEach(({ id, quantity }) => {
const item = database.items.find(item => item.id === id);
if (item.quantityAvailable < quantity) {
validated = false;
error = 'There is an insufficient quantity of ' + id;
}
});
;
res
.status(200)
.json({ validated, error });

});

app.get('/cart/:owner', (req, res) => {


const cart = req.cart;
res
.status(200)
.json(cart);

});

app.use([ '/card/validate/:owner', '/card/charge/:owner' ], (req, res, next) => {


const { owner } = req.params;
const card = database.cards.find(card => card.owner === owner);
if (!card) {
res.status(500).send({ error: `No card is available for user ${owner}` });
}
req.card = card;
next();
});

app.get('/card/validate/:owner', (req, res) => {


const { card } = req;
res
.status(200)
.json({ validated: true });
});

app.get('/card/charge/:owner', (req, res) => {


const { card, cart } = req;
const { owner } = req.params;
const country = database.users.find(user => user.id === owner).country;
const total = cart.items.reduce((total, { quantity, id }) => {
const item = database.items.find(item => item.id === id);
const symbol = country === 'CAD' ? 'cad' : 'usd';
const baseValue = item[ symbol ];
total += baseValue * quantity;
return total;
}, 0);

if (card.availableFunds <= total) {


return res
.status(402)
.json({ success: false });
}

card.availableFunds -= total;
res
.status(201)
.send({ success: true });

});

app.get('/items/:ids', (req, res) => {


const ids = req.params.ids.split(',');
const items = ids.map(id => database.items.find(item => item.id === id));
if (items.includes(undefined)) {
res
.status(500)
.json({ error: 'A specified ID had no matching item' });
} else {
res
.status(200)
.json(items);
}
});

app.get('/prices/:symbol/:ids', (req, res) => {


const ids = req.params.ids.split(',');
const items = ids.map(id => database.items.find(item => item.id === id));
const supportedSymbols = [ 'CAD', 'USD' ];
const symbol = req.params.symbol;
if (!supportedSymbols.includes(symbol)) {
return res
.status(403)
.json({
error: 'The currency symbol provided is inaccurate, see list of supported
currencies',
supportedSymbols
});
}

if (items.includes(undefined)) {
return res
.status(500)
.json({ error: 'A specified ID had no matching item' });
} else {
res
.status(200)
.json(items.map(item => ({
id: item.id,
symbol,
price: symbol === 'USD' ? item.usd : item.cad
})));
}
});

app.get('/shipping/:items', (req, res) => {


const ids = req.params.items.split(',');
let total = 0;
ids.forEach(id => {
const item = database.items.find(item => item.id === id);
if (item.weight === 0) {
total += 0;
} else if (item.weight < 0.5) {
total += 3.5;
} else {
total += 8.5;
}
});
res
.status(200)
.json({
total
});
});

app.get('/tax/:symbol', (req, res) => {


const { symbol } = req.params;
const taxRate = database.taxRates.find(rate => rate.symbol === symbol);
if (!taxRate) {
return res
.status(500)
.json({
symbol,
error: 'No tax rate info for symbol ' + symbol
});
}

res
.status(200)
.json({
rate: taxRate.rate
});

});

app.listen(port, () => {
console.log(`Redux Saga Cart backend server is listening on ${port}`);
});
});
app.use(cors());

# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`

`npm start`

Visit `http://localhost:8082` using Chrome

# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`

`npm start`

Visit `http://localhost:8082` using Chrome

# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`
`npm start`

Visit `http://localhost:8082` using Chrome

# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`

`npm start`

Visit `http://localhost:8082` using Chrome

# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`

`npm start`

Visit `http://localhost:8082` using Chrome

# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`
`npm start`

Visit `http://localhost:8082` using Chrome

# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`

`npm start`

Visit `http://localhost:8082` using Chrome

# redux-saga-sandbox
A console-based Redux Saga sandbox

## About
Use this tool to learn Redux Saga in a responsive, browser-based environment.

## Installation and Getting Started

Enusre you have installed `nodejs` from https://nodejs.org/. Something >= 6.x
should be fine., You will also need to have a version of `npm` >= 5.2.

To install dependencies and start the application run the following commands:

`npm install`

`npm start`

Visit `http://localhost:8082` using Chrome

Anda mungkin juga menyukai