Implement global search results component for Protocol templates [SCI-10477]

This commit is contained in:
sboursen-scinote 2024-03-26 16:42:36 +01:00
parent b14113d1a5
commit 9c6dac339e
4 changed files with 100 additions and 3 deletions

View file

@ -13,10 +13,10 @@ class SearchController < ApplicationController
case params[:group]
when 'projects'
@project_search_count = fetch_cached_count Project
@project_search_count = fetch_cached_count(Project)
search_projects
if params[:preview] == 'true'
results = @project_results.limit(4)
results = @project_results&.limit(4) || []
else
results = @project_results.page(params[:page]).per(Constants::SEARCH_LIMIT)
end
@ -28,6 +28,22 @@ class SearchController < ApplicationController
next_page: (results.next_page if results.respond_to?(:next_page)),
}
return
when 'protocols'
@protocol_search_count = fetch_cached_count(Protocol)
search_protocols
results = if params[:preview] == 'true'
@protocol_results&.limit(4) || []
else
@protocol_results.page(params[:page]).per(Constants::SEARCH_LIMIT)
end
render json: results,
each_serializer: GlobalSearch::ProtocolSerializer,
meta: {
total: @search_count,
next_page: (results.next_page if results.respond_to?(:next_page)),
}
return
end
#@search_id = params[:search_id] ? params[:search_id] : generate_search_id

View file

@ -3,12 +3,56 @@
<h2 class="flex items-center gap-2 mt-0 mb-4">
<i class="sn-icon sn-icon-protocols-templates"></i>
{{ i18n.t('search.index.protocol_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.id') }}:</b>
<span class="shrink-0">{{ row.attributes.code }}</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 :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', 'ProtocolsComponent')">View all</button>
</div>
</div>
</div>
</template>
<script>
import searchMixin from './search_mixin';
export default {
name: 'ProtocolsComponent'
name: 'ProtocolsComponent',
mixins: [searchMixin],
data() {
return {
group: 'protocols'
};
}
};
</script>

View file

@ -0,0 +1,35 @@
# frozen_string_literal: true
module GlobalSearch
class ProtocolSerializer < ActiveModel::Serializer
include Rails.application.routes.url_helpers
attributes :id, :name, :code, :created_at, :updated_at, :created_by, :team, :archived, :url
def team
{
name: object.team.name,
url: protocols_path(team: object.team)
}
end
def created_by
{
name: object.created_by.name,
avatar_url: avatar_path(object.created_by, :icon_small)
}
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
protocol_path(object)
end
end
end

View file

@ -457,6 +457,8 @@ en:
clear_filters: "Clear filters"
id: "ID"
created_at: "Created on"
created_by: "Created by"
updated_at: "Updated on"
team: "Team"
folder: "Folder"
comments: