Skip to content

Commit

Permalink
Merge branch 'calculators-constructor' into textarea-formula-field
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielVajnagi authored Dec 1, 2024
2 parents ba1ade1 + f934abb commit 2a74483
Show file tree
Hide file tree
Showing 62 changed files with 1,309 additions and 133 deletions.
56 changes: 53 additions & 3 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@ name: CI
on:
push:

pull_request:
branches:
- develop
- master
types:
- closed

release:
types: [published]

jobs:
rubocop:
runs-on: ubuntu-latest
Expand Down Expand Up @@ -50,16 +60,19 @@ jobs:
with:
ruby-version: 3.3.5
bundler-cache: true


- name: Update packages
run: sudo apt-get update

- name: Install system dependencies
run: |
sudo apt-get update
sudo apt-get install -y libvips42 libvips-dev imagemagick
- uses: actions/setup-node@v1
with:
node-version: '14.x'
registry-url: 'https://registry.npmjs.org'
node-version: "14.x"
registry-url: "https://registry.npmjs.org"

- uses: nanasess/setup-chromedriver@master

Expand All @@ -81,3 +94,40 @@ jobs:
token: ${{ secrets.GITHUB_TOKEN }}
json-path: tmp/rspec_results.json
if: always()

deploy-to-staging:
needs: rspec
runs-on: ubuntu-latest
if: github.event_name == 'pull_request' && github.event.action == 'closed' && github.event.pull_request.merged == true
steps:
- uses: actions/checkout@v2

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.3.5
bundler-cache: true

- uses: miloserdow/capistrano-deploy@v3
with:
target: staging
deploy_key: ${{ secrets.STAGING_KEY_PASSWORD }}
enc_rsa_key_pth: config/credentials/staging_deploy_id_ed25519_enc

deploy-to-production:
needs: rspec
runs-on: ubuntu-latest
if: github.event_name == 'release' && github.event.action == 'published'
steps:
- uses: actions/checkout@v2

- name: Set up Ruby
uses: ruby/setup-ruby@v1
with:
ruby-version: 3.3.5
bundler-cache: true

- uses: miloserdow/capistrano-deploy@v3
with:
target: production
deploy_key: ${{ secrets.PROD_DEPLOY_KEY }}
2 changes: 1 addition & 1 deletion .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ inherit_gem:
AllCops:
SuggestExtensions: true
NewCops: enable
TargetRubyVersion: 3.0.6
TargetRubyVersion: 3.2

Layout/SpaceInsideHashLiteralBraces:
Enabled: true
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ The latest version from the release branch 'master' is automatically deployed to
- Bootstrap

## Clone

$ `git clone https://github.com/ita-social-projects/ZeroWaste.git`

## Local setup
Expand Down
Binary file added app/assets/images/pad_scales.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/pads_bought.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added app/assets/images/pads_to_buy.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions app/assets/stylesheets/application.tailwind.css
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,6 @@
@import "/components/pagination";
@import "/components/breadcrumbs.scss";
@import "/components/description_block";
@import "/components/showpage_calculator";
@import "/utilities/custom-utilities";
@import "/pages/under_construction"
21 changes: 21 additions & 0 deletions app/assets/stylesheets/components/showpage_calculator.css
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;
}
}
27 changes: 27 additions & 0 deletions app/assets/stylesheets/pages/calculator.scss
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,28 @@
font-style: normal;
transition: transform 0.5s ease-in-out;
max-width: 300px;
@include transition(all 0.5s ease-in-out);

&:hover {
background-color: $matte_lime_green;
}
}

.btn-nonito {
font-size: 14px;
letter-spacing: 2px;
text-transform: uppercase;
font-weight: 400;
font-family: "Nunito", sans-serif;
font-style: normal;
}

.dynamic-text-color {
color: var(--calculator-color);
}

.dynamic-background-color {
background-color: var(--calculator-color);
}

#calc {
Expand Down Expand Up @@ -61,6 +83,11 @@
margin-right: 0px;
}

.calculator-field {
background-color: $light_gray !important;
border: 0;
}

.flex-item {
display: flex;
flex-wrap: wrap;
Expand Down
6 changes: 0 additions & 6 deletions app/assets/stylesheets/pages/feature_flags.scss
Original file line number Diff line number Diff line change
Expand Up @@ -41,19 +41,13 @@
}

input[type="submit"] {
background-color: $success;
border: none;
border-radius: 4px;
color: #fff;
cursor: pointer;
font-size: 16px;
padding: 10px;
min-width: 110px;
@include transition(all 0.5s ease-in-out);

&:hover {
background-color: $matte_lime_green;
}
}

