Cleaned code for consistency. Additionally added month function for report form.

This commit is contained in:
wildscotsmen 2018-10-29 00:33:35 -04:00
parent 48273519cb
commit c9f22b74d5
2 changed files with 80 additions and 60 deletions

View File

@ -53,19 +53,6 @@ const styles = theme => ({
},
});
/**
* Function for formatting the
* year as a string that
* Material UI can use.
* @param {*} date, Date passed in.
*/
function getYear(date) {
var d = new Date(date),
year = d.getFullYear();
return year;
}
/**
* Types of sightings. Label is what is
* viewed in the application, value is
@ -228,8 +215,8 @@ class ReportForm extends React.Component {
* State of form components.
*/
state = {
month: '01',
year: getYear(new Date()),
month: this.getMonth(new Date()),
year: this.getYear(new Date()),
time: 'unknown',
type: 'visual',
confidence: '1',
@ -249,6 +236,38 @@ class ReportForm extends React.Component {
});
};
/**
* Function for formatting the
* year as a string that
* Material UI can use.
* @param {*} date, Date passed in.
*/
getYear = date => {
var d = new Date(date),
year = d.getFullYear();
return year;
}
/**
* Function for formatting the
* month as a string that
* Material UI can use.
* @param {*} date, Date passed in.
*/
getMonth = date => {
var d = new Date(date),
month = d.getMonth();
month = month.toString();
if (month.length == 1) {
month = "0" + month;
}
return month;
}
/**
* Handles closing the toast.
*/
@ -294,8 +313,8 @@ class ReportForm extends React.Component {
}
sightingsRef.push(sighting);
this.setState({
year: getYear(new Date()),
month: '01',
year: this.getYear(new Date()),
month: this.getMonth(new Date()),
time: 'unknown',
type: 'visual',
confidence: '1',

View File

@ -101,40 +101,6 @@ const confidenceLevels = [
},
];
/**
* Gets formatted confidence value.
*/
function getConfidence(item) {
for (var i = 0; i < confidenceLevels.length; i++) {
if (confidenceLevels[i].value === item) {
return confidenceLevels[i].label;
}
}
}
/**
* Gets formatted time value.
*/
function getTime(item) {
for (var i = 0; i < timeTypes.length; i++) {
if (timeTypes[i].value === item) {
return timeTypes[i].label;
}
}
}
/**
* Gets formatted type value.
*/
function getType(item) {
for (var i = 0; i < sightingTypes.length; i++) {
if (sightingTypes[i].value === item) {
return sightingTypes[i].label;
}
}
}
export class MapContainer extends Component {
// Get the user's location using Google's geolocation
@ -162,6 +128,41 @@ export class MapContainer extends Component {
}
}
/**
* Gets formatted type value.
*/
getType = item => {
for (var i = 0; i < sightingTypes.length; i++) {
if (sightingTypes[i].value === item) {
return sightingTypes[i].label;
}
}
}
/**
* Gets formatted time value.
*/
getTime = item => {
for (var i = 0; i < timeTypes.length; i++) {
if (timeTypes[i].value === item) {
return timeTypes[i].label;
}
}
}
/**
* Gets formatted confidence value.
*/
getConfidence = item => {
for (var i = 0; i < confidenceLevels.length; i++) {
if (confidenceLevels[i].value === item) {
return confidenceLevels[i].label;
}
}
}
// When the component has mounted to the DOM, get the user's location
componentDidMount() {
this.getLocation();
@ -249,10 +250,10 @@ export class MapContainer extends Component {
key={sighting.id}
position={{ lat: sighting.lat, lng: sighting.lng }}
onClick={this.onMarkerClick}
type = {'Type: ' + getType(sighting.type)}
confidence = {<Fragment><b>Confidence:</b> {getConfidence(sighting.confidence)}</Fragment>}
type={'Type: ' + this.getType(sighting.type)}
confidence={<Fragment><b>Confidence:</b> {this.getConfidence(sighting.confidence)}</Fragment>}
date={<Fragment><b>Date:</b> {this.formatDate(sighting.date)}</Fragment>}
time = {<Fragment><b>Time:</b> {getTime(sighting.time)}</Fragment>}
time={<Fragment><b>Time:</b> {this.getTime(sighting.time)}</Fragment>}
description={<Fragment><b>Description:</b> {sighting.desc}</Fragment>}
/>
)