Skip to content

Commit

Permalink
Merge branch 'react-interaction'
Browse files Browse the repository at this point in the history
Pharmacy login with phone number now functional
  • Loading branch information
Newvick Lee committed May 13, 2018
2 parents 64bd887 + 50f2b04 commit 0994fed
Show file tree
Hide file tree
Showing 6 changed files with 244 additions and 47 deletions.
60 changes: 39 additions & 21 deletions backend/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,6 @@ def index():
return render_template('index.html')


@app.route('/login', methods=['GET', 'POST'])
def login():
return


@app.route('/api/v1/search', methods=['GET'])
@crossdomain(origin='*')
Expand Down Expand Up @@ -210,7 +206,7 @@ def set_available():
if req_newInStock:
for drug in req_newInStock:
# TODO: Get pharma_id from JSON
pharma_id = 215
pharma_id = 214
now = now_int()
drug_id = drug['drug_id']
by_pharmacist = 1
Expand All @@ -222,7 +218,7 @@ def set_available():
if req_noStock:
for drug in req_noStock:
# TODO: Get pharma_id from JSON
pharma_id = 215
pharma_id = 214
now = now_int()
drug_id = drug['drug_id']
by_pharmacist = 1
Expand Down Expand Up @@ -319,30 +315,52 @@ def pharmacy_onboarding():
return str(pharma_id)


@app.route('/api/v1/pharmacy/<int:pharma_id>', methods=['GET'])
def pharmacy_info(pharma_id):
@app.route('/api/v1/pharmacy/<int:phone>', methods=['GET'])
@crossdomain(origin='*')
def pharmacy_info(phone):
# get DB
db = get_db()
cur = db.cursor()

# get pharmacy info
doc = json.loads(cur.execute(
'SELECT doc FROM PharmaDoc WHERE oid = ?', (pharma_id,)).fetchone()[0])
doc['id'] = pharma_id

pharmacy = dict()
info = cur.execute('SELECT rowid AS pharma_id, * from PharmaDoc where phone = ?', (phone,)).fetchone()

if info == None:
""" If the pharmacy phone number doesn't exist, return an unsuccesful response message """
response = dict()
response['unsuccessful'] = "Sorry we could not find that pharmacy in our records."
return jsonify(response)

pharmacy['info'] = {
'pharma_id': info[0],
'name': info[1],
'address': info[2],
'phone': info[3],
'fax': info[4],
'manager': info[5],
'hours': info[6],
'latitude': info[7],
'longitude': info[8]
}
# get drug availability
cur.execute("""SELECT drug_id
FROM Availabilities
WHERE pharma_id = ?
AND availability = 1
ORDER BY when_reported
LIMIT 20""", (pharma_id,))
cur.execute("""SELECT Availabilities.drug_id, DrugNames.name
FROM Availabilities
INNER JOIN DrugNames on Availabilities.drug_id = DrugNames.drug_id
WHERE pharma_id = ?
AND availability = 1
ORDER BY when_reported
LIMIT 20""", (pharmacy['info']['pharma_id'],))
drugs = list()
for row in cur:
drugs.append(row[0])
doc['drugs'] = drugs
drug = {
'drug_id': row[0],
'name': row[1]
}
drugs.append(drug)
pharmacy['drugs'] = drugs

return jsonify(doc)
return jsonify(pharmacy)


@app.route('/api/v1/drug/<int:drug_id>', methods=['GET'])
Expand Down
5 changes: 3 additions & 2 deletions create_db.sql
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,9 @@ CREATE TABLE `PharmaLoc` (
CREATE TABLE `PharmaDoc` (
`name` TEXT NOT NULL,
`address` TEXT NOT NULL,
`phone` REAL NOT NULL,
`fax` REAL,
`phone` INTEGER NOT NULL,
`fax` INTEGER,
`hours` TEXT,
`manager` TEXT,
`latitude` REAL,
`longitude` REAL
Expand Down
147 changes: 126 additions & 21 deletions frontend/components/Pharmacy.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,54 +6,145 @@ class Pharmacy extends React.Component {
constructor(props) {
super(props);
this.state = {
logged_in: true
logged_in: false,
pharmacy_info: null,
pharmacy_drugs: null,
drug_requests: null

};

this.handleLogin = this.handleLogin.bind(this);
};

handleLogin(pharmacy_info, pharmacy_drugs) {
this.setState(function () {
return {
logged_in: true,
pharmacy_info: pharmacy_info,
pharmacy_drugs: pharmacy_drugs
}
});
};



render() {
let logged_in = this.state.logged_in;
let pharmacy_info = this.state.pharmacy_info;
let pharmacy_drugs = this.state.pharmacy_drugs;
return (
<div className="container-flex">
<div className="content-container">
{!logged_in &&
<Login />}
<Login
logged_in={logged_in}
handleLogin={this.handleLogin}
/>}
{logged_in &&
<Welcome />}
<Welcome
pharmacy_info={pharmacy_info}
/>}
{logged_in &&
<DrugsStock />}
<DrugStock
pharmacy_drugs={pharmacy_drugs}
/>}

</div>
</div>
)
};
};