.btn-grey {
Expand Down
9 changes: 7 additions & 2 deletions app/controllers/account/calculators_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,9 @@

class Account::CalculatorsController < Account::BaseController
load_and_authorize_resource
before_action :check_constructor_flipper

def index
render "shared/under_construction" unless Rails.env.local?

@q = collection.ransack(params[:q])
@calculators = @q.result.page(params[:page])
end
Expand Down Expand Up @@ -95,4 +94,10 @@ def updater
@calculator.update(calculator_params)
end
end

def check_constructor_flipper
return if Flipper[:constructor_status].enabled?

raise ActionController::RoutingError, "Constructor flipper is disabled"
end
end
11 changes: 11 additions & 0 deletions app/controllers/account/users_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class Account::UsersController < Account::BaseController
layout "account"

before_action :set_paper_trail_whodunnit
before_action :blocking_admin, only: :update

load_and_authorize_resource

Expand Down Expand Up @@ -71,6 +72,16 @@ def user_params
prms
end

def blocking_admin
@user = resource

return if params.dig(:user, :blocked).blank? || !@user.admin?

flash[:alert] = t("errors.messages.blocked_user_cannot_be_admin")

redirect_to account_users_path
end

def collection
User.ordered_by_email
end
Expand Down
20 changes: 20 additions & 0 deletions app/controllers/api/v1/pad_calculators_controller.rb
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
22 changes: 22 additions & 0 deletions app/controllers/calculators_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
class CalculatorsController < ApplicationController
before_action :authenticate_user!, only: :receive_recomendations

before_action :check_constructor_flipper, only: [:index, :show, :calculate]
before_action :check_mhc_flipper, only: :mhc_calculator

def index
if Flipper[:show_calculators_list].enabled?
@q = collection.ransack(params[:q])
Expand All @@ -14,6 +17,8 @@ def index

def show
@calculator = resource
add_breadcrumb t("breadcrumbs.home"), root_path
add_breadcrumb @calculator.name
end

def calculate
Expand All @@ -37,6 +42,11 @@ def calculator
end
end

def mhc_calculator
add_breadcrumb t("breadcrumbs.home"), root_path
add_breadcrumb t(".mhc_calculator.calculator_name")
end

def receive_recomendations
current_user.toggle(:receive_recomendations)
current_user.save
Expand All @@ -51,4 +61,16 @@ def collection
def resource
collection.friendly.find(params[:slug])
end

def check_constructor_flipper
return if Flipper[:constructor_status].enabled?

raise ActionController::RoutingError, "Constructor flipper is disabled"
end

def check_mhc_flipper
return if Flipper[:mhc_calculator_status].enabled?

raise ActionController::RoutingError, "Mhc calculator flipper is disabled"
end
end
9 changes: 9 additions & 0 deletions app/helpers/calculators_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ def link_to_external(text:, url:, **options)
end
end

def mhc_calculator_items
[{ image: "pads_bought.png", data_target: "padsUsed", unit: t(".pieces"), text: t(".bought_products") },
"arrow",
{ image: "pads_to_buy.png", data_target: "padsToBeUsed", unit: t(".pieces"), text: t(".will_buy_products") },
{ image: "money_spent_2.png", data_target: "moneySpent", unit: t(".unit"), text: t(".money_spent") },
"arrow",
{ image: "money_to_spent_2.png", data_target: "moneyWillBeSpent", unit: t(".unit"), text: t(".money_will_be_spent") }]
end

def new_calculator_items
[{ image: "diapers_bought_2.png", data_target: "diapersUsed", unit: t(".pieces"), text_target: "boughtDiapersPluralize", text: t(".bought_diapers", count: 0) },
"arrow",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Controller } from "@hotwired/stimulus"

// Connects to data-controller="constructors-form-indexing"
export default class extends Controller {
static targets = ["index"];

afterInsert(event) {
const fieldsets = this.element.querySelectorAll(":scope > .nested-fields");
const span = fieldsets[fieldsets.length - 1].querySelector("[data-constructors-form-indexing-target='index']")

if (span) {
span.textContent = `${fieldsets.length}`;
}
}

afterRemove(event) {
const fieldsets = this.element.querySelectorAll(":scope > .nested-fields");

fieldsets.forEach((fieldset, index) => {
const span = fieldset.querySelector("[data-constructors-form-indexing-target='index']");
if (span) {
span.textContent = `${index + 1}`;
}
});
}
}
Loading

0 comments on commit 2a74483

Please sign in to comment.