var
express
=
require(
'express'
);
var
route_session
=
require(
'./server_modules/route-session'
);
var
route_registration
=
require(
'./server_modules/route-registration'
);
var
app
=
express();
var
mongo
=
require(
'./server_modules/mongo'
);
var
bodyParser
=
require(
'body-parser'
);
var
cookieParser
=
require(
'cookie-parser'
);
var
route_parking
=
require(
'./server_modules/route-parking'
);
var
path
=
require(
'path'
);
// Initialize connection once
mongo.connect().then(
function
() {
app.listen(app.get(
'port'
),
function
() {
console.log(
'Express started on http://localhost:'
+
app.get(
'port'
)
+
'; press Ctrl-C to terminate.'
);
});
})
.
catch
(
function
(error) {
console.error(
'error in connecting to MongoDB'
);
throw
error;
});
var
port
=
process.env.PORT
||
8080
;
app.set(
'port'
, port);
app.use(bodyParser.json());
// for parsing application/json
app.use(cookieParser(
"Look at my horse! My horse is amazing!"
));
app.use(
'/api/session'
, route_session);
app.use(
'/api/registration'
, route_registration);
app.use(
'/api/parking'
, route_parking);
// Serve static assets
app.use(
'/release'
, express.
static
(__dirname
+
'/release'
));
// Always return the main index.html, so react-router render the route in the client
app.get(
'*'
,
function
(req, res) {
res.sendFile(path.resolve(__dirname,
'.'
,
'release'
,
'index.html'
));
});
// 404 catch-all handler (middleware)
app.use(
function
(req, res, next) {
res.status(
404
);
});
// 500 error handler (middleware)
app.use(
function
(err, req, res, next) {
console.error(err.stack);
res.status(
500
);
});
72
ПРИЛОЖЕНИЕ Л
Программный код React-компонента ParkingSpot.react.js
import
React
from
'react'
;
import
*
as
styles
from
'./ParkingSpot.scss'
;
class
ParkingSpot
extends
React.
PureComponent
{
static
displayName
=
'ParkingSpot'
;
constructor
(props) {
super
(props);
}
static
propTypes
= {
occupied
: React.
PropTypes
.
bool
,
text
: React.
PropTypes
.
string
,
noBorder
: React.
PropTypes
.
bool
,
leftBorder
: React.
PropTypes
.
bool
,
onSpotClick
: React.
PropTypes
.
func
,
reserved
: React.
PropTypes
.
bool
};
render
() {
const
{
occupied
,
text
,
noBorder
,
leftBorder
,
onSpotClick
,
reserved
} =
this
.
props
;
let
currentClassName
= styles.circleGreen;
if
(
occupied
) {
currentClassName
= styles.circleRed;
}
else if
(
reserved
) {
currentClassName
= styles.circleGrey;
}
return
(
<
div
className
=
{styles.container}
style
=
{
noBorder
? {
border
:
'none'
} : {
borderLeft
:
leftBorder
?
'4px solid white'
:
'none'
}}
onClick
=
{
onSpotClick
}
>
<
div
className
=
{styles.innerContainer}>
<
div
className
=
{styles.
caption
}>{
text
}
div
>
<
div
className
=
{
currentClassName
}
style
=
{
occupied
!==
null
?
null
: {
width
:
'0px'
,
height
:
'0px'
}}
/>
div
>
div
>
);
}
}
export default
ParkingSpot;
73
ПРИЛОЖЕНИЕ М
Программный код хранилища ParkingSpotsStore.js
import
EventEmitter
from
'events'
;
var
CURRENT_USER_RESERVED_SPOT
=
'CURRENT_USER_RESERVED_SPOT'
;
var
CONTENT_CHANGE
=
'CONTENT_CHANGE'
;
var
RESERVED_SPOT_CHANGE
=
'RESERVED_SPOT_CHANGE'
;
var
ParkingSpotsStore
= Object.
assign
({}, EventEmitter.
prototype
, {
content
:
null
,
reservedSpotId
:
null
,
wasSpotReserved
:
false
,
emitCurrentUserSpotReserve
:
function
() {
this
.
emit
(
CURRENT_USER_RESERVED_SPOT
);
},
emitContentChange
:
function
() {
this
.
emit
(
CONTENT_CHANGE
);
},
isLoaded
:
function
() {
return
!!
this
.
content
;
},
/**
*
@param
{function} callback
*/
addCurrentUserSpotReserveListener
:
function
(callback) {
this
.
on
(
CURRENT_USER_RESERVED_SPOT
, callback);
},
/**
*
@param
{function} callback
*/
addContentChangeListener
:
function
(callback) {
this
.
on
(
CONTENT_CHANGE
, callback);
},
/**
*
@param
{function} callback
*/
addReserveListener
:
function
(callback) {
this
.
on
(
RESERVED_SPOT_CHANGE
, callback);
},
reserveParkingSpot
:
function
(id) {
if
(id !==
null
) {
if
(!
this
.
content
||
this
.
content
.length <=
0
) {
return
;
}
this
.
reservedSpotId
= +id;
this
.
emitReserve
();
// this.emitContentChange();
}
},
emitReserve
:
function
() {
this
.
emit
(
RESERVED_SPOT_CHANGE
);
},
74
rewriteContent
:
function
(data) {
if
(data) {
let
newReservedSpotId
=
null
;
let
didThisUserReserve
=
false
;
for
(
let
index
=
0
;
index
< data.length; ++
index
) {
if
(data[
index
].currentUserReservation ===
true
) {
newReservedSpotId
=
index
;
didThisUserReserve
=
true
;
break
;
}
}
this
.
toggleSpotReservationBlocking
(
didThisUserReserve
);
this
.
content
= data;
if
(
newReservedSpotId
!==
this
.
reservedSpotId
) {
this
.
reservedSpotId
=
newReservedSpotId
;
this
.
emitReserve
();
}
this
.
emitContentChange
();
}
},
toggleSpotReservationBlocking
:
function
(wasSpotReserved) {
if
(wasSpotReserved !==
this
.
wasSpotReserved
) {
this
.
wasSpotReserved
= wasSpotReserved;
this
.
emitCurrentUserSpotReserve
();
}
},
/**
*
@param
{function} callback
*/
removeCurrentUserSpotReserveListener
:
function
(callback) {
this
.
removeListener
(
CURRENT_USER_RESERVED_SPOT
, callback);
},
/**
*
@param
{function} callback
*/
removeContentChangeListener
:
function
(callback) {
this
.
removeListener
(
CONTENT_CHANGE
, callback);
},
/**
*
@param
{function} callback
*/
removeReserveListener
:
function
(callback) {
this
.
removeListener
(
RESERVED_SPOT_CHANGE
, callback);
}
});
export default
ParkingSpotsStore
;
Do'stlaringiz bilan baham: |