Skip to content

Commit

Permalink
Reunify all animals from SR and Owner pages. (#259)
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexMountain authored Mar 22, 2021
1 parent 2f548b8 commit d24a5b6
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 7 deletions.
44 changes: 43 additions & 1 deletion frontend/src/hotline/ServiceRequestDetails.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
faClipboardCheck, faClipboardList, faComment, faEdit, faHouseDamage,
faKey, faMapMarkedAlt, faMinusSquare, faPlusSquare, faTimes, faTrailer, faUsers
} from '@fortawesome/free-solid-svg-icons';
import { faCalendarEdit, faHomeAlt } from '@fortawesome/pro-solid-svg-icons';
import { faCalendarEdit, faHomeAlt, faHomeHeart } from '@fortawesome/pro-solid-svg-icons';
import Header from '../components/Header';
import History from '../components/History';
import noImageFound from '../static/images/image-not-found.png';
Expand Down Expand Up @@ -64,10 +64,25 @@ function ServiceRequestDetails({id}) {
visit_notes: [],
});

const [show, setShow] = useState(false);
const handleClose = () => setShow(false);
const [animalToDelete, setAnimalToDelete] = useState({id:0, name:''});
const [showAnimalConfirm, setShowAnimalConfirm] = useState(false);
const handleAnimalClose = () => setShowAnimalConfirm(false);

// Handle animal reunification submit.
const handleSubmit = async () => {
await axios.patch('/hotline/api/servicerequests/' + id + '/', {reunite_animals:true})
.then(response => {
setData(prevState => ({ ...prevState, "status":"Closed", "animals":prevState['animals'].map(animal => ({...animal, status:animal.status !== 'DECEASED' ? 'REUNITED' : 'DECEASED'})) }));
handleClose()
})
.catch(error => {
console.log(error.response);
});
}

