Implement global search results component for Label templates [SCI-10478]

This commit is contained in:
sboursen-scinote 2024-03-27 12:41:30 +01:00
parent 5b7ca1e49d
commit da19a79851
6 changed files with 149 additions and 6 deletions

View file

@ -16,7 +16,7 @@ class SearchController < ApplicationController
@project_search_count = fetch_cached_count(Project)
search_projects
if params[:preview] == 'true'
results = @project_results&.limit(4) || []
results = @project_results.take(4) || []
else
results = @project_results.page(params[:page]).per(Constants::SEARCH_LIMIT)
end
@ -61,7 +61,7 @@ class SearchController < ApplicationController
@protocol_search_count = fetch_cached_count(Protocol)
search_protocols
results = if params[:preview] == 'true'
@protocol_results&.limit(4) || []
@protocol_results.take(4) || []
else
@protocol_results.page(params[:page]).per(Constants::SEARCH_LIMIT)
end
@ -70,8 +70,26 @@ class SearchController < ApplicationController
each_serializer: GlobalSearch::ProtocolSerializer,
meta: {
total: @search_count,
next_page: (results.next_page if results.respond_to?(:next_page)),
}
next_page: (results.next_page if results.respond_to?(:next_page))
}
return
when 'label_templates'
return render json: [], meta: { disabled: true }, status: :ok unless LabelTemplate.enabled?
@label_template_search_count = fetch_cached_count(LabelTemplate)
search_label_templates
results = if params[:preview] == 'true'
@label_template_results.take(4) || []
else
@label_template_results.page(params[:page]).per(Constants::SEARCH_LIMIT)
end
render json: results,
each_serializer: GlobalSearch::LabelTemplateSerializer,
meta: {
total: @search_count,
next_page: (results.next_page if results.respond_to?(:next_page))
}
return
end
@ -343,6 +361,12 @@ class SearchController < ApplicationController
@search_count = @protocol_search_count
end
def search_label_templates
@label_template_results = []
@label_template_results = search_by_name(LabelTemplate) if @label_template_search_count.positive?
@search_count = @label_template_search_count
end
def search_steps
@step_results = []
@step_results = search_by_name(Step) if @step_search_count.positive?

View file

@ -1,14 +1,69 @@
<template>
<div class="bg-white rounded p-4 mb-4">
<div class="bg-white rounded p-4 mb-4" v-if="!disabled">
<h2 class="flex items-center gap-2 mt-0 mb-4">
<i class="sn-icon sn-icon-label-templates"></i>
{{ i18n.t('search.index.label_templates') }}
[{{ total }}]
</h2>
<div>
<div class="grid grid-cols-[auto_110px_auto_auto_auto_auto] items-center">
<template v-for="row in preparedResults" :key="row.id">
<a :href="row.attributes.url" class="h-full py-2 px-4 overflow-hidden font-bold border-0 border-b border-solid border-sn-light-grey">
<StringWithEllipsis class="w-full" :text="row.attributes.name"></StringWithEllipsis>
</a>
<div class="h-full py-2 px-4 flex items-center gap-1 text-xs border-0 border-b border-solid border-sn-light-grey">
<b class="shrink-0">{{ i18n.t('search.index.format') }}:</b>
<span class="shrink-0">{{ row.attributes.format }}</span>
</div>
<div class="h-full py-2 px-4 flex items-center gap-1 text-xs border-0 border-b border-solid border-sn-light-grey">
<b class="shrink-0">{{ i18n.t('search.index.created_at') }}:</b>
<span class="shrink-0">{{ row.attributes.created_at }}</span>
</div>
<div class="h-full py-2 px-4 flex items-center gap-1 text-xs border-0 border-b border-solid border-sn-light-grey">
<b class="shrink-0">{{ i18n.t('search.index.updated_at') }}:</b>
<span class="shrink-0">{{ row.attributes.updated_at }}</span>
</div>
<div class="h-full py-2 px-4 flex items-center gap-1 text-xs border-0 border-b border-solid border-sn-light-grey">
<b class="shrink-0">{{ i18n.t('search.index.created_by') }}:</b>
<img v-if="row.attributes.created_by.avatar_url" :src="row.attributes.created_by.avatar_url" class="w-5 h-5 border border-sn-super-light-grey rounded-full mx-1" />
<span class="shrink-0">{{ row.attributes.created_by.name }}</span>
</div>
<div class="h-full py-2 px-4 grid grid-cols-[auto_1fr] items-center gap-1 text-xs border-0 border-b border-solid border-sn-light-grey">
<b class="shrink-0">{{ i18n.t('search.index.team') }}:</b>
<a :href="row.attributes.team.url" class="shrink-0 overflow-hidden">
<StringWithEllipsis class="w-full" :text="row.attributes.team.name"></StringWithEllipsis>
</a>
</div>
</template>
</div>
<div v-if="!selected && total > 4" class="mt-4">
<button class="btn btn-light" @click="$emit('selectGroup', 'LabelTemplatesComponent')">View all</button>
</div>
</div>
</div>
</template>
<script>
import searchMixin from './search_mixin';
export default {
name: 'LabelTemplatesComponent'
name: 'LabelTemplatesComponent',
mixins: [searchMixin],
data() {
return {
group: 'label_templates',
enabled: false,
};
},
created() {
this.checkEnabled();
},
methods: {
checkEnabled() {
if (!this.enabled) {
this.$emit('selectGroup', 'LabelTemplatesComponent');
}
}
}
};
</script>

