web/src/components/things/SaveListing.svelte

127 lines · 2937 bytes

  1<script lang="ts">
  2  import { onMount } from 'svelte';
  3  import FontAwesome from '@components/form/FontAwesome.svelte';
  4  import { slugify } from '@utils/fetch-data';
  5
  6  interface Props {
  7    categoryName: string;
  8    sectionName: string;
  9    serviceName: string;
 10    showLabel?: boolean;
 11  }
 12  const {
 13    categoryName,
 14    sectionName,
 15    serviceName,
 16    showLabel = false,
 17  }: Props = $props();
 18
 19  const serviceRef = $derived(
 20    `${slugify(categoryName)}/${slugify(sectionName)}/${slugify(serviceName)}`,
 21  );
 22
 23  let isSaved = $state(false);
 24
 25  onMount(async () => {
 26    const stored = JSON.parse(localStorage.getItem('savedServices') || '[]');
 27    if (stored.includes(serviceRef)) {
 28      isSaved = true;
 29    }
 30  });
 31
 32  function toggleSave() {
 33    const stored = JSON.parse(localStorage.getItem('savedServices') || '[]');
 34    const index = stored.indexOf(serviceRef);
 35    if (index === -1) {
 36      stored.push(serviceRef);
 37      isSaved = true;
 38    } else {
 39      stored.splice(index, 1);
 40      isSaved = false;
 41    }
 42    localStorage.setItem('savedServices', JSON.stringify(stored));
 43  }
 44</script>
 45
 46<div class="wrapper-or-something">
 47  <button
 48    class={`save-container ${isSaved ? 'saved' : ''} ${showLabel ? 'label-button' : ''}`}
 49    title={`Save ${serviceName}`}
 50    onclick={toggleSave}
 51  >
 52    {#if showLabel}
 53      <span>
 54        {isSaved ? 'Saved' : 'Save'}
 55      </span>
 56    {/if}
 57    <FontAwesome iconName="saveListing" />
 58  </button>
 59
 60  {#if showLabel && isSaved}
 61    <div class="done-msg">
 62      You can view all saved items in your <a href="/inventory">Inventory</a>
 63    </div>
 64  {/if}
 65</div>
 66
 67<style lang="scss">
 68  .wrapper-or-something {
 69    display: flex;
 70    flex-direction: row-reverse;
 71    gap: var(--space-xs);
 72  }
 73  .save-container {
 74    cursor: pointer;
 75    background: none;
 76    border: none;
 77    display: flex;
 78    gap: var(--space-sm);
 79    align-items: center;
 80    transition: var(--transition-normal);
 81    span {
 82      font-size: var(--text-md);
 83      opacity: var(--opacity-subtle);
 84      color: var(--foreground);
 85      font-family: var(--font-subtitle);
 86    }
 87    :global(svg) {
 88      color: var(--foreground);
 89      width: 1.2rem;
 90      height: 1.2rem;
 91      opacity: var(--opacity-dim);
 92      transition: var(--transition-normal);
 93    }
 94    &:hover {
 95      :global(svg) {
 96        color: var(--accent-3);
 97        opacity: 1;
 98      }
 99    }
100    &.saved {
101      :global(svg) {
102        color: var(--accent-2);
103        opacity: 1;
104      }
105    }
106    &.label-button {
107      padding: 0.2rem 0.4rem;
108      border-radius: var(--curve-sm);
109      box-shadow: var(--shadow-sm);
110      border: var(--border-light);
111      background: var(--background-form);
112
113      &:hover {
114        box-shadow: var(--shadow-sm-hover);
115      }
116    }
117  }
118
119  .done-msg {
120    max-width: 165px;
121    font-size: var(--text-sm);
122    opacity: var(--opacity-muted);
123    @media (max-width: 768px) {
124      display: none;
125    }
126  }
127</style>