// Handle animal removal submit.
const handleAnimalSubmit = async () => {
await axios.patch('/hotline/api/servicerequests/' + id + '/', {remove_animal:animalToDelete.id})
.then(response => {
Expand Down Expand Up @@ -300,6 +315,19 @@ function ServiceRequestDetails({id}) {
>
<Link href={"/animals/new?servicerequest_id=" + id}><FontAwesomeIcon icon={faPlusSquare} className="ml-1" inverse /></Link>
</OverlayTrigger>
{data.status.toLowerCase() !== 'closed' ?
<OverlayTrigger
key={"reunite"}
placement="top"
overlay={
<Tooltip id={`tooltip-reunite`}>
Reunite all service request animals
</Tooltip>
}
>
<FontAwesomeIcon icon={faHomeHeart} onClick={() => setShow(true)} style={{cursor:'pointer'}} className="ml-1 fa-move-up" inverse />
</OverlayTrigger>
: ""}
</h4>
</Card.Title>
<hr />
Expand Down Expand Up @@ -517,6 +545,20 @@ function ServiceRequestDetails({id}) {
<Button variant="secondary" onClick={handleAnimalClose}>Close</Button>
</Modal.Footer>
</Modal>
<Modal show={show} onHide={handleClose}>
<Modal.Header closeButton>
<Modal.Title>Confirm Animal Reunification</Modal.Title>
</Modal.Header>
<Modal.Body>
<p>
Have all of the animals in this service request been reunited with their owner?
</p>
</Modal.Body>
<Modal.Footer>
<Button variant="primary" onClick={handleSubmit}>Yes</Button>
<Button variant="secondary" onClick={handleClose}>Close</Button>
</Modal.Footer>
</Modal>
</>
);
};
Expand Down
47 changes: 45 additions & 2 deletions frontend/src/people/PersonDetails.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import React, {useEffect, useState} from 'react';
import axios from "axios";
import { Link } from 'raviger';
import { Card, ListGroup, OverlayTrigger, Tooltip } from 'react-bootstrap';
import { Button, Card, ListGroup, Modal, OverlayTrigger, Tooltip } from 'react-bootstrap';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import {
faClipboardList, faEdit, faPhone, faPlusSquare
} from '@fortawesome/free-solid-svg-icons';
import { faHomeHeart } from '@fortawesome/pro-solid-svg-icons';
import Moment from 'react-moment';
import Header from '../components/Header';
import History from '../components/History';
Expand All @@ -16,6 +17,21 @@ function PersonDetails({id}) {
// Determine if this is an owner or reporter when creating a Person.
var is_owner = window.location.pathname.includes("owner")

const [show, setShow] = useState(false);
const handleClose = () => setShow(false);

// Handle animal reunification submit.
const handleSubmit = async () => {
await axios.patch('/people/api/person/' + id + '/', {reunite_animals:true})
.then(response => {
setData(prevState => ({ ...prevState, "animals":prevState['animals'].map(animal => ({...animal, status:animal.status !== 'DECEASED' ? 'REUNITED' : 'DECEASED'})) }));
handleClose()
})
.catch(error => {
console.log(error.response);
});
}

const [data, setData] = useState({
first_name: '',
last_name: '',
Expand Down Expand Up @@ -73,7 +89,7 @@ function PersonDetails({id}) {
placement="bottom"
overlay={
<Tooltip id={`tooltip-add-owner`}>
Add another owner
Add another owner for all of these animals
</Tooltip>
}
>
Expand Down Expand Up @@ -166,6 +182,19 @@ function PersonDetails({id}) {
>
<Link href={"/animals/new?owner_id=" + id}><FontAwesomeIcon icon={faPlusSquare} className="ml-1" inverse /></Link>
</OverlayTrigger>
{is_owner && data.animals.filter(animal => (!['REUNITED', 'DECEASED'].includes(animal.status))).length > 0 ?
<OverlayTrigger
key={"reunite"}
placement="top"
overlay={
<Tooltip id={`tooltip-reunite`}>
Reunite all owner animals
</Tooltip>
}
>
<FontAwesomeIcon icon={faHomeHeart} onClick={() => setShow(true)} style={{cursor:'pointer'}} className="ml-1 fa-move-up" inverse />
</OverlayTrigger>
: ""}
</h4>
</Card.Title>
<hr/>
Expand All @@ -176,6 +205,20 @@ function PersonDetails({id}) {
</div>
</div>
<History action_history={data.action_history} />
<Modal show={show} onHide={handleClose}>
<Modal.Header closeButton>
<Modal.Title>Confirm Animal Reunification</Modal.Title>
</Modal.Header>
<Modal.Body>
<p>
Have all of the animals been reunited with this owner?
</p>
</Modal.Body>
<Modal.Footer>
<Button variant="primary" onClick={handleSubmit}>Yes</Button>
<Button variant="secondary" onClick={handleClose}>Close</Button>
</Modal.Footer>
</Modal>
</>
);
};
Expand Down
2 changes: 1 addition & 1 deletion hotline/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
class ServiceRequest(Location):

#keys
owners = models.ManyToManyField(Person, blank=True)
owners = models.ManyToManyField(Person, blank=True, related_name='request')
reporter = models.ForeignKey(Person, on_delete=models.SET_NULL, blank=True, null=True, related_name='reporter_service_request')
status = models.CharField(max_length=10, choices=STATUS_CHOICES, blank=False, default='open')

Expand Down
11 changes: 10 additions & 1 deletion hotline/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,16 @@ def perform_update(self, serializer):

if service_request.status == 'canceled':
service_request.animal_set.update(status='CANCELED')
action.send(self.request.user, verb='updated service request', target=service_request)

if self.request.data.get('reunite_animals'):
service_request.animal_set.exclude(status='DECEASED').update(status='REUNITED', shelter=None, room=None)
for animal in service_request.animal_set.exclude(status='DECEASED'):
action.send(self.request.user, verb=f'changed animal status to reunited', target=animal)
service_request.status = 'closed'
service_request.save()
action.send(self.request.user, verb='closed service request', target=service_request)
else:
action.send(self.request.user, verb='updated service request', target=service_request)

def get_queryset(self):
queryset = (
Expand Down
14 changes: 12 additions & 2 deletions people/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -77,8 +77,18 @@ def perform_update(self, serializer):
if change_dict:
PersonChange.objects.create(user=self.request.user, person=person, changes=change_dict, reason=self.request.data.get('change_reason', ''))

# Record update action.
action.send(self.request.user, verb='updated person', target=person)
if self.request.data.get('reunite_animals'):
person.animal_set.exclude(status='DECEASED').update(status='REUNITED', shelter=None, room=None)
for animal in person.animal_set.exclude(status='DECEASED'):
action.send(self.request.user, verb=f'changed animal status to reunited', target=animal)
if person.request.exists():
service_request = person.request.first()
service_request.status = 'closed'
service_request.save()
action.send(self.request.user, verb='closed service request', target=service_request)
else:
# Record update action.
action.send(self.request.user, verb='updated person', target=person)

class OwnerContactViewSet(viewsets.ModelViewSet):

Expand Down

0 comments on commit d24a5b6

Please sign in to comment.