View file

@ -17,6 +17,7 @@ export default {
total: 0,
loading: false,
page: 1,
disabled: false,
fullDataLoaded: false
};
},
@ -80,6 +81,7 @@ export default {
if (this.selected) this.fullDataLoaded = true;
this.results = this.results.concat(response.data.data);
this.total = response.data.meta.total;
this.disabled = response.data.meta.disabled;
this.loading = false;
this.page = response.data.meta.next_page;
})

View file

@ -7,6 +7,9 @@ class LabelTemplate < ApplicationRecord
belongs_to :created_by, class_name: 'User', optional: true
belongs_to :last_modified_by, class_name: 'User', optional: true
SEARCHABLE_ATTRIBUTES = ['label_templates.name',
'label_templates.description'].freeze
enum unit: { in: 0, mm: 1 }
validates :name, presence: true, length: { minimum: Constants::NAME_MIN_LENGTH,
@ -21,6 +24,25 @@ class LabelTemplate < ApplicationRecord
ApplicationSettings.instance.values['label_templates_enabled'] == true
end
def self.search(
user = nil,
include_archived = nil,
query = nil,
page = 1,
current_team = nil,
options = {}
)
new_query = LabelTemplate.where_attributes_like(SEARCHABLE_ATTRIBUTES, query, options)
# Show all results if needed
if page == Constants::SEARCH_NO_LIMIT
new_query
else
new_query.limit(Constants::SEARCH_LIMIT).offset((page - 1) * Constants::SEARCH_LIMIT)
end
end
def icon
'zpl'
end

View file

@ -0,0 +1,39 @@
# frozen_string_literal: true
module GlobalSearch
class LabelTemplateSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers
attributes :id, :name, :format, :created_at, :updated_at, :created_by, :team, :url
def team
{
name: object.team.name,
url: label_templates_path(team: object.team)
}
end
def created_by
{
name: object.created_by ? object.created_by.name : object.created_by_user,
avatar_url: object.created_by ? avatar_path(object.created_by, :icon_small) : nil
}
end
def format
object.label_format
end
def created_at
I18n.l(object.created_at, format: :full_date)
end
def updated_at
I18n.l(object.updated_at, format: :full_date)
end
def url
label_template_path(object)
end
end
end

View file

@ -452,6 +452,7 @@ en:
inventory_items: "Inventory items"
protocol_templates: "Protocol templates"
label_templates: "Label templates"
format: "Format"
reports: "Reports"
more_search_options: "More search options"
clear_filters: "Clear filters"