UBER-278: add Yes-No to popup, refactor (#3289)

Signed-off-by: Vyacheslav Tumanov <me@slavatumanov.me>
This commit is contained in:
Vyacheslav Tumanov 2023-05-30 17:03:37 +05:00 committed by GitHub
parent 825ec100c0
commit e0ea9fd07f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 28 deletions

View File

@ -13,40 +13,42 @@
// limitations under the License. // limitations under the License.
--> -->
<script lang="ts"> <script lang="ts">
import { Card, createQuery } from '@hcengineering/presentation' import { Card } from '@hcengineering/presentation'
import { AccountRole, Doc, getCurrentAccount, Ref, SortingOrder } from '@hcengineering/core' import { AccountRole, Doc, getCurrentAccount, Ref } from '@hcengineering/core'
import view from '@hcengineering/view-resources/src/plugin' import view from '@hcengineering/view-resources/src/plugin'
import { createEventDispatcher } from 'svelte' import { createEventDispatcher } from 'svelte'
import contact, { Employee, EmployeeAccount } from '@hcengineering/contact' import { EmployeeAccount } from '@hcengineering/contact'
import EmployeePresenter from './EmployeePresenter.svelte'
import { employeeAccountByIdStore } from '../utils' import { employeeAccountByIdStore } from '../utils'
import ui, { Label } from '@hcengineering/ui' import ui, { Button, Label } from '@hcengineering/ui'
import EmployeeAccountRefPresenter from './EmployeeAccountRefPresenter.svelte'
import EmployeeAccountPresenter from './EmployeeAccountPresenter.svelte'
export let object: Doc | Doc[] export let object: Doc | Doc[]
export let deleteAction: () => void export let deleteAction: () => void
const objectArray = Array.isArray(object) ? object : [object] const objectArray = Array.isArray(object) ? object : [object]
let owners: Ref<Employee>[] = [] const owners: EmployeeAccount[] = Array.from($employeeAccountByIdStore.values()).filter(
const query = createQuery() (acc) => acc.role === AccountRole.Owner
query.query(
contact.class.EmployeeAccount,
{ role: AccountRole.Owner },
(res) => {
owners = res.map((account) => account.employee)
},
{
sort: { name: SortingOrder.Descending }
}
) )
const dispatch = createEventDispatcher() const dispatch = createEventDispatcher()
const creators = [ $: creators = [...new Set(objectArray.map((obj) => obj.createdBy as Ref<EmployeeAccount>))]
...new Set(objectArray.map((obj) => $employeeAccountByIdStore.get(obj.createdBy as Ref<EmployeeAccount>)?.employee)) $: canDelete =
] (creators.length === 1 && creators.includes(getCurrentAccount()._id as Ref<EmployeeAccount>)) ||
const me = $employeeAccountByIdStore.get(getCurrentAccount()._id as Ref<EmployeeAccount>)?.employee getCurrentAccount().role === AccountRole.Owner
const canDelete = (creators.length === 1 && creators.includes(me)) || (me && owners.includes(me)) $: label = canDelete ? view.string.DeleteObject : view.string.DeletePopupNoPermissionTitle
const label = canDelete ? view.string.DeleteObject : view.string.DeletePopupNoPermissionTitle
</script> </script>
<Card {label} okAction={deleteAction} canSave={canDelete} okLabel={ui.string.Ok} on:close={() => dispatch('close')}> <Card
{label}
okAction={deleteAction}
canSave={canDelete}
okLabel={canDelete ? view.string.LabelYes : ui.string.Ok}
on:close={() => dispatch('close')}
>
<svelte:fragment slot="buttons">
{#if canDelete}
<Button label={view.string.LabelNo} on:click={() => dispatch('close')} />
{/if}
</svelte:fragment>
<div class="flex-grow flex-col"> <div class="flex-grow flex-col">
{#if canDelete} {#if canDelete}
<div class="mb-2"> <div class="mb-2">
@ -58,17 +60,17 @@
</div> </div>
<div class="mb-2"> <div class="mb-2">
<Label label={view.string.DeletePopupCreatorLabel} /> <Label label={view.string.DeletePopupCreatorLabel} />
{#each creators as employee} {#each creators as account}
<div class="my-2"> <div class="my-2">
<EmployeePresenter value={employee} /> <EmployeeAccountRefPresenter value={account} />
</div> </div>
{/each} {/each}
</div> </div>
<div class="mb-2"> <div class="mb-2">
<Label label={view.string.DeletePopupOwnerLabel} /> <Label label={view.string.DeletePopupOwnerLabel} />
{#each owners as employee} {#each owners as owner}
<div class="my-2"> <div class="my-2">
<EmployeePresenter value={employee} /> <EmployeeAccountPresenter value={owner} />
</div> </div>
{/each} {/each}
</div> </div>

View File

@ -96,7 +96,7 @@ test.describe('contact tests', () => {
}) })
await page.click('text="Delete"') await page.click('text="Delete"')
// Click text=Ok // Click text=Ok
await page.click('text=Ok') await page.click('text=Yes')
await expect(page.locator(`td:has-text("${first} ${last}")`)).toHaveCount(0) await expect(page.locator(`td:has-text("${first} ${last}")`)).toHaveCount(0)
}) })