Skip to content

Commit

Permalink
Merge pull request #223 from TreinaDev/feat/protege-rotas-premium
Browse files Browse the repository at this point in the history
Adiciona opção de assinatura e protege rotas pagas
  • Loading branch information
Luckvc authored Feb 15, 2024
2 parents 449e065 + 93aae3f commit 0f941aa
Show file tree
Hide file tree
Showing 24 changed files with 396 additions and 75 deletions.
2 changes: 1 addition & 1 deletion app/assets/stylesheets/navbar.scss
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@
}

.dropdown-item:active {
background-color: #9030df !important;
background-color: #9030df !important;
}
18 changes: 11 additions & 7 deletions app/controllers/invitation_requests_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,22 @@ class InvitationRequestsController < ApplicationController
before_action :authenticate_user!, only: %i[index]

def index
invitation_requests = current_user.invitation_requests
@error = false
@invitation_request_infos = []
return if current_user.subscription.inactive?

begin
@invitation_request_infos = InvitationRequestService::InvitationRequest.send(invitation_requests)
rescue StandardError
return @error = true
end
@error = false
request_data

return @invitation_request_infos if params[:filter].blank?

@invitation_request_infos.filter! { |request| request.status == params[:filter] }
end

private

def request_data
@invitation_request_infos = InvitationRequestService::InvitationRequest.send(current_user.invitation_requests)
rescue StandardError
@error = true
end
end
8 changes: 8 additions & 0 deletions app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
class ProjectsController < ApplicationController
before_action :authenticate_user!
before_action :authenticate_subscriber, only: :create_invitation_request

def index
@invitation_request = current_user.invitation_requests.build
@invitation_requests = current_user.invitation_requests.pluck(:project_id).to_json
@invitation_requests_projects_ids = current_user.invitation_requests.pluck(:project_id)
@free_user = current_user.subscription.inactive?
end

def create_invitation_request
Expand All @@ -19,6 +21,12 @@ def create_invitation_request

private

def authenticate_subscriber
return if current_user.subscription.active?

redirect_to root_path, alert: t('alerts.unauthorized')
end

def invitation_request_params
invitation_request_params = params.require(:invitation_request).permit(:message)
invitation_request_params['project_id'] = params['project_id']
Expand Down
13 changes: 13 additions & 0 deletions app/controllers/subscriptions_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class SubscriptionsController < ApplicationController
before_action :authenticate_user!

def index
@subscription = current_user.subscription
end

def update
@subscription = Subscription.find params[:id]
@subscription.active!
redirect_to subscriptions_path, notice: t('.success')
end
end
31 changes: 17 additions & 14 deletions app/javascript/components/projects_vue.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export default {
showingForm: false,
currentProjectId: null,
invitationRequestsProjectsIds: window.invitationRequestsProjectsIds,
freeUser: window.freeUser,
errorMsg: false,
}
},
Expand Down Expand Up @@ -44,21 +45,23 @@ export default {
},

