2020-09-20 15:18:35 +00:00
|
|
|
import React, { useEffect, useState } from 'react';
|
2020-09-12 21:53:41 +00:00
|
|
|
import { useRouter } from 'next/router';
|
2020-09-19 21:20:10 +00:00
|
|
|
import Spinner from 'react-bootstrap/Spinner';
|
|
|
|
import { getKey, SESSION_KEYS } from 'utils/storage/sessionStorage';
|
2020-11-07 11:10:49 +00:00
|
|
|
import { file, getFiles } from 'services/fileService';
|
2020-09-19 21:20:10 +00:00
|
|
|
import { getData, LS_KEYS } from 'utils/storage/localStorage';
|
|
|
|
import PreviewCard from './components/PreviewCard';
|
|
|
|
import { getActualKey } from 'utils/common/key';
|
2020-09-20 15:18:35 +00:00
|
|
|
import styled from 'styled-components';
|
|
|
|
import { PhotoSwipeGallery } from 'react-photoswipe';
|
|
|
|
|
|
|
|
const Container = styled.div`
|
|
|
|
max-width: 1260px;
|
|
|
|
display: flex;
|
|
|
|
flex-wrap: wrap;
|
|
|
|
margin: 0 auto;
|
|
|
|
|
|
|
|
.pswp-thumbnail {
|
|
|
|
display: inline-block;
|
|
|
|
cursor: pointer;
|
|
|
|
}
|
|
|
|
`;
|
2020-09-12 21:53:41 +00:00
|
|
|
|
|
|
|
export default function Gallery() {
|
|
|
|
const router = useRouter();
|
2020-09-19 21:20:10 +00:00
|
|
|
const [loading, setLoading] = useState(false);
|
2020-11-07 11:10:49 +00:00
|
|
|
const [data, setData] = useState<file[]>();
|
2020-09-12 21:53:41 +00:00
|
|
|
|
2020-09-20 15:18:35 +00:00
|
|
|
useEffect(() => {
|
2020-09-13 06:30:07 +00:00
|
|
|
const key = getKey(SESSION_KEYS.ENCRYPTION_KEY);
|
2020-09-19 21:20:10 +00:00
|
|
|
const token = getData(LS_KEYS.USER).token;
|
2020-09-13 06:30:07 +00:00
|
|
|
if (!key) {
|
2020-09-12 21:53:41 +00:00
|
|
|
router.push("/");
|
|
|
|
}
|
2020-09-19 21:20:10 +00:00
|
|
|
const main = async () => {
|
|
|
|
setLoading(true);
|
|
|
|
const encryptionKey = await getActualKey();
|
2020-09-20 15:18:35 +00:00
|
|
|
const resp = await getFiles("0", token, "24", encryptionKey);
|
2020-09-19 21:20:10 +00:00
|
|
|
setLoading(false);
|
|
|
|
setData(resp);
|
|
|
|
};
|
|
|
|
main();
|
2020-09-12 21:53:41 +00:00
|
|
|
}, []);
|
|
|
|
|
2020-09-19 21:20:10 +00:00
|
|
|
if (!data || loading) {
|
2020-09-20 15:18:35 +00:00
|
|
|
return <div className="text-center">
|
2020-11-07 11:10:49 +00:00
|
|
|
<Spinner animation="border" variant="primary" />;
|
2020-09-20 15:18:35 +00:00
|
|
|
</div>
|
2020-09-12 21:53:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-20 15:18:35 +00:00
|
|
|
const getThumbnail = (item) => (
|
2020-11-07 11:10:49 +00:00
|
|
|
<PreviewCard data={item} />
|
2020-09-20 15:18:35 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
return (<Container>
|
|
|
|
<PhotoSwipeGallery
|
|
|
|
items={data.map(item => ({
|
|
|
|
...item,
|
|
|
|
src: '/image.svg',
|
2020-10-03 14:21:56 +00:00
|
|
|
w: window.innerWidth,
|
|
|
|
h: window.innerHeight,
|
2020-09-20 15:18:35 +00:00
|
|
|
}))}
|
|
|
|
thumbnailContent={getThumbnail}
|
|
|
|
/>
|
|
|
|
</Container>);
|
2020-09-12 21:53:41 +00:00
|
|
|
}
|