mirror of
https://github.com/DerTyp7/f1r3wave-website.git
synced 2025-10-29 13:02:09 +01:00
Fetch images client side for admin page
This commit is contained in:
@@ -1,6 +1,5 @@
|
|||||||
import ImageManager from '@/components/ImageManager';
|
import ImageManager from '@/components/ImageManager';
|
||||||
import ImageUpload from '@/components/ImageUpload';
|
import ImageUpload from '@/components/ImageUpload';
|
||||||
import { ImagesResponse, TagsResponse } from '@/interfaces/api';
|
|
||||||
import { getSession } from '@/lib/session';
|
import { getSession } from '@/lib/session';
|
||||||
import styles from '@/styles/AdminPage.module.scss';
|
import styles from '@/styles/AdminPage.module.scss';
|
||||||
import { redirect } from 'next/navigation';
|
import { redirect } from 'next/navigation';
|
||||||
@@ -12,33 +11,10 @@ export default async function AdminPage() {
|
|||||||
redirect('/login');
|
redirect('/login');
|
||||||
}
|
}
|
||||||
|
|
||||||
const fetchImages = async () => {
|
|
||||||
const apiUrl = new URL(
|
|
||||||
'/api/images?imagesPerPage=-1}',
|
|
||||||
process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3000',
|
|
||||||
);
|
|
||||||
const response = await fetch(apiUrl.toString());
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Network response was not ok: ${response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (await response.json()) as ImagesResponse;
|
|
||||||
};
|
|
||||||
|
|
||||||
const fetchTags = async () => {
|
|
||||||
const apiUrl = new URL('/api/tags', process.env.NEXT_PUBLIC_SITE_URL || 'http://localhost:3000');
|
|
||||||
const response = await fetch(apiUrl.toString());
|
|
||||||
if (!response.ok) {
|
|
||||||
throw new Error(`Network response was not ok: ${response.statusText}`);
|
|
||||||
}
|
|
||||||
|
|
||||||
return (await response.json()) as TagsResponse;
|
|
||||||
};
|
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.container}>
|
<div className={styles.container}>
|
||||||
<ImageUpload />
|
<ImageUpload />
|
||||||
<ImageManager tags={await fetchTags()} images={(await fetchImages()).images} />
|
<ImageManager />
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,3 +1,4 @@
|
|||||||
|
/* eslint-disable react-hooks/exhaustive-deps */
|
||||||
'use client';
|
'use client';
|
||||||
|
|
||||||
import Button from '@/components/Button';
|
import Button from '@/components/Button';
|
||||||
@@ -5,19 +6,14 @@ import InputField from '@/components/InputField';
|
|||||||
import { ImageMeta } from '@/interfaces/image';
|
import { ImageMeta } from '@/interfaces/image';
|
||||||
import styles from '@/styles/ImageManager.module.scss';
|
import styles from '@/styles/ImageManager.module.scss';
|
||||||
import Image from 'next/image';
|
import Image from 'next/image';
|
||||||
import { redirect, useSearchParams } from 'next/navigation';
|
import { useSearchParams } from 'next/navigation';
|
||||||
import React, { useEffect, useState } from 'react';
|
import React, { useEffect, useState } from 'react';
|
||||||
import Tags from './Tags';
|
import Tags from './Tags';
|
||||||
|
|
||||||
interface ImageManagerProps {
|
export default function ImageManager() {
|
||||||
images: ImageMeta[];
|
const [images, setImages] = useState<ImageMeta[]>([]);
|
||||||
tags: string[];
|
|
||||||
}
|
|
||||||
|
|
||||||
export default function ImageManager({ images: initialImages, tags: initialTags }: ImageManagerProps) {
|
|
||||||
const [images, setImages] = useState<ImageMeta[]>(initialImages);
|
|
||||||
const [error, setError] = useState<string>();
|
const [error, setError] = useState<string>();
|
||||||
const [tags, setTags] = useState<string[]>(initialTags);
|
const [tags, setTags] = useState<string[]>([]);
|
||||||
const [activeTag, setActiveTag] = useState<string>('all');
|
const [activeTag, setActiveTag] = useState<string>('all');
|
||||||
const searchParams = useSearchParams();
|
const searchParams = useSearchParams();
|
||||||
|
|
||||||
@@ -92,23 +88,17 @@ export default function ImageManager({ images: initialImages, tags: initialTags
|
|||||||
};
|
};
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
setActiveTag(searchParams.get('tag') ?? 'all');
|
fetchTags();
|
||||||
}, [searchParams, tags]);
|
fetchImages();
|
||||||
|
}, []);
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const filterImages = () => {
|
fetchImages();
|
||||||
if (activeTag === 'all') {
|
}, [activeTag]);
|
||||||
setImages(initialImages);
|
|
||||||
return;
|
|
||||||
} else if (activeTag && !tags.includes(activeTag)) {
|
|
||||||
redirect('/admin');
|
|
||||||
} else {
|
|
||||||
setImages(initialImages.filter((image) => image.tags.includes(activeTag)));
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
filterImages();
|
useEffect(() => {
|
||||||
}, [activeTag, initialImages, tags]);
|
setActiveTag(searchParams.get('tag') ?? 'all');
|
||||||
|
}, [searchParams, tags]);
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<div className={styles.container}>
|
<div className={styles.container}>
|
||||||
|
|||||||
Reference in New Issue
Block a user