Fix Vacancies search blinking (#1566)

Signed-off-by: Andrey Sobolev <haiodo@gmail.com>
This commit is contained in:
Andrey Sobolev 2022-04-27 21:29:11 +07:00 committed by GitHub
parent ca2b5ba0f3
commit 13834e6c07
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -14,10 +14,10 @@
-->
<script lang="ts">
import contact from '@anticrm/contact'
import core, { Doc, DocumentQuery, Lookup, Ref, WithLookup } from '@anticrm/core'
import core, { Doc, DocumentQuery, Lookup, Ref } from '@anticrm/core'
import { createQuery } from '@anticrm/presentation'
import { Applicant, Vacancy } from '@anticrm/recruit'
import { Button, getCurrentLocation, Icon, Label, navigate, Scroller, showPopup, IconAdd } from '@anticrm/ui'
import { Vacancy } from '@anticrm/recruit'
import { Button, getCurrentLocation, Icon, IconAdd, Label, navigate, Scroller, showPopup } from '@anticrm/ui'
import SearchEdit from '@anticrm/ui/src/components/SearchEdit.svelte'
import { Table } from '@anticrm/view-resources'
import recruit from '../plugin'
@ -31,67 +31,38 @@
}
let search: string = ''
let vquery: string = ''
let resultQuery: DocumentQuery<Doc> = {}
let vacancyQuery: DocumentQuery<Doc> = {}
let vacancies: WithLookup<Vacancy>[] = []
const query = createQuery()
let appQuery = false
const lookup: Lookup<Vacancy> = {
company: contact.class.Organization
}
$: query.query(recruit.class.Vacancy, { archived: false }, (res) => {
vacancies = res
}, { lookup })
$: resultQuery = search === '' ? {} : { $search: search }
function lowerIncludes (a: string | undefined, b: string): boolean {
return (a ?? '').toLowerCase().includes(b.toLowerCase())
}
$: if (vacancies.length > 0 && !appQuery) {
vacancyQuery = {
_id: {
$in: vacancies
.filter(
(it) =>
lowerIncludes(it.name, vquery) ||
lowerIncludes(it.description, vquery) ||
lowerIncludes(it.$lookup?.company?.name, vquery) ||
(applications?.get(it._id) ?? 0) > 0
)
.map((it) => it._id)
}
}
}
$: resultQuery = vquery === '' ? {} : { $search: vquery }
type ApplicationInfo = { count: number, modifiedOn: number }
let applications: Map<Ref<Vacancy>, ApplicationInfo> | undefined
const applicantQuery = createQuery()
$: if (vacancies.length > 0) {
appQuery = true
applicantQuery.query(
recruit.class.Applicant,
{ ...(resultQuery as DocumentQuery<Applicant>), space: { $in: vacancies.map((it) => it._id) } },
(res) => {
const result = new Map<Ref<Vacancy>, ApplicationInfo>()
$: applicantQuery.query(
recruit.class.Applicant, { },
(res) => {
const result = new Map<Ref<Vacancy>, ApplicationInfo>()
for (const d of res) {
const v = result.get(d.space) ?? { count: 0, modifiedOn: 0 }
v.count++
v.modifiedOn = Math.max(v.modifiedOn, d.modifiedOn)
result.set(d.space, v)
}
applications = result
appQuery = false
for (const d of res) {
const v = result.get(d.space) ?? { count: 0, modifiedOn: 0 }
v.count++
v.modifiedOn = Math.max(v.modifiedOn, d.modifiedOn)
result.set(d.space, v)
}
)
}
applications = result
}, {
projection: {
_id: 1,
modifiedOn: 1,
space: 1
}
}
)
function showCreateDialog () {
showPopup(CreateVacancy, { space: recruit.space.CandidatesPublic }, 'top')
@ -107,8 +78,8 @@
</div>
<SearchEdit
bind:value={search}
on:change={() => {
vquery = search
on:change={(e) => {
search = e.detail
}}
/>
<Button icon={IconAdd} label={recruit.string.VacancyCreateLabel} kind={'primary'} on:click={showCreateDialog} />
@ -128,7 +99,7 @@
key: '',
presenter: recruit.component.VacancyCountPresenter,
label: recruit.string.Applications,
props: { applications, resultQuery },
props: { applications },
sortingKey: '@applications',
sortingFunction: applicationSorting
},
@ -148,7 +119,7 @@
lookup
}}
query={{
...vacancyQuery,
...resultQuery,
archived: false
}}
showNotification