aboutsummaryrefslogtreecommitdiff
blob: f9b365ba7df51477c030f3733b997ab5bdd73d4b (plain)
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
import { GetStaticProps } from 'next';
import Head from 'next/head';
import dynamic from 'next/dynamic';
import { useEffect, useState } from 'react';

import { Trip } from 'models';
import getTripsData from 'lib/trips';
import Sidebar from 'components/Sidebar/Sidebar';
import Gallery from 'components/Gallery/Gallery';
import styles from 'styles/Home.module.css';

interface Props {
  allTripsData: Trip[];
}

const Map = dynamic(() => import('components/Map/Map'), { ssr: false });

export default function Home({ allTripsData }: Props): JSX.Element {
  const [currentTrip, setCurrentTrip] = useState(allTripsData[0]);
  const [currentPhoto, setCurrentPhoto] = useState(null);
  const [asideOpen, setAsideOpen] = useState(false);

  useEffect(() => {
    if (window.location.hash.length === 0) {
      window.location.hash = currentTrip.name;
    } else {
      const index = allTripsData.findIndex((t) => t.name === window.location.hash.substr(1));
      if (index > -1) setCurrentTrip(allTripsData[index]);
    }
  }, []);

  useEffect(() => {
    window.location.hash = currentTrip.name;
  }, [currentTrip]);

  function handleMarkerClick(photo: string) {
    const index = currentTrip.photos.findIndex((p) => p.name === photo);
    setCurrentPhoto(index);
  }

  function handlePhotoChange(direction: boolean): void {
    if (direction) {
      if (currentTrip.photos.length > currentPhoto + 1) {
        setCurrentPhoto(currentPhoto + 1);
      }
    } else if (currentPhoto - 1 >= 0) {
      setCurrentPhoto(currentPhoto - 1);
    }
  }

  return (
    <div className={styles.container}>
      <Head>
        <title>Trip Share</title>
        <link rel="icon" href="/favicon.ico" />
      </Head>

      {currentPhoto !== null && (
        <Gallery
          currentPhoto={currentTrip.photos[currentPhoto]}
          handleClose={() => setCurrentPhoto(null)}
          handlePhotoChange={handlePhotoChange}
        />
      )}

      <Sidebar
        trips={allTripsData}
        currentTrip={currentTrip}
        asideOpen={asideOpen}
        handleClose={() => setAsideOpen(false)}
        setCurrentTrip={setCurrentTrip}
      />

      <button type="button" className={styles.asideToggle} onClick={() => setAsideOpen(true)}>
        |||
      </button>

      <main className={styles.main}>
        <Map trip={currentTrip} handleMarkerClick={handleMarkerClick} />
      </main>
    </div>
  );
}

export const getStaticProps: GetStaticProps = async () => {
  const allTripsData = getTripsData();

  return {
    props: {
      allTripsData,
    },
  };
};