All files / src/views/favorites FavoriteLocationsTab.tsx

3.7% Statements 5/135
100% Branches 0/0
0% Functions 0/1
3.7% Lines 5/135

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 1721x 1x 1x 1x 1x                                                                                                                                                                                                                                                                                                                                              
import { useAuth } from '@/contexts/AuthContext';
import { useTranslation } from 'react-i18next';
import { useFavoriteLocationsViewModel } from '@/viewmodels/favorites/useFavoriteLocationsViewModel';
import { formatEventLocation } from '@/utils/eventLocation';
import '@/styles/favorites.css';
 
export default function FavoriteLocationsTab() {
  const { t } = useTranslation();
  const { token } = useAuth();
  const vm = useFavoriteLocationsViewModel(token);
 
  return (
    <div className="fav-loc">
      {/* Header with count + add button */}
      <div className="fav-loc-header">
        <span className="fav-loc-count">
          {t('favorites.locations_count', { count: vm.locations.length, max: vm.maxLocations })}
        </span>
        <button
          type="button"
          className="fav-loc-add-btn"
          onClick={vm.openAddModal}
          disabled={!vm.canAddMore}
        >
          {t('favorites.add_location')}
        </button>
      </div>
 
      {/* Limit reached info */}
      {!vm.canAddMore && !vm.isLoading && (
        <div className="fav-loc-limit-banner">
          {t('favorites.limit_reached', { count: vm.maxLocations })}
        </div>
      )}
 
      {/* Loading */}
      {vm.isLoading && (
        <div className="me-loading">
          <span className="spinner" />
          <p>{t('favorites.loading_locations')}</p>
        </div>
      )}
 
      {/* Error */}
      {vm.error && (
        <div className="me-error">
          <p>{vm.error}</p>
          <button type="button" className="me-retry-btn" onClick={vm.retry}>
            {t('common.retry')}
          </button>
        </div>
      )}
 
      {/* Empty state */}
      {!vm.isLoading && !vm.error && vm.locations.length === 0 && (
        <div className="me-empty">
          <p>{t('favorites.empty_locations', { count: vm.maxLocations })}</p>
        </div>
      )}
 
      {/* Location cards */}
      {!vm.isLoading && !vm.error && vm.locations.length > 0 && (
        <div className="fav-loc-list">
          {vm.locations.map((loc) => (
            <div key={loc.id} className="fav-loc-card">
              <div className="fav-loc-card-icon">📍</div>
              <div className="fav-loc-card-info">
                <h3 className="fav-loc-card-name">{loc.name}</h3>
                <p className="fav-loc-card-address">{loc.address}</p>
              </div>
              <button
                type="button"
                className="fav-loc-card-remove"
                onClick={() => vm.handleRemove(loc.id)}
                disabled={vm.removingId === loc.id}
                aria-label={t('favorites.remove_location_aria', { name: loc.name })}
              >
                {vm.removingId === loc.id ? <span className="spinner" /> : '×'}
              </button>
            </div>
          ))}
        </div>
      )}
 
      {/* Add location modal */}
      {vm.showAddModal && (
        <div className="fav-loc-modal-overlay" onClick={vm.closeAddModal}>
          <div className="fav-loc-modal" onClick={(e) => e.stopPropagation()}>
            <h2 className="fav-loc-modal-title">{t('favorites.add_location_title')}</h2>
 
            {vm.addError && (
              <div className="fav-loc-modal-error">{vm.addError}</div>
            )}
 
            <div className="fav-loc-modal-field">
              <label className="fav-loc-modal-label" htmlFor="loc-name">{t('favorites.location_name')}</label>
              <input
                id="loc-name"
                type="text"
                className="field-input"
                placeholder={t('favorites.location_name_placeholder')}
                maxLength={64}
                value={vm.addName}
                onChange={(e) => vm.setAddName(e.target.value)}
                disabled={vm.isSubmitting}
              />
            </div>
 
            <div className="fav-loc-modal-field">
              <label className="fav-loc-modal-label" htmlFor="loc-search">{t('favorites.search_address')}</label>
              <div className="fav-loc-search-wrapper">
                <input
                  id="loc-search"
                  type="text"
                  className="field-input"
                  placeholder={t('favorites.search_address_placeholder')}
                  value={vm.addQuery}
                  onChange={(e) => vm.handleSearchChange(e.target.value)}
                  disabled={vm.isSubmitting}
                />
                {vm.isSearching && (
                  <span className="fav-loc-searching">{t('common.searching')}</span>
                )}
                {vm.addSuggestions.length > 0 && (
                  <ul className="fav-loc-suggestions">
                    {vm.addSuggestions.map((s, i) => (
                      <li key={i}>
                        <button
                          type="button"
                          className="fav-loc-suggestion-item"
                          onClick={() => vm.selectSuggestion(s)}
                        >
                          {formatEventLocation(s.display_name)}
                        </button>
                      </li>
                    ))}
                  </ul>
                )}
              </div>
            </div>
 
            {vm.selectedSuggestion && (
              <div className="fav-loc-selected">
                📍 {t('favorites.selected_location', { location: formatEventLocation(vm.selectedSuggestion.display_name) })}
              </div>
            )}
 
            <div className="fav-loc-modal-actions">
              <button
                type="button"
                className="fav-loc-modal-cancel"
                onClick={vm.closeAddModal}
                disabled={vm.isSubmitting}
              >
                {t('common.cancel')}
              </button>
              <button
                type="button"
                className="fav-loc-modal-save"
                onClick={vm.handleAdd}
                disabled={vm.isSubmitting || !vm.addName.trim() || !vm.selectedSuggestion}
              >
                {vm.isSubmitting ? <span className="spinner" /> : t('favorites.save_location')}
              </button>
            </div>
          </div>
        </div>
      )}
    </div>
  );
}