generated from ita-social-projects/DevTemplate
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #985 from ita-social-projects/update_for_constructor
Develop into calculators constructor base branch
- Loading branch information
Showing
41 changed files
with
940 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
@layer components { | ||
.main-show-container { | ||
@apply flex flex-col bg-white rounded-lg shadow-md w-full p-6 text-left mt-4; | ||
} | ||
|
||
.back-arrow { | ||
@apply rounded mb-4 px-2 flex items-center; | ||
} | ||
|
||
.calc-details { | ||
@apply flex flex-col mb-4 px-2; | ||
} | ||
|
||
.showpage-buttons { | ||
@apply flex justify-start w-full space-x-4; | ||
} | ||
|
||
.showpage-text { | ||
@apply text-slate-600 text-sm; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
class Api::V1::PadCalculatorsController < ApplicationController | ||
def calculate | ||
@validation = MhcCalculatorValidator.new(params) | ||
|
||
if @validation.valid? | ||
calc_service = Calculators::PadUsageService.new( | ||
user_age: params[:user_age], | ||
menstruation_age: params[:menstruation_age], | ||
menopause_age: params[:menopause_age], | ||
average_menstruation_cycle_duration: params[:average_menstruation_cycle_duration], | ||
pads_per_cycle: params[:pads_per_cycle], | ||
pad_category: params[:pad_category] | ||
) | ||
|
||
render json: calc_service.calculate, status: :ok | ||
else | ||
render json: { errors: @validation.errors }, status: :unprocessable_entity | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
import { Controller } from "@hotwired/stimulus"; | ||
import { FetchRequest } from "@rails/request.js"; | ||
|
||
export default class extends Controller { | ||
static targets = ["userAge", "menstruationAge", "menopauseAge", "averageMenstruationCycleDuration", "padsPerCycle", "padCategory"]; | ||
static outlets = ["pad-results"]; | ||
static values = { | ||
url: { | ||
type: String, | ||
default: "en/api/v1/pad_calculators", | ||
} | ||
}; | ||
|
||
submit(e) { | ||
e.preventDefault(); | ||
|
||
let formData = { | ||
user_age: parseInt(this.userAgeTarget.value), | ||
menstruation_age: parseInt(this.menstruationAgeTarget.value), | ||
menopause_age: parseInt(this.menopauseAgeTarget.value), | ||
average_menstruation_cycle_duration: parseInt(this.averageMenstruationCycleDurationTarget.value), | ||
pads_per_cycle: parseInt(this.padsPerCycleTarget.value), | ||
pad_category: this.padCategoryTarget.value | ||
}; | ||
|
||
const request = new FetchRequest("POST", this.urlValue, { | ||
responseKind: "json", | ||
body: JSON.stringify(formData), | ||
}); | ||
|
||
this.sendRequest(request); | ||
} | ||
|
||
async sendRequest(request) { | ||
const response = await request.perform(); | ||
const result = await response.json; | ||
|
||
this.clearErrors() | ||
|
||
if (response.ok) { | ||
this.padResultsOutlet.showResults(result); | ||
} else if (response.statusCode == 422) { | ||
this.showErrors(result.errors); | ||
} | ||
} | ||
|
||
showErrors(errors) { | ||
Object.keys(errors).forEach(errorKey => { | ||
const targetKey = errorKey.replace(/_([a-z])/g, (match, letter) => letter.toUpperCase()); | ||
const feedbackDiv = document.createElement('div'); | ||
|
||
feedbackDiv.className = 'invalid-feedback'; | ||
feedbackDiv.textContent = errors[errorKey]; | ||
|
||
this[`${targetKey}Target`].classList.add("is-invalid"); | ||
this[`${targetKey}Target`].insertAdjacentElement('afterend', feedbackDiv); | ||
}); | ||
} | ||
|
||
clearErrors(){ | ||
this.constructor.targets.forEach(targetKey => { | ||
const targetElement = this[`${targetKey}Target`]; | ||
targetElement.classList.remove("is-invalid"); | ||
|
||
const feedbackDiv = targetElement.nextElementSibling; | ||
if (feedbackDiv && feedbackDiv.classList.contains('invalid-feedback')) { | ||
feedbackDiv.remove(); | ||
} | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
|
||
export default class extends Controller { | ||
static targets = [ | ||
"padsUsed", | ||
"padsToBeUsed", | ||
"moneySpent", | ||
"moneyWillBeSpent" | ||
]; | ||
|
||
showResults(data) { | ||
let result = data; | ||
|
||
this.moneySpentTarget.innerHTML = Math.ceil(result.already_used_products_cost); | ||
this.moneyWillBeSpentTarget.innerHTML = Math.ceil(result.products_to_be_used_cost); | ||
this.padsUsedTarget.innerHTML = Math.ceil(result.already_used_products); | ||
this.padsToBeUsedTarget.innerHTML = Math.ceil(result.products_to_be_used); | ||
|
||
this.element.scrollIntoView({ behavior: "smooth" }); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
class Calculators::PadUsageService | ||
attr_accessor :user_age, :menstruation_age, :menopause_age, | ||
:average_menstruation_cycle_duration, | ||
:pads_per_cycle, :pad_category | ||
|
||
PAD_PRICES = { | ||
budget: 2, | ||
average: 4, | ||
premium: 7 | ||
} | ||
|
||
def initialize(user_age:, menstruation_age:, menopause_age:, average_menstruation_cycle_duration:, | ||
pads_per_cycle:, pad_category:) | ||
@user_age = user_age | ||
@menstruation_age = menstruation_age | ||
@menopause_age = menopause_age || 48.7 | ||
@average_menstruation_cycle_duration = average_menstruation_cycle_duration | ||
@pads_per_cycle = pads_per_cycle | ||
@pad_category = (pad_category || :budget).to_sym | ||
end | ||
|
||
def calculate | ||
{ | ||
already_used_products:, | ||
already_used_products_cost:, | ||
products_to_be_used:, | ||
products_to_be_used_cost: | ||
} | ||
end | ||
|
||
private | ||
|
||
def already_used_products | ||
menstruations_from_age_range(menstruation_age, user_age) * pads_per_cycle | ||
end | ||
|
||
def products_to_be_used | ||
menstruations_from_age_range(user_age, menopause_age) * pads_per_cycle | ||
end | ||
|
||
def already_used_products_cost | ||
already_used_products * PAD_PRICES[pad_category] | ||
end | ||
|
||
def products_to_be_used_cost | ||
products_to_be_used * PAD_PRICES[pad_category] | ||
end | ||
|
||
def menstruations_from_age_range(from_age, till_age) | ||
(till_age - from_age) * (365 / average_menstruation_cycle_duration) | ||
end | ||
end |
Oops, something went wrong.