2023-05-26 09:11:10 -04:00
|
|
|
<script lang="ts">
|
2023-07-01 00:50:47 -04:00
|
|
|
import CircleIconButton from '$lib/components/elements/buttons/circle-icon-button.svelte';
|
|
|
|
|
import {
|
|
|
|
|
NotificationType,
|
|
|
|
|
notificationController,
|
|
|
|
|
} from '$lib/components/shared-components/notification/notification';
|
2023-08-16 16:04:55 -04:00
|
|
|
import { handleError } from '$lib/utils/handle-error';
|
2023-07-01 00:50:47 -04:00
|
|
|
import { api } from '@api';
|
|
|
|
|
import MenuOption from '../../shared-components/context-menu/menu-option.svelte';
|
2024-01-17 20:18:04 +01:00
|
|
|
import { getAssetControlContext } from '../asset-select-control-bar.svelte';
|
2023-10-25 09:48:25 -04:00
|
|
|
import { mdiArchiveArrowUpOutline, mdiArchiveArrowDownOutline, mdiTimerSand } from '@mdi/js';
|
2024-01-17 20:18:04 +01:00
|
|
|
import type { OnArchive } from '$lib/utils/actions';
|
2023-07-01 00:50:47 -04:00
|
|
|
|
2023-08-16 16:04:55 -04:00
|
|
|
export let onArchive: OnArchive | undefined = undefined;
|
2023-07-01 00:50:47 -04:00
|
|
|
|
|
|
|
|
export let menuItem = false;
|
|
|
|
|
export let unarchive = false;
|
|
|
|
|
|
|
|
|
|
$: text = unarchive ? 'Unarchive' : 'Archive';
|
2023-10-25 09:48:25 -04:00
|
|
|
$: icon = unarchive ? mdiArchiveArrowUpOutline : mdiArchiveArrowDownOutline;
|
2023-07-01 00:50:47 -04:00
|
|
|
|
2023-08-16 16:04:55 -04:00
|
|
|
let loading = false;
|
|
|
|
|
|
2023-11-11 15:06:19 -06:00
|
|
|
const { clearSelect, getOwnedAssets } = getAssetControlContext();
|
2023-07-01 00:50:47 -04:00
|
|
|
|
|
|
|
|
const handleArchive = async () => {
|
|
|
|
|
const isArchived = !unarchive;
|
2023-08-16 16:04:55 -04:00
|
|
|
loading = true;
|
2023-07-01 00:50:47 -04:00
|
|
|
|
2023-08-16 16:04:55 -04:00
|
|
|
try {
|
2024-02-02 04:18:00 +01:00
|
|
|
const assets = [...getOwnedAssets()].filter((asset) => asset.isArchived !== isArchived);
|
2023-08-16 16:04:55 -04:00
|
|
|
const ids = assets.map(({ id }) => id);
|
2023-07-01 00:50:47 -04:00
|
|
|
|
2023-08-16 16:04:55 -04:00
|
|
|
if (ids.length > 0) {
|
|
|
|
|
await api.assetApi.updateAssets({ assetBulkUpdateDto: { ids, isArchived } });
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const asset of assets) {
|
|
|
|
|
asset.isArchived = isArchived;
|
2023-07-01 00:50:47 -04:00
|
|
|
}
|
|
|
|
|
|
2023-08-16 16:04:55 -04:00
|
|
|
onArchive?.(ids, isArchived);
|
2023-07-01 00:50:47 -04:00
|
|
|
|
2023-08-16 16:04:55 -04:00
|
|
|
notificationController.show({
|
|
|
|
|
message: `${isArchived ? 'Archived' : 'Unarchived'} ${ids.length}`,
|
|
|
|
|
type: NotificationType.Info,
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
clearSelect();
|
|
|
|
|
} catch (error) {
|
|
|
|
|
handleError(error, `Unable to ${isArchived ? 'archive' : 'unarchive'}`);
|
|
|
|
|
} finally {
|
|
|
|
|
loading = false;
|
|
|
|
|
}
|
2023-07-01 00:50:47 -04:00
|
|
|
};
|
2023-05-26 09:11:10 -04:00
|
|
|
</script>
|
|
|
|
|
|
|
|
|
|
{#if menuItem}
|
2023-07-01 00:50:47 -04:00
|
|
|
<MenuOption {text} on:click={handleArchive} />
|
2023-08-16 16:04:55 -04:00
|
|
|
{/if}
|
|
|
|
|
|
|
|
|
|
{#if !menuItem}
|
|
|
|
|
{#if loading}
|
2023-10-25 09:48:25 -04:00
|
|
|
<CircleIconButton title="Loading" icon={mdiTimerSand} />
|
2023-08-16 16:04:55 -04:00
|
|
|
{:else}
|
2023-10-25 09:48:25 -04:00
|
|
|
<CircleIconButton title={text} {icon} on:click={handleArchive} />
|
2023-08-16 16:04:55 -04:00
|
|
|
{/if}
|
2023-05-26 09:11:10 -04:00
|
|
|
{/if}
|