Skip to content

Instantly share code, notes, and snippets.

@ac205
Created February 8, 2020 01:34
Show Gist options
  • Save ac205/629bbba3e4b6d884ae0ac953fa553547 to your computer and use it in GitHub Desktop.
Save ac205/629bbba3e4b6d884ae0ac953fa553547 to your computer and use it in GitHub Desktop.
Firebase - Redux
import productsList from "../dummyData/productsDummyData";
const initState = {
products: productsList,
count: 1,
cartContents: [],
confirmDelete: {
open: false,
id: null
},
addToCartSuccess: false
};
export default function(state = initState, action) {
switch (action.type) {
case "ADD_TO_CART":
{
const existingItem = state.cartContents.find(item => {
return item.id === action.payload.id
});
let newCartContents;
if (existingItem) {
newCartContents = state.cartContents.map(item => {
if (item.id === action.payload.id) {
return {
...item,
count:
action.payload.count >= 1
? action.payload.count
: item.count + 1
};
}
return item;
});
} else {
let newItem;
action.payload.count
? (newItem = {
...state.firestore.data.products[action.payload.id],
count: action.payload.count
})
: (newItem = state.firestore.data.products[action.payload.id]);
newCartContents = [...state.cartContents, newItem];
}
let addToCartSnackBar = action.payload.buyNow ? false : true;
state = {
...state,
cartItems: state.cartItems + 1,
cartContents: newCartContents,
addToCartSuccess: addToCartSnackBar
};
}
break;
case "SUBTRACT_ITEM":
{
let confirm = {
open: false
};
let subtractItem = state.cartContents.map(item => {
if (item.id === Number(action.payload) && item.count === 1) {
confirm = {
open: true,
id: Number(action.payload)
};
return item;
} else if (item.id === Number(action.payload) && item.count !== 0) {
return { ...item, count: item.count - 1 };
} else {
return item;
}
});
state = {
...state,
cartContents: subtractItem,
confirmDelete: confirm
};
}
break;
case "ADD_ITEM":
{
let addItem = state.cartContents.map(item => {
if (item.id === Number(action.payload)) {
return { ...item, count: item.count + 1 };
}
return item;
});
state = {
...state,
cartContents: addItem
};
}
break;
case "DELETE_ITEM_CONFIRM":
state = {
...state,
confirmDelete: {
open: true,
id: Number(action.payload)
}
};
break;
case "CANCEL_DELETE_ITEM":
state = {
...state,
confirmDelete: {
open: false,
id: null
}
};
break;
case "DELETE_ITEM":
let deletedItem = state.cartContents.filter(
item => item.id !== state.confirmDelete.id
);
state = {
...state,
confirmDelete: {
open: false,
id: null
},
cartContents: deletedItem
};
break;
case "CLOSE_SUCCESS_MSG":
state = {
...state,
addToCartSuccess: false
};
break;
default:
}
return state;
}
const productsList = [{
id: 0,
productCode: "TS1000",
name: "Sample T-Shirt",
subname: "High Quality Shirt",
category: "Shirts",
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut vel odio vitae dolor condimentum efficitur id ullamcorper massa. Proin eleifend euismod justo sed dictum. Aliquam luctus, justo eget hendrerit fringilla, dolor magna rhoncus ante, et tincidunt neque odio a arcu. Quisque accumsan pellentesque risus ut blandit",
price: 5.99,
salePrice: null,
img: [
"https://img.represent.com/uploads/8590b79f04a01bbdfd6fa32311382a2a.png?auto=format&w=750",
"https://img.represent.com/uploads/8590b79f04a01bbdfd6fa32311382a2a.png?auto=format&w=750",
"https://img.represent.com/uploads/8590b79f04a01bbdfd6fa32311382a2a.png?auto=format&w=750",
"https://img.represent.com/uploads/8590b79f04a01bbdfd6fa32311382a2a.png?auto=format&w=750",
],
tags: ["tshirt", "gray"],
rating: 4,
available: false,
count: 1,
},
{
id: 1,
productCode: "TS1001",
name: "Sample T-Shirt 2",
subname: "Medium Quality Shirt",
category: "Shirts",
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut vel odio vitae dolor condimentum efficitur id ullamcorper massa. Proin eleifend euismod justo sed dictum. Aliquam luctus, justo eget hendrerit fringilla, dolor magna rhoncus ante, et tincidunt neque odio a arcu. Quisque accumsan pellentesque risus ut blandit",
price: 15.99,
salePrice: 11.99,
img: [
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false",
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false",
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false",
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false",
],
tags: ["tshirt", "white"],
rating: 5,
available: true,
count: 1,
},
{
id: 2,
productCode: "TS1001",
name: "Sample T-Shirt 3",
subname: "Medium Quality Shirt",
category: "Shirts",
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut vel odio vitae dolor condimentum efficitur id ullamcorper massa. Proin eleifend euismod justo sed dictum. Aliquam luctus, justo eget hendrerit fringilla, dolor magna rhoncus ante, et tincidunt neque odio a arcu. Quisque accumsan pellentesque risus ut blandit",
price: 29.99,
salePrice: 9.99,
img: [
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false",
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false",
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false",
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false",
],
tags: ["tshirt", "white"],
rating: 1,
available: true,
count: 1,
},
{
id: 3,
productCode: "TS1001",
name: "Sample Shoes",
subname: "Nike Shoes",
category: "Shoes",
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut vel odio vitae dolor condimentum efficitur id ullamcorper massa. Proin eleifend euismod justo sed dictum. Aliquam luctus, justo eget hendrerit fringilla, dolor magna rhoncus ante, et tincidunt neque odio a arcu. Quisque accumsan pellentesque risus ut blandit",
price: 525.99,
salePrice: 105.99,
img: [
"https://www.triads.co.uk/images/products/zoom/1388768969-24349200.jpg",
"https://www.triads.co.uk/images/products/medium/1388768970-89460500.jpg",
"https://www.triads.co.uk/images/products/medium/1388768974-29585200.jpg",
"https://www.triads.co.uk/images/products/medium/1388768977-96193600.jpg",
],
tags: ["shoes", "white"],
rating: 2,
available: true,
count: 1,
},
{
id: 4,
productCode: "TS1001",
name: "Sample Shoes 2",
subname: "Nike Shoes",
category: "Shoes",
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut vel odio vitae dolor condimentum efficitur id ullamcorper massa. Proin eleifend euismod justo sed dictum. Aliquam luctus, justo eget hendrerit fringilla, dolor magna rhoncus ante, et tincidunt neque odio a arcu. Quisque accumsan pellentesque risus ut blandit",
price: 425.99,
salePrice: 399.99,
img: [
"https://www.triads.co.uk/images/products/medium/1564056059-04593500.jpg",
"https://www.triads.co.uk/images/products/medium/1564056067-04653000.jpg",
"https://www.triads.co.uk/images/products/medium/1564056075-79328300.jpg",
"https://www.triads.co.uk/images/products/medium/1564056083-28078800.jpg",
],
tags: ["shoes", "black"],
rating: 4,
available: true,
count: 1,
},
{
id: 5,
productCode: "TS1001",
name: "Sunglasses",
subname: "Designer Shades",
category: "Accessories",
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut vel odio vitae dolor condimentum efficitur id ullamcorper massa. Proin eleifend euismod justo sed dictum. Aliquam luctus, justo eget hendrerit fringilla, dolor magna rhoncus ante, et tincidunt neque odio a arcu. Quisque accumsan pellentesque risus ut blandit",
price: 25.99,
salePrice: 2.99,
img: [
"https://embryo-react.theironnetwork.org/static/media/a-5-a.eae9d54f.jpg",
"https://embryo-react.theironnetwork.org/static/media/a-5-a.eae9d54f.jpg",
"https://embryo-react.theironnetwork.org/static/media/a-5-a.eae9d54f.jpg",
"https://embryo-react.theironnetwork.org/static/media/a-5-a.eae9d54f.jpg",
],
tags: ["sunglasses", "black"],
rating: 4,
available: true,
count: 1,
},
{
id: 6,
productCode: "TS1001",
name: "Leather Wallet",
subname: "Wallets",
category: "Accessories",
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut vel odio vitae dolor condimentum efficitur id ullamcorper massa. Proin eleifend euismod justo sed dictum. Aliquam luctus, justo eget hendrerit fringilla, dolor magna rhoncus ante, et tincidunt neque odio a arcu. Quisque accumsan pellentesque risus ut blandit",
price: 45.99,
salePrice: null,
img: [
"https://embryo-react.theironnetwork.org/static/media/a-4-a.8147d4e6.jpg",
"https://embryo-react.theironnetwork.org/static/media/a-4-a.8147d4e6.jpg",
"https://embryo-react.theironnetwork.org/static/media/a-4-a.8147d4e6.jpg",
"https://embryo-react.theironnetwork.org/static/media/a-4-a.8147d4e6.jpg",
],
tags: ["wallet", "brown"],
rating: 3,
available: true,
count: 1,
},
{
id: 7,
productCode: "TS1001",
name: "Sample T-Shirt 8",
subname: "Medium Quality Shirt",
category: "Shirts",
description: "Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut vel odio vitae dolor condimentum efficitur id ullamcorper massa. Proin eleifend euismod justo sed dictum. Aliquam luctus, justo eget hendrerit fringilla, dolor magna rhoncus ante, et tincidunt neque odio a arcu. Quisque accumsan pellentesque risus ut blandit",
price: 529.99,
salePrice: 515.99,
img: [
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false",
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false",
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false",
"https://www.rushordertees.com/design/ZoomImage.php?src=3082864_f&style=G200&colorCode=00&x=240&y=300&width=880&height=880&scale=1.7&watermark=false",
],
tags: ["tshirt", "white"],
rating: 5,
available: true,
count: 1,
},
];
export default productsList;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment