Skip to content

Commit

Permalink
add views method
Browse files Browse the repository at this point in the history
  • Loading branch information
pixel365 committed Jul 10, 2024
1 parent 668f966 commit 54c5466
Show file tree
Hide file tree
Showing 6 changed files with 127 additions and 17 deletions.
24 changes: 14 additions & 10 deletions internal/src/ts/event_listeners.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ const opRequest = (action: "run" | "cancel" | "stop" | "increase_off", csrfmiddl
return success
}

const changeRequest = (el: HTMLInputElement, action: string, value: number, csrfmiddlewaretoken: string): boolean => {
const changeRequest = (el: HTMLInputElement, action: string, value: number,
csrfmiddlewaretoken: string, callback: Function | null): boolean => {
let success = false
el.disabled = true
el.classList.add("readonly-input")
Expand All @@ -52,6 +53,9 @@ const changeRequest = (el: HTMLInputElement, action: string, value: number, csrf
success = response.ok
if (success) {
response.json().then((data: ITask) => {
if (callback instanceof Function) {
callback()
}
toastr.success(`Change request completed successfully!<br />Task Id: ${data.id}`)
})
}
Expand Down Expand Up @@ -467,7 +471,7 @@ export const eventListeners = () => {
}

if (minutes > 0) {
changeRequest(smoothPeriod, Events.IncreaseOn, minutes, ev.detail.csrfmiddlewaretoken)
changeRequest(smoothPeriod, Events.IncreaseOn, minutes, ev.detail.csrfmiddlewaretoken, null)
}
}
}) as EventListener)
Expand All @@ -476,7 +480,7 @@ export const eventListeners = () => {
if (ev.detail.value > 0) {
const el = <HTMLInputElement>document.getElementById("orderDetailViewers")
if (el) {
changeRequest(el, Events.ChangeOnlineValue, ev.detail.value, ev.detail.csrfmiddlewaretoken)
changeRequest(el, Events.ChangeOnlineValue, ev.detail.value, ev.detail.csrfmiddlewaretoken, null)
}
}
}) as EventListener)
Expand All @@ -485,19 +489,19 @@ export const eventListeners = () => {
if (ev.detail.value > 0) {
const el = <HTMLInputElement>document.getElementById("orderDetailSmoothPeriod")
if (el) {
changeRequest(el, Events.ChangeIncreaseValue, ev.detail.value, ev.detail.csrfmiddlewaretoken)
changeRequest(el, Events.ChangeIncreaseValue, ev.detail.value, ev.detail.csrfmiddlewaretoken, null)
}
}
}) as EventListener)

window.addEventListener(Events.AddViews, ((ev: CustomEvent) => {
if (ev.detail.value > 0) {
const el = <HTMLInputElement>document.getElementById("orderDetailAddViews")
if (el) {
const success = changeRequest(el, Events.AddViews, ev.detail.value, ev.detail.csrfmiddlewaretoken)
if (success) {
//todo
}
const el = <HTMLInputElement>document.getElementById("orderDetailAddViews"),
closeBtn = <HTMLButtonElement>document.getElementById("closeAddViewsModal")
if (el && closeBtn) {
changeRequest(el, Events.AddViews, ev.detail.value, ev.detail.csrfmiddlewaretoken, () => {
closeBtn.click()
})
}
}
}) as EventListener)
Expand Down
30 changes: 30 additions & 0 deletions internal/src/ts/order_actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ export const orderActions = () => {
smoothGain = <HTMLInputElement>document.getElementById("orderDetailSmoothGain"),
smoothPeriod = <HTMLInputElement>document.getElementById("orderDetailSmoothPeriod"),
delayPeriod = <HTMLInputElement>document.getElementById("orderDetailDelayLaunchPeriod"),
addViews = <HTMLInputElement>document.getElementById("orderDetailAddViews"),
addViewsTmp = <HTMLElement>document.getElementById("addViewsTmp"),
action = <HTMLButtonElement>document.getElementById("orderDetailAction"),
cancel = <HTMLButtonElement>document.getElementById("orderDetailCancel"),
csrfmiddlewaretoken = <HTMLInputElement>document.querySelector(`input[name="csrfmiddlewaretoken"]`)
Expand Down Expand Up @@ -80,6 +82,34 @@ export const orderActions = () => {
}
}

if (addViews) {
if (addViewsTmp) {
addViews.onkeyup = () => {
const value = parseInt(addViews.value),
price = addViewsTmp.dataset.price
if (isNaN(value) || value < 1) {
addViewsTmp.textContent = "0"
} else {
if (price) {
addViewsTmp.textContent = (value * parseFloat(price)).toFixed(2).toString()
} else {
addViewsTmp.textContent = "0"
}
}
}
}

addViews.onchange = () => {
const value = parseInt(addViews.value)
window.dispatchEvent(new CustomEvent(Events.AddViews, {
detail: {
value: isNaN(value) ? 0 : value,
csrfmiddlewaretoken: csrfmiddlewaretoken.value
}
}))
}
}

if (action) {
action.onclick = () => {
const a = action.dataset.action
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
{% load i18n %}

{% if can_edit %}
<div class="mt-1 mb-3">
<button type="button" class="btn btn-sm btn-primary"
data-bs-toggle="modal" data-bs-target="#addViewsModal">
{% trans "Add views to order" %}
</button>
<div class="modal fade" id="addViewsModal" tabindex="-1"
style="display: none;" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="addViewsModalTitle">
{% trans "Add View to Order" %}
</h5>
<button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
</div>
<div class="modal-body">
<div class="row">
<div class="col mb-3">
<label for="orderDetailAddViews" class="form-label">
{% trans "Number of Views" %}
</label>
<input type="number" id="orderDetailAddViews" class="form-control"
placeholder="{% trans "Number of Views" %}"
value="1000">
</div>
</div>
{% if perms.core.can_view_prices %}
<div class="row g-2">
<div class="alert alert-secondary" role="alert">
<div class="d-flex w-100 flex-wrap align-items-center justify-content-between gap-2">
<div>{% trans "Cost" %}</div>
<div>
<h5 class="mb-1">
<i class="bx bx-{{ currency }}"></i>
<span id="addViewsTmp" data-price="{{ order.tariff.details.price }}">
{% widthratio order.tariff.details.price 1 1000 %}
</span>
</h5>
<h5 class="mb-0">
<small class="text-muted">
<i class="bx bx-{{ currency }}"></i>
{{ order.tariff.details.price }}
{% trans "per view" %}
</small>
</h5>
</div>
</div>
</div>
</div>
{% endif %}
</div>
<div class="modal-footer">
<button type="button" id="closeAddViewsModal"
class="btn btn-outline-secondary"
data-bs-dismiss="modal">
{% trans "Cancel" %}
</button>
<button type="button" class="btn btn-primary">
{% trans "Add" %}
</button>
</div>
</div>
</div>
</div>
</div>
{% endif %}
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,25 @@ <h5 class="display-6">
</h5>
<span class="display-6"><i class="bx bx-{{ currency }}"></i></span>
<span class="display-5">{{ order.cost }}</span>
<h5 class="mb-0">
<small class="text-muted">
<i class="bx bx-{{ currency }}"></i>
{{ order.tariff.details.price }}
{% trans "per view" %}
</small>
</h5>
</div>
</div>
{% endif %}

{% include "internal/include/content/order_details/_add_views.html" %}

<div class="row">
<label class="form-label mt-0">
{% trans "Balance on order" %}
</label>
<div class="d-flex w-100 flex-wrap align-items-center justify-content-between gap-2">
<div>{% trans "View" %}</div>
<div>{% trans "Views" %}</div>
<div>{{ order.views_balance }}</div>
</div>
{% if perms.core.can_view_prices %}
Expand Down
7 changes: 2 additions & 5 deletions internal/views/order_details.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
from django.http import HttpRequest
from django.http.response import (
HttpResponse as HttpResponse,
HttpResponseForbidden,
JsonResponse,
)
from django.urls import reverse_lazy
Expand Down Expand Up @@ -86,9 +85,7 @@ def get_context_data(self, **kwargs: Any) -> dict[str, Any]:
def get(self, request: HttpRequest, *args: Any, **kwargs: Any) -> HttpResponse:
return super(OrderDetailsView, self).get(request, *args, **kwargs)

def post(
self, request: HttpRequest, *args: Any, **kwargs: Any
) -> JsonResponse | HttpResponseForbidden:
def post(self, request: HttpRequest, *args: Any, **kwargs: Any) -> JsonResponse:
user = self.request.user
is_superuser = user.is_superuser
can_edit = is_superuser or self.object.user == user
Expand All @@ -106,7 +103,7 @@ def post(
]

if not user.has_perms(edit_perms):
return HttpResponseForbidden()
raise PermissionDenied

client = RxClient()
body = json.loads(request.body.decode())
Expand Down
2 changes: 1 addition & 1 deletion static/internal/script.js

Large diffs are not rendered by default.

0 comments on commit 54c5466

Please sign in to comment.