Skip to content

Commit 06ea4dd

Browse files
authored
Merge pull request #211 from CodeForPhilly/api-pets-reduce-requests
reduce requests and event data from Adoption table
2 parents 81a2971 + 11fe8d8 commit 06ea4dd

File tree

2 files changed

+72
-64
lines changed

2 files changed

+72
-64
lines changed

src/client/src/pages/DataView360/components/Adoptions.js

Lines changed: 66 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,97 @@
1-
import React, { Component } from 'react';
2-
import { Paper, Typography, Table, TableContainer, TableHead, TableBody, TableRow, TableCell, Container} from '@material-ui/core';
3-
import { withStyles } from '@material-ui/core/styles';
1+
import React, {Component} from 'react';
2+
import {
3+
Paper,
4+
Typography,
5+
Table,
6+
TableContainer,
7+
TableHead,
8+
TableBody,
9+
TableRow,
10+
TableCell,
11+
Container
12+
} from '@material-ui/core';
13+
import {withStyles} from '@material-ui/core/styles';
414
import styles from "./styles/Adoptions.module.css";
515
import "./styles/table.css";
616
import _ from 'lodash';
17+
import moment from "moment";
718

819
/* I don't khow, how to remove it. So I changed background-color on 'initial' */
9-
const StyledTableCell = withStyles((theme)=>({
10-
head:{
20+
const StyledTableCell = withStyles((theme) => ({
21+
head: {
1122
backgroundColor: 'initial', // here
1223
fontWeight: 600,
1324
}
1425
}))(TableCell);
1526

16-
const StyledTableRow = withStyles((theme)=>({
17-
root:{
18-
'&:nth-of-type(even)':{
27+
const StyledTableRow = withStyles((theme) => ({
28+
root: {
29+
'&:nth-of-type(even)': {
1930
backgroundColor: 'initial', // and here
2031
}
2132
}
2233
}))(TableRow);
2334

24-
35+
const PET_COUNT = 3;
2536

2637
class Adoptions extends Component {
38+
constructor(props) {
39+
super(props);
40+
}
2741

28-
getAnimalIds() {
29-
let result = [];
42+
getLatestPets(petList) {
43+
let retVal;
3044

31-
let animal_ids = _.get(this.props, 'adoptions[0].animal_ids');
32-
if(animal_ids) {
33-
result = _.filter(animal_ids.split("'"), item => {
34-
return _.isNaN(_.parseInt(item)) !== true;
35-
})
36-
}
45+
retVal = petList.slice(0, PET_COUNT);
46+
47+
return retVal;
48+
}
3749

38-
return result;
50+
getAnimalAge(epochTime) {
51+
let dateOfBirth = moment(epochTime * 1000);
52+
return moment().diff(dateOfBirth, 'years');
3953
}
4054

4155
render() {
4256
// todo: update when we add pet info
4357
// todo: clean array of animal_id
44-
const numAdoptions = _.size(this.props.adoptions);
45-
return (<Container className={styles.adoptions} style={{"marginTop":"1em"}}>
46-
<Typography className={styles.adoptions_title} variant='h4'>Adoption/Foster Records {(numAdoptions > 3) && "(Showing latest 3 out of " + numAdoptions + ")"}</Typography>
47-
<TableContainer className="main_table_container" style={{"marginTop":"1em"}} component={Paper} variant='outlined'>
48-
<Table className="main_table">
49-
<TableHead>
50-
<TableRow>
51-
<StyledTableCell align="center">Name</StyledTableCell>
52-
<StyledTableCell align="center">Adoption Type</StyledTableCell>
53-
<StyledTableCell align="center">Adoption Subtype</StyledTableCell>
54-
<StyledTableCell align="center">Animal Type</StyledTableCell>
55-
<StyledTableCell align="center">Breed</StyledTableCell>
56-
<StyledTableCell align="center">Age</StyledTableCell>
57-
<StyledTableCell align="center">Photo</StyledTableCell>
58-
</TableRow>
59-
</TableHead>
60-
<TableBody>
61-
{_.map(this.props.adoptions, (adoptionInfo, index) => {
62-
const photoLink = adoptionInfo["animal_details"]["Photos"][0]
63-
const photo = <img src={photoLink} style={{"maxWidth": "100px"}}/>;
64-
return <StyledTableRow key={adoptionInfo["Time"] + index}>
65-
<TableCell align="center">{adoptionInfo["animal_details"]["Name"]}</TableCell>
58+
const numOfPets = _.size(this.props.adoptions);
59+
const latestPets = this.getLatestPets(this.props.adoptions);
60+
61+
return (<Container className={styles.adoptions} style={{"marginTop": "1em"}}>
62+
<Typography className={styles.adoptions_title} variant='h4'>Adoption/Foster
63+
Records {(numOfPets > 3) && "(Showing 3 Pets out of " + numOfPets + ")"}</Typography>
64+
<TableContainer className="main_table_container" style={{"marginTop": "1em"}} component={Paper}
65+
variant='outlined'>
66+
<Table className="main_table">
67+
<TableHead>
68+
<TableRow>
69+
<StyledTableCell align="center">Name</StyledTableCell>
70+
<StyledTableCell align="center">Animal Type</StyledTableCell>
71+
<StyledTableCell align="center">Breed</StyledTableCell>
72+
<StyledTableCell align="center">Age</StyledTableCell>
73+
<StyledTableCell align="center">Photo</StyledTableCell>
74+
</TableRow>
75+
</TableHead>
76+
<TableBody>
77+
{_.map(latestPets, (adoptionInfo, index) => {
78+
79+
const photoLink = _.get(adoptionInfo, "Photos.[0]");
80+
const photo = <img src={photoLink} style={{"maxWidth": "100px"}}/>
81+
82+
return <StyledTableRow key={index}>
83+
<TableCell align="center">{adoptionInfo["Name"]}</TableCell>
6684
<TableCell align="center">{adoptionInfo["Type"]}</TableCell>
67-
<TableCell align="center">{adoptionInfo["Subtype"]}</TableCell>
68-
<TableCell align="center">{adoptionInfo["animal_details"]["Type"]}</TableCell>
69-
<TableCell align="center">{adoptionInfo["animal_details"]["Breed"]}</TableCell>
70-
<TableCell align="center">{(parseInt(adoptionInfo["animal_details"]["Age"])/12).toFixed(2)}</TableCell>
85+
<TableCell align="center">{adoptionInfo["Breed"]}</TableCell>
86+
<TableCell
87+
align="center">{this.getAnimalAge(adoptionInfo["DOBUnixTime"])}</TableCell>
7188
<TableCell align="center">{photo}</TableCell>
7289
</StyledTableRow>
73-
})}
74-
</TableBody>
75-
</Table>
76-
</TableContainer>
77-
</Container>
90+
})}
91+
</TableBody>
92+
</Table>
93+
</TableContainer>
94+
</Container>
7895
);
7996
}
8097
}

src/server/api/common_api.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -74,21 +74,12 @@ def get_360(matching_id):
7474
animal_ids = person_json["Animal_ids"]
7575

7676
for animal_id in animal_ids:
77-
animal_events = requests.get("http://shelterluv.com/api/v1/animals/{}/events".format(animal_id),
78-
headers={"x-api-key": SHELTERLUV_SECRET_TOKEN})
79-
animal_events_json = animal_events.json()
80-
81-
for event in animal_events_json["events"]:
82-
for adoption in event["AssociatedRecords"]:
83-
if adoption["Type"] == "Person" and adoption["Id"] == row["source_id"]:
84-
del event["AssociatedRecords"]
85-
animal_details = requests.get(
86-
"http://shelterluv.com/api/v1/animals/{}".format(animal_id),
87-
headers={"x-api-key": SHELTERLUV_SECRET_TOKEN})
88-
89-
animal_details_json = animal_details.json()
90-
event["animal_details"] = animal_details_json
91-
adoptions.append(event)
77+
animal_details = requests.get(
78+
"http://shelterluv.com/api/v1/animals/{}".format(animal_id),
79+
headers={"x-api-key": SHELTERLUV_SECRET_TOKEN})
80+
81+
animal_details_json = animal_details.json()
82+
adoptions.append(animal_details_json)
9283

9384
result['adoptions'] = adoptions
9485

0 commit comments

Comments
 (0)