2020-09-19 21:20:10 +00:00
|
|
|
import React, { useLayoutEffect, 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';
|
|
|
|
import { fileData, getFiles } from 'services/fileService';
|
|
|
|
import { getData, LS_KEYS } from 'utils/storage/localStorage';
|
|
|
|
import PreviewCard from './components/PreviewCard';
|
|
|
|
import { getActualKey } from 'utils/common/key';
|
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);
|
|
|
|
const [data, setData] = useState<fileData[]>();
|
2020-09-12 21:53:41 +00:00
|
|
|
|
2020-09-19 21:20:10 +00:00
|
|
|
useLayoutEffect(() => {
|
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();
|
|
|
|
const resp = await getFiles("0", token, "100", encryptionKey);
|
|
|
|
setLoading(false);
|
|
|
|
setData(resp);
|
|
|
|
};
|
|
|
|
main();
|
2020-09-12 21:53:41 +00:00
|
|
|
}, []);
|
|
|
|
|
2020-09-19 21:20:10 +00:00
|
|
|
if (!data || loading) {
|
|
|
|
return <Spinner animation="border" variant="primary"/>;
|
2020-09-12 21:53:41 +00:00
|
|
|
}
|
|
|
|
|
2020-09-19 21:20:10 +00:00
|
|
|
return (data || []).map(item => <PreviewCard key={item.id} data={item} />);
|
2020-09-12 21:53:41 +00:00
|
|
|
}
|