-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added_notification_serivice_and_data_service
- Loading branch information
Showing
10 changed files
with
267 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
.pc-condensed a{ | ||
margin-right:25px; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import React, { Component } from 'react'//we are grabbing one specific thing 'component' from react,hence curly braces | ||
import './product-condensed.css'; | ||
|
||
class ProductCondensed extends Component{ | ||
render(){ | ||
return ( | ||
<li className = "list-group-item pc-condensed"> | ||
<a className= "btn btn-outline-danger">X</a> | ||
<p>{this.props.product.title} | <b>${this.props.product.price}</b></p> | ||
</li> | ||
) | ||
} | ||
} | ||
|
||
export default ProductCondensed; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,7 @@ | ||
.Product { | ||
width:20rem; | ||
max-width:15rem; | ||
} | ||
|
||
.Product img { | ||
max-height:15rem; | ||
max-height:10rem; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
import NotificationService, {NOTIF_WISHLIST_CHANGED} from './notification-service'; | ||
|
||
let ns = new NotificationService(); | ||
// no matter how many times a new object of type notificationservice is created, | ||
// it will still reference to the same one in memory | ||
let instance = null; | ||
var wishList = []; | ||
|
||
class DataService{ | ||
constructor(){ | ||
if(!instance){ | ||
instance = this; | ||
// if instance is created for the very first time, we are | ||
// making it not null and create once in memory at "this" point in time | ||
// and we store in the instance permanently | ||
// now next time an object of the class is created, it checks that | ||
// the object is not null and thus returns the same instance | ||
|
||
} | ||
return instance; | ||
} | ||
addWishListItem = item => { | ||
wishList.push(item); | ||
ns.postNotification(NOTIF_WISHLIST_CHANGED,wishList); | ||
// posts notification with the new item in the wish list | ||
} | ||
removeWishListItem = item => { | ||
for(var x=0; x<wishList.length;x++){ | ||
if (wishList[x]._id === item._id){ | ||
wishList.splice(x,1); | ||
ns.postNotification(NOTIF_WISHLIST_CHANGED,wishList); | ||
break; | ||
} | ||
} | ||
} | ||
|
||
} | ||
export default DataService; | ||
// we are gonna use it for the application so that it has only one dataservice | ||
// that can be accessed. If we have more than one different data services, the data is | ||
// inconsistent and | ||
|
||
|
||
|
||
|
||
|
||
|
||
// class Car { | ||
// // engine size | ||
// // color | ||
// } | ||
|
||
// car1 = Car(); | ||
// car2 = Car(); | ||
// a singleton allows only one instance of the car in the memory. | ||
// it can never be more than one |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// we could also make our http service a singleton which is not needed here. | ||
// If there is app that has multipe components and those components needs to access the | ||
// HTTP service we would want to make it a singleton. | ||
export const NOTIF_WISHLIST_CHANGED = "notif_wishlist_changed"; | ||
// in our data-service, we need to start posting notifications, but before posting | ||
// notifications, we need to have a central place where we can store them | ||
var observers = {}; | ||
let instance = null; | ||
class NotificationService { | ||
constructor(){ | ||
if (!instance){ | ||
instance = this; | ||
} | ||
|
||
return instance; | ||
} | ||
|
||
postNotification = (notifName, data) => { | ||
let obs = observers[notifName]; | ||
for (var x = 0; x < obs.length; x++){ | ||
var obj = obs[x]; | ||
obj.callBack(data); | ||
} | ||
} | ||
removeObserver = (observer,notifName) => { | ||
var obs = observers[notifName]; | ||
if(obs) { | ||
for (var x=0; x<obs.length; x++){ | ||
if(observer === obs[x].observer){ | ||
obs.splice(x,1); | ||
observers[notifName] = obs; | ||
break; | ||
// *important* | ||
// multiple things can hold on to the same memory address, to the same | ||
// memory spot. If there is a component created by react and it is on | ||
// the screen, it existed but if it is also stored in observers, now there are | ||
// two places that are referencing or pointing to the same space in memory | ||
// Here in this program, it is being checked whether the observer is the exact same one | ||
// one thats in memory at that address location | ||
} | ||
} | ||
} | ||
} | ||
|
||
addObserver = (notifName, observer, callBack) => { | ||
let obs = observers[notifName]; | ||
|
||
if(!obs){ | ||
observers[notifName] = []; | ||
//if there is no array in there, | ||
// that means we have never registered for that notification | ||
// before. So we are creating an | ||
// empty array at tha slot for that specific notification | ||
// name that is passed to this function | ||
} | ||
let obj = {observer: observer, callBack: callBack}; | ||
observers[notifName].push(obj); | ||
// notification name is used as a special key on our observers list | ||
// and pushed to that array | ||
} | ||
} | ||
|
||
export default NotificationService; | ||
// we store a list of observers. An observer is a class or a component. An observer is a class or a component that says hello, I would like to listen | ||
// (kind of like register to vote, here class registers to observe) and when it is time to be notified. Obbserver will register and the system will send | ||
// back notifications when it is time to be notified. |
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
import React, { Component } from 'react'//we are grabbing one specific thing 'component' from react,hence curly braces | ||
import './wishlist.css'; | ||
import ProductCondensed from '../product-condensed/product-condensed' | ||
import DataService from '../services/data-service'; | ||
import NotificationService,{NOTIF_WISHLIST_CHANGED} from '../services/notification-service' | ||
|
||
let ns = new NotificationService(); | ||
class WishList extends Component{ | ||
|
||
constructor(props){ | ||
super(props); | ||
|
||
this.state = {wishList:[]}; | ||
// bind functions | ||
this.createWishList = this.createWishList.bind(this); | ||
this.onWishListChanged = this.onWishListChanged.bind(this); | ||
} | ||
|
||
// when the component is mounting or it is about to load or if it did just load on | ||
// the screen we can do something, same goes with unmount, we can do something when | ||
// it goes out of memory | ||
componentDidMount() { | ||
ns.addObserver(NOTIF_WISHLIST_CHANGED, this, this.onWishListChanged) | ||
// we can add ourselves an observer here | ||
} | ||
|
||
componentWillUnmount(){ | ||
ns.removeObserver(this, NOTIF_WISHLIST_CHANGED); | ||
// we can remove ourelves an observer here. If we dont remove ourselves | ||
// an observer here, we could have a memory leak on our app. The notification | ||
// service will still hold on to this entire component even though it is not | ||
// on the screen anymore. | ||
} | ||
|
||
onWishListChanged(newWishList){ | ||
this.setState({wishList: newWishList}) | ||
} | ||
|
||
createWishList = () => { | ||
const list = this.state.wishList.map((product) => | ||
<ProductCondensed product= {product} key={product._id} /> | ||
|
||
) | ||
return (list); | ||
} | ||
|
||
render(){ | ||
return ( | ||
<div className = "card"> | ||
<div className = "card-block"> | ||
<h4 className = "card-title">wish List</h4> | ||
<ul className = "list-group"> | ||
{this.createWishList()} | ||
</ul> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
export default WishList; |