function Notification (props) {
let error = props.error_msg;
return (
<div className="notification-container">
<div className="notification-card">
<div className="notification-error">
<h4 className="notification-title">{error}</h4>
<div className="notification-message"></div>
{/* TODO: Implement notification-dismiss control
<span className="notification-dismiss">x</span>*/}
</div>
</div>
</div>
)
};




class Login extends React.Component {
constructor(props) {
super(props);
this.state = {
phone: null,
error: null
};

this.handleLogin = this.handleLogin.bind(this);
this.handleChange = this.handleChange.bind(this);
};

handleChange(event) {
let phone = event.target.value;
this.setState(function () {
return { phone: phone }
});
};

handleLogin(event) {
event.preventDefault();
self = this;
let phone = this.state.phone;
api.logIn(phone)
.then(function (response) {
if (response.data.unsuccessful) {
self.setState(function () {
return {
error: response.data.unsuccessful
}
});
} else {
self.setState(function () {
return { error: null }
});
let pharmacy_info = response.data.info;
let pharmacy_drugs = response.data.drugs;
self.props.handleLogin(pharmacy_info, pharmacy_drugs);
};
});
};

render() {
let handleChange = this.handleChange;
let error = this.state.error;
return (
<div className="login-container">
<div className="postCard verticalCard fullWidthCard">
<h2 className="login-title">Login with your Pharmacy phone number*</h2>
<h2 className="login-title">Log in with your Pharmacy phone number*</h2>
<p className="asterisk-point">* Numbers only, no spaces or dashes</p>
<input className="login-input" autocomplete="off" placeholder="Phone Number" type="text" />
<button className="submit-button btn-blue" type="submit">Login</button>
<input className="login-input" autoComplete="off" placeholder="Phone Number" type="text" onChange={this.handleChange}/>
<button className="submit-button btn-blue" type="submit" onClick={this.handleLogin}>Login</button>
</div>
{error &&
<Notification
error_msg={error}
/>}
</div>
)
};
};

function Welcome (props) {
let name = props.pharmacy_info.name;
let address = props.pharmacy_info.address;
let phone = props.pharmacy_info.phone;
return (
<div className="column-container welcome-container">
<h1>Welcome Nazs Pharmacy</h1>
<p>6410 Main Street Vancouver, BC</p>
<p>6043253241</p>
<h1>{name}</h1>
<p>{address}</p>
<p>{phone}</p>
</div>
)
};
Expand All @@ -80,14 +171,14 @@ function DrugCheckbox (props) {
)
};

class DrugsStock extends React.Component {
class DrugStock extends React.Component {
constructor(props) {
super(props);
this.state = {
inStock: [],
newInStock: [],
noStock: [],
drugRequests: []
drugRequests: [],
};

this.createDrugCheckbox = this.createDrugCheckbox.bind(this);
Expand All @@ -97,12 +188,24 @@ class DrugsStock extends React.Component {
};

componentDidMount() {
/* Initializes the drugRequests and inStock state.
*
* Adds the default in_stock propery to each drug.
*
*/
let inStock = this.props.pharmacy_drugs;
let updatedInStock = this.addInStockState(inStock, true);




api.drugRequests()
.then(function (drugRequests) {
let updatedDrugRequests = this.addInStockState(drugRequests.data);
let updatedDrugRequests = this.addInStockState(drugRequests.data, false);
this.setState(function() {
return {
drugRequests: updatedDrugRequests
drugRequests: updatedDrugRequests,
inStock: updatedInStock
}
});
}.bind(this));
Expand Down Expand Up @@ -156,7 +259,7 @@ class DrugsStock extends React.Component {
inStock: inStock,
drugRequests: drugRequests,
noStock: noStock
}, function() { return console.log(this.state);});
});
};
if (drugRequests[index].in_stock == true) {
/* If user clicks a checkbox,
Expand All @@ -173,7 +276,7 @@ class DrugsStock extends React.Component {
inStock: inStock,
drugRequests: drugRequests,
noStock: noStock
}, function() { return console.log(this.state);});
});
};

};
Expand All @@ -199,16 +302,16 @@ class DrugsStock extends React.Component {
/>;
};

addInStockState(drugRequests) {
addInStockState(drugRequests, trueOrFalse) {
/*
* Adds the in_stock property for each drug in drugRequests, sets the default in_stock to false
* Adds the in_stock property for each drug in drugRequests
*
* @param {array of objects} drugRequests is the array that is returned from api.drugRequests()
*
* @returns {array of objects} Returns drugRequests but each drug object has a new property in_stock that defaults to false
* @returns {array of objects} Returns drugRequests but each drug object has a new property in_stock
*/
drugRequests.map(function (drug) {
drug.in_stock = false;
drug.in_stock = trueOrFalse;
});
return drugRequests;
};
Expand All @@ -217,7 +320,9 @@ class DrugsStock extends React.Component {

render() {
let createDrugCheckbox = this.createDrugCheckbox;
let drugCheckboxes = this.state.drugRequests.map(createDrugCheckbox);
let drugRequests = this.state.drugRequests.map(createDrugCheckbox);
let drugsInStock = this.state.inStock.map(createDrugCheckbox);
let drugCheckboxes = [].concat(drugsInStock, drugRequests);
let handleSubmit = this.handleSubmit;
return (
<div>
Expand Down
Loading

0 comments on commit 0994fed

Please sign in to comment.