Skip to content

Commit

Permalink
autopilot modal updates
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalloor, Eric Abraham committed May 8, 2023
1 parent 5ffb502 commit cda86ee
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 37 deletions.
73 changes: 38 additions & 35 deletions register/templates/register/react_register.html
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
max-width: 80% !important;
}

/* The container */
.container {
/* The container-register */
.container-register {
display: block;
position: relative;
padding-left: 35px;
Expand All @@ -63,7 +63,7 @@
}

/* Hide the browser's default checkbox */
.container input {
.container-register input {
position: absolute;
opacity: 0;
cursor: pointer;
Expand All @@ -82,12 +82,12 @@
}

/* On mouse-over, add a grey background color */
.container:hover input ~ .checkmark {
.container-register:hover input ~ .checkmark {
background-color: #ccc;
}

/* When the checkbox is checked, add a blue background */
.container input:checked ~ .checkmark {
.container-register input:checked ~ .checkmark {
background-color: #2196F3;
}

Expand All @@ -99,12 +99,12 @@
}

/* Show the checkmark when checked */
.container input:checked ~ .checkmark:after {
.container-register input:checked ~ .checkmark:after {
display: block;
}

/* Style the checkmark/indicator */
.container .checkmark:after {
.container-register .checkmark:after {
left: 9px;
top: 5px;
width: 5px;
Expand Down Expand Up @@ -193,7 +193,6 @@
</style>

{% block content %}
<div>

<div id="report"></div>

Expand Down Expand Up @@ -546,7 +545,7 @@ <h5 className="mb-4">Morning Register</h5>
{this.state.data.m_register.map((entry, index) => {
return entry.status && (
<label key={index}
className="container autopilot-modal-content-mobile">{entry.name} | {entry.m_quantity} ML
className="container-register autopilot-modal-content-mobile">{entry.name} | {entry.m_quantity} ML
<input name={`${entry.id}-morning`}
type="checkbox" defaultChecked="checked"/>
<span className="checkmark"></span>
Expand All @@ -561,7 +560,7 @@ <h5 className="mb-4 text-success">Add Customer to
{this.state.data.autopilot_morning_register.map((entry, index) => {
return (
<label key={index}
className="container autopilot-modal-content-mobile text-success">
className="container-register autopilot-modal-content-mobile text-success">
{entry.name} | {entry.m_quantity} ML
<input name={`${entry.id}-morning`}
type="checkbox" defaultChecked="checked"/>
Expand All @@ -578,7 +577,7 @@ <h5 className="mb-4">Evening Register</h5>
{this.state.data.e_register.map((entry, index) => {
return entry.status && (
<label key={index}
className="container autopilot-modal-content-mobile">{entry.name} | {entry.e_quantity} ML
className="container-register autopilot-modal-content-mobile">{entry.name} | {entry.e_quantity} ML
<input name={`${entry.id}-evening`}
type="checkbox" defaultChecked="checked"/>
<span className="checkmark"></span>
Expand All @@ -593,7 +592,7 @@ <h5 className="mb-4 text-success">Add Customer to
{this.state.data.autopilot_evening_register.map((entry, index) => {
return (
<label key={index}
className="container autopilot-modal-content-mobile text-success">
className="container-register autopilot-modal-content-mobile text-success">
{entry.name} | {entry.e_quantity} ML
<input name={`${entry.id}-evening`}
type="checkbox" defaultChecked="checked"/>
Expand Down Expand Up @@ -726,37 +725,41 @@ <h4 className="text-danger text-center"> Please <a
await fetch('{% url 'view_add_entry' %}', requestOptions)
.then(response => response.json())
.then(data => {
const current_state = this.state.registerPage
if (data.return) {
const current_state = this.state.registerPage

// Update state
const updatedRegister = current_state.map((customer) => {
if (customer.id === data.customer_id) {
const existingEntry = customer.register_entry.find((entry) => entry.id === data.entry.id);

if (existingEntry) {
return {
...customer,
register_entry: customer.register_entry.map((entry) => {
if (entry.id === data.entry.id) {
return {...entry, ...data.entry};
}
return entry;
}),
};
}

// Update state
const updatedRegister = current_state.map((customer) => {
if (customer.id === data.customer_id) {
const existingEntry = customer.register_entry.find((entry) => entry.id === data.entry.id);

if (existingEntry) {
return {
...customer,
register_entry: customer.register_entry.map((entry) => {
if (entry.id === data.entry.id) {
return {...entry, ...data.entry};
}
return entry;
}),
register_entry: [...customer.register_entry, data.entry],
};
}

return {
...customer,
register_entry: [...customer.register_entry, data.entry],
};
}

return customer;
});
return customer;
});

this.setState({registerPage: updatedRegister});
this.setState({registerPage: updatedRegister});

show_toast('Entry Saved', `${data.quantity} saved for ${data.customer_name} on ${data.logDate}`, 'success');
show_toast('Entry Saved', `${data.quantity} saved for ${data.customer_name} on ${data.logDate}`, 'success');
} else {
show_toast('Error', `${data.message}`, 'danger');
}
})
.catch(error => {
if (error) {
Expand Down
8 changes: 6 additions & 2 deletions register/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,15 @@ def add_entry(request, year=None, month=None):
yes_or_no = 'yes' if int(attendance) else 'no'
schedule = request.POST.get("schedule", 'morning').lower()
full_schedule = f'{schedule.lower()}-{yes_or_no}'
quantity = form['quantity'].value() or False
quantity = int(form['quantity'].value()) or 0
current_price = tenant.milk_price
if quantity % 250:
return JsonResponse({
'return': False,
'message': f'{quantity} ML is not allowed. Quantity should be in multiple of 250 ML',
})

# check if entry exists for give day and schedule

entry = Register.objects.filter(tenant_id=request.user.id, customer_id=customer,
log_date=full_log_date,
schedule__startswith=schedule).first()
Expand Down

0 comments on commit cda86ee

Please sign in to comment.