|
| 1 | +<script lang="ts"> |
| 2 | + import Sortable, { type Options } from 'sortablejs'; |
| 3 | + import SortableList from '../input/SortableList.svelte'; |
| 4 | + import { isEqual } from 'lodash-es'; |
| 5 | + import getArrayDifference from '$lib/utilities/arrayOrder'; |
| 6 | + import graphql from '$lib/utilities/api'; |
| 7 | + import { success } from '$lib/utilities/toasts'; |
| 8 | +
|
| 9 | + interface Props { |
| 10 | + images: any[]; |
| 11 | + } |
| 12 | +
|
| 13 | + const { images }: Props = $props(); |
| 14 | +
|
| 15 | + let originalOrder = $state(images.map((x: any) => `${x.mainImageConfig.id}`)); |
| 16 | + // svelte-ignore state_referenced_locally |
| 17 | + let updatedElements = $state($state.snapshot(originalOrder)); |
| 18 | +
|
| 19 | + let sortable: Sortable | undefined = $state(); |
| 20 | + const sortableOptions: Options = { |
| 21 | + draggable: '.image-item', |
| 22 | + chosenClass: 'item-chosen', |
| 23 | + ghostClass: 'item-ghost', |
| 24 | + animation: 150, |
| 25 | + dataIdAttr: 'data-imageId', |
| 26 | + onEnd: () => (updatedElements = sortable!.toArray()) |
| 27 | + }; |
| 28 | +
|
| 29 | + let saveEnabled: boolean = $derived(!isEqual(originalOrder, updatedElements)); |
| 30 | +
|
| 31 | + async function updateImagesOrder() { |
| 32 | + const query = ` |
| 33 | + mutation updateMainPageConfig($id: Int!, $index: Int!) { |
| 34 | + updateMainPageConfig(id: $id, index: $index) { |
| 35 | + id |
| 36 | + } |
| 37 | + } |
| 38 | + `; |
| 39 | +
|
| 40 | + const updates = getArrayDifference(originalOrder, updatedElements); |
| 41 | +
|
| 42 | + updates.forEach(async ({ id, newPos }) => { |
| 43 | + await graphql(query, { id: parseInt(id), index: newPos }); |
| 44 | + }); |
| 45 | +
|
| 46 | + originalOrder = updatedElements; |
| 47 | +
|
| 48 | + success('Orden actualizado con éxito.'); |
| 49 | + } |
| 50 | +</script> |
| 51 | + |
| 52 | +<SortableList bind:sortable sortableId="sortable-main-page" {sortableOptions}> |
| 53 | + <div |
| 54 | + id="sortable-main-page" |
| 55 | + class="grid grid-cols-3 lg:grid-cols-5 gap-5 items-center w-full overflow-y-scroll overflow-x-hidden images-container" |
| 56 | + > |
| 57 | + {#each images as image (image.filename)} |
| 58 | + <a |
| 59 | + href={`/proyectos/${image.space.project.id}/ambientes/${image.space.id}/imagenes/${image.filename}`} |
| 60 | + class="image-item transition-all border-vector-orange hover:border-4 relative" |
| 61 | + data-imageId={image.mainImageConfig.id} |
| 62 | + > |
| 63 | + <img src={image.imageUrl} alt={image.altTextEs} /> |
| 64 | + <div |
| 65 | + class="absolute top-0 left-0 text-yellow-500 bg-vector-black/70 text-2xl px-1 pb-0.5 flex gap-x-2 items-start" |
| 66 | + > |
| 67 | + {#if image.space.project.thumbnail?.filename === image.filename} |
| 68 | + <div title="Imagen principal">⭐</div> |
| 69 | + {/if} |
| 70 | + {#if image.mainPage} |
| 71 | + <div title="Imagen en página principal">🏠</div> |
| 72 | + {/if} |
| 73 | + {#if image.sculpture} |
| 74 | + <div title="Imagen es una escultura">🖼️</div> |
| 75 | + {/if} |
| 76 | + </div> |
| 77 | + </a> |
| 78 | + {/each} |
| 79 | + <div |
| 80 | + id="cancel-images-main-page" |
| 81 | + class={`${saveEnabled ? 'opacity-100' : 'opacity-0'} transition-all flex flex-col items-center gap-1`} |
| 82 | + > |
| 83 | + <button |
| 84 | + class="w-fit text-xl p-1 rounded-md text-green-500 hover:bg-green-500 hover:text-black" |
| 85 | + onclick={updateImagesOrder} |
| 86 | + > |
| 87 | + ✓ |
| 88 | + </button> |
| 89 | + <button |
| 90 | + type="button" |
| 91 | + onclick={() => { |
| 92 | + sortable?.sort(originalOrder!); |
| 93 | + updatedElements = originalOrder; |
| 94 | + |
| 95 | + let buttons = document.getElementById('cancel-images-main-page')!; |
| 96 | + |
| 97 | + sortable?.el.appendChild(buttons); |
| 98 | + }} |
| 99 | + class="w-fit text-xl text-red-500 p-1 rounded-md hover:text-black hover:bg-red-500 transition-colors cursor-pointer" |
| 100 | + > |
| 101 | + X |
| 102 | + </button> |
| 103 | + </div> |
| 104 | + </div> |
| 105 | +</SortableList> |
0 commit comments