async created() {
try {
let response = await fetch('/api/v1/projects', { signal });
if (response.ok) {
let data = await response.json();
if (!data.message) {
this.projects = data;
if (!freeUser) {
try {
let response = await fetch('/api/v1/projects', { signal });
if (response.ok) {
let data = await response.json();
if (!data.message) {
this.projects = data;
}
} else {
this.errorMsg = true;
}
} catch (error) {
if (error.name == 'AbortError') {
console.log('Requisição abortada');
} else {
this.errorMsg = true;
}
} else {
this.errorMsg = true;
}
} catch (error) {
if (error.name == 'AbortError') {
console.log('Requisição abortada');
} else {
this.errorMsg = true;
}
}
}
Expand Down
14 changes: 14 additions & 0 deletions app/models/subscription.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Subscription < ApplicationRecord
belongs_to :user
enum status: { inactive: 0, active: 10 }

def active!
self.start_date = Time.zone.now.to_date
super
end

def inactive!
self.start_date = nil
super
end
end
2 changes: 2 additions & 0 deletions app/models/user.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ class User < ApplicationRecord
devise :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
has_one :profile, dependent: :destroy
has_one :subscription, dependent: :destroy
has_many :posts, dependent: :nullify
has_many :likes, dependent: :destroy
has_many :comments, dependent: :nullify
Expand All @@ -24,6 +25,7 @@ class User < ApplicationRecord
after_create :'create_profile!'
after_create :subscribe_likes_mailer_job
after_create :update_search_name
after_create :create_subscription

def description
if admin?
Expand Down
2 changes: 1 addition & 1 deletion app/views/home/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
<%= t('.find_connect_shine') %> <br>
<span> <%= t('.your_profile_your_space') %> </span><br>
<span class="text-primary fw-bold py-3"><%= t('.become_part_of_comunity') %></span> <br>
<%= link_to t('.create_account'), new_user_registration_path, class: 'btn btn-primary col-10 py-2 my-4 fs-4' %>
<%= link_to t('.create_account_btn'), new_user_registration_path, class: 'btn btn-primary col-10 py-2 my-4 fs-4' %>
</h3>
</div>
</div>
Expand Down
84 changes: 45 additions & 39 deletions app/views/invitation_requests/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,48 +1,54 @@
<h2 class="text-center mb-5"><%= InvitationRequest.model_name.human(count: @invitation_request_infos.length) %></h2>

<div class="container">
<div class="row justify-content-center gap-5">
<%= form_with url: invitation_requests_path, method: :get, class: 'col-10 me-5 px-0' do |f| %>
<div class="d-flex gap-2 col-3">
<%= f.label :filter, class: 'd-none' %>
<%= f.select :filter,
invitation_request_filter_options,
{ include_blank: 'Todas', selected: params[:filter] },
class: 'form-select' %>
<%= f.submit t(:filter_btn ), class: 'btn btn-sm btn-primary' %>
</div>
<% end %>
<% if current_user.subscription.inactive? %>
<div class="text-center become-premium-div mt-5">
<h2 class="become-premium-text"><%= t('subscriptions.become') %> <%= link_to t('subscriptions.subscriber'), subscriptions_path %> <%= t('subscriptions.see_and_request') %></h2>
</div>
<% else %>
<div class="container">
<div class="row justify-content-center gap-5">
<%= form_with url: invitation_requests_path, method: :get, class: 'col-10 me-5 px-0' do |f| %>
<div class="d-flex gap-2 col-3">
<%= f.label :filter, class: 'd-none' %>
<%= f.select :filter,
invitation_request_filter_options,
{ include_blank: 'Todas', selected: params[:filter] },
class: 'form-select' %>
<%= f.submit t(:filter_btn ), class: 'btn btn-sm btn-primary' %>
</div>
<% end %>
<% if @invitation_request_infos.any? %>
<% @invitation_request_infos.each do |invitation_request| %>
<div class="col-5 rounded border p-3" id="request_<%= invitation_request.id %>">
<div class="card-body">
<div class="d-flex flex-column gap-4">
<div class="d-flex flex-column gap-2">
<h4 class="text-break text-dark m-0"><%= invitation_request.project_title %></h4>
<div class="d-flex flex-column gap-2 align-items-start">
<p class="m-0"><%= invitation_request.project_description %></p>
<p class="m-0 fs-sm badge text-bg-secondary"><%= invitation_request.project_category %></p>
<% if @invitation_request_infos.any? %>
<% @invitation_request_infos.each do |invitation_request| %>
<div class="col-5 rounded border p-3" id="request_<%= invitation_request.id %>">
<div class="card-body">
<div class="d-flex flex-column gap-4">
<div class="d-flex flex-column gap-2">
<h4 class="text-break text-dark m-0"><%= invitation_request.project_title %></h4>
<div class="d-flex flex-column gap-2 align-items-start">
<p class="m-0"><%= invitation_request.project_description %></p>
<p class="m-0 fs-sm badge text-bg-secondary"><%= invitation_request.project_category %></p>
</div>
</div>
<div class="d-flex justify-content-between align-items-center">
<p class="m-0 text-muted">
<%= t(:time_ago, time: time_ago_in_words(invitation_request.created_at)) %>
</p>
<p class="m-0 text-center <%= css_color_class(invitation_request.status.to_sym) %> fw-bold fs-5">
<%= InvitationRequest.human_attribute_name("status.#{invitation_request.status}") %>
</p>
</div>
</div>
<div class="d-flex justify-content-between align-items-center">
<p class="m-0 text-muted">
<%= t(:time_ago, time: time_ago_in_words(invitation_request.created_at)) %>
</p>
<p class="m-0 text-center <%= css_color_class(invitation_request.status.to_sym) %> fw-bold fs-5">
<%= InvitationRequest.human_attribute_name("status.#{invitation_request.status}") %>
</p>
</div>
</div>
</div>
</div>
<% end %>
<% elsif @error %>
<h3 class="mt-5 alert alert-warning text-center" ><%= t(:project_api_error) %></h3>
<% elsif params[:filter].nil? || params[:filter].blank? %>
<h3 class="mt-5 alert alert-warning text-center" ><%= t(:no_invitation_request_msg) %></h3>
<% else %>
<h3 class="mt-5 alert alert-warning text-center" ><%= t(:no_filter_results_msg) %></h3>
<% end %>
<% elsif @error %>
<h3 class="mt-5 alert alert-warning text-center" ><%= t(:project_api_error) %></h3>
<% elsif params[:filter].nil? || params[:filter].blank? %>
<h3 class="mt-5 alert alert-warning text-center" ><%= t(:no_invitation_request_msg) %></h3>
<% else %>
<h3 class="mt-5 alert alert-warning text-center" ><%= t(:no_filter_results_msg) %></h3>
<% end %>
</div>
</div>
</div>
<% end %>
9 changes: 7 additions & 2 deletions app/views/projects/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<div class="container" id="vue-projects-app">
<h2 class="text-center"><%= t('.index_title') %> </h2>

<div v-if="emptyData" class="text-center">
<div v-if="freeUser" class="text-center become-premium-div mt-5">
<h2 class="become-premium-text"><%= t('subscriptions.become') %> <%= link_to t('subscriptions.subscriber'), subscriptions_path %> <%= t('subscriptions.see_listed_projects') %></h2 class="become-premium-text">
</div>

<div v-else-if="emptyData" class="text-center">
<p>{{ emptyData }}</p>
</div>

Expand Down Expand Up @@ -40,7 +44,7 @@
<h3 v-else-if="projects.length == 0 && !errorMsg" class="mt-5 alert alert-warning text-center">
<%= t('.errors.no_result') %>
</h3>

<div v-else>
<div class="mt-5 border-top w-50 mx-auto" v-for="project in filteredProjects" :key="project.id">
<h3 class="mt-3">{{ project.title }}</h3>
Expand Down Expand Up @@ -71,4 +75,5 @@

<script>
var invitationRequestsProjectsIds = <%= @invitation_requests_projects_ids %>
var freeUser = <%= @free_user %>
</script>
16 changes: 10 additions & 6 deletions app/views/shared/_navbar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -33,15 +33,9 @@
<li class="dropdown-item" href="#">
<%= link_to "#{current_user.description}", current_user.profile, class: 'nav-link' %>
</li>
<li class="dropdown-item">
<%= link_to t('projects.model.other'), projects_path, class: 'nav-link' %>
</li>
<li class="dropdown-item">
<%= link_to Invitation.model_name.human(count: 2), invitations_path, class: 'nav-link' %>
</li>
<li class="dropdown-item">
<%= link_to 'Solicitações de Convites', invitation_requests_path, class: 'nav-link' %>
</li>
<li class="dropdown-item">
<%= link_to notifications_path, class: 'nav-link' do %>
<%= Notification.model_name.human(count: 2) %>
Expand All @@ -67,6 +61,16 @@
<li class="dropdown-item" href="#">
<%= link_to t('settings.index.settings'), profile_settings_path(current_user.profile), class: 'nav-link' %>
</li>
<div class="dropdown-divider"></div>
<li class="dropdown-item" href="#">
<%= link_to Subscription.model_name.human, subscriptions_path, class: 'nav-link text-primary' %>
</li>
<li class="dropdown-item">
<%= link_to t('projects.model.other'), projects_path, class: 'nav-link text-primary' %>
</li>
<li class="dropdown-item">
<%= link_to InvitationRequest.model_name.human(count: 2), invitation_requests_path, class: 'nav-link text-primary' %>
</li>
<li class="dropdown-item" href="#">
<%= button_to t('log_out'), destroy_user_session_path, method: :delete, class: 'btn btn-danger w-100' %>
</li>
Expand Down
46 changes: 46 additions & 0 deletions app/views/subscriptions/index.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
<h2 class="text-center"><%= Subscription.model_name.human %></h2>

<div class="container justify-items-center text-center mt-5">
<div class="d-flex row gap-5 justify-content-center">
<div class="card column col-4 shadow">
<div id="free-account-div" class="card-body d-flex flex-column justify-content-between">
<div>
<h3 class="card-title"><%= t('.free_account') %></h3>

<ul class="list-group">
<li class="list-group-item"><%= t('.platform_access') %></li>
<li class="list-group-item"><%= t('.searchable_content') %></li>
<li class="list-group-item"><%= t('.hiring_capabilities') %></li>
</ul>
</div>

<% if current_user.subscription.inactive? %>
<div class="card-footer">
<h4><%= t('.current_tier') %></h4>
</div>
<% end %>
</div>
</div>

<div class="card column col-4 shadow">
<div id="premium-account-div" class="card-body d-flex flex-column justify-content-between">
<h3 class="card-title"><%= t('.premium_account') %></h3>

<ul class="list-group">
<li class="list-group-item"><%= t('.projects_access') %></li>
<li class="list-group-item"><%= t('.highlight_in_search_results')%></li>
<li class="list-group-item"><%= t('.request_invitation') %></li>
<li class="list-group-item"><%= t('.no_ads') %></li>
</ul>

<% if current_user.subscription.inactive? %>
<%= button_to t('.subscribe_btn'), subscription_path(current_user.subscription), method: :patch, class: "btn btn-primary btn-lg mt-4" %>
<% else %>
<div class="card-footer">
<h4><%= t('.current_tier') %></h4>
</div>
<% end %>
</div>
</div>
</div>
</div>
6 changes: 3 additions & 3 deletions config/locales/home.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ pt-BR:
find_connect_shine: Descubra, Conecte, Brilhe
your_profile_your_space: Seu Portfólio, Seu Espaço
become_part_of_comunity: Faça parte da nossa comunidade!
create_account: Crie já a sua conta
find_out_more:
create_account_btn: Crie já a sua conta
find_out_more:
do: Faça
or: ou
or: ou
find_out: para descobrir conteúdos relevantes para você.
login: login
create_account: crie uma conta
Loading

0 comments on commit 0f941aa

Please sign in to comment.