Skip to content

Commit 5899266

Browse files
authored
Merge pull request #610 from Alt-Org/nikolas/feature/603-implement-directus-to-gallery-page
Nikolas/feature/603 implement directus to gallery page
2 parents b82fc2a + 0883f9b commit 5899266

File tree

8 files changed

+201
-105
lines changed

8 files changed

+201
-105
lines changed

frontend-next-migration/src/entities/Gallery/api/galleryApi.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,13 @@ const galleryApi = directusApi.injectEndpoints({
4646
'category.translations.*',
4747
'full.translations.*',
4848
'preview.translations.*',
49+
'translations.*',
4950
],
5051
deep: {
5152
category: { translations: true },
5253
preview: { translations: true },
5354
full: { translations: true },
55+
translations: true,
5456
},
5557
}),
5658
);

frontend-next-migration/src/entities/Gallery/api/translations.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { PhotoVersionTranslations, CategoryTranslations } from '../types/gallery';
1+
import {
2+
PhotoVersionTranslations,
3+
CategoryTranslations,
4+
PhotoObjectTranslations,
5+
} from '../types/gallery';
26

37
export const getLanguageCode = (language: string): string => {
48
return language === 'en' ? 'en-US' : language === 'fi' ? 'fi-FI' : 'default';
@@ -27,3 +31,20 @@ export const getPhotoVersionTranslation = (
2731
) => {
2832
return getTranslation(translations, languageCode, 'altText', '');
2933
};
34+
35+
export const getPhotoObjectTexts = (
36+
translations: PhotoObjectTranslations[] = [],
37+
languageCode: string,
38+
) => {
39+
if (!translations || translations.length === 0) {
40+
return { title: '', author: '', description: '' };
41+
}
42+
43+
const tr = translations.find((t) => t.languages_code === languageCode) ?? translations[0];
44+
45+
return {
46+
title: tr.title ?? '',
47+
author: tr.author ?? '',
48+
description: tr.description ?? '',
49+
};
50+
};

frontend-next-migration/src/entities/Gallery/api/useGetDirectusGalleryImages.test.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ jest.mock('../api/galleryCategoriesApi', () => ({
1616
jest.mock('../api/translations', () => ({
1717
getPhotoVersionTranslation: jest.fn(),
1818
getCategoryTranslation: jest.fn(),
19+
getPhotoObjectTexts: jest.fn(() => ({ title: '', author: '', description: '' })),
1920
}));
2021

2122
jest.mock('@/shared/const/envHelper', () => ({

frontend-next-migration/src/entities/Gallery/api/useGetDirectusGalleryImages.ts

Lines changed: 44 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { envHelper } from '@/shared/const/envHelper';
22
import { useMemo } from 'react';
33
import { Category, PhotoObject, PhotoVersion } from '../types/gallery';
4-
import { getPhotoVersionTranslation } from '../api/translations';
4+
import { getPhotoVersionTranslation, getPhotoObjectTexts } from '../api/translations';
55
import { useGetGalleryCategoriesQuery } from '../api/galleryCategoriesApi';
66
import { useGetPhotoObjectsQuery, useGetPhotoVersionsQuery } from '../api/galleryApi';
77

@@ -62,34 +62,52 @@ export const useGetDirectusGalleryImages = (lng: string) => {
6262
height: item.height,
6363
altText: getPhotoVersionTranslation(item.translations || [], lng),
6464
}));
65-
}, [pvData]);
65+
}, [pvData, lng]);
6666

6767
const photoObjects: PhotoObject[] = useMemo(() => {
6868
if (!poData) return [];
69-
return poData.map((item) => ({
70-
id: item.id,
71-
category: {
72-
id: item.category.id,
73-
translations: item.category.translations,
74-
},
75-
versions: {
76-
preview: {
77-
id: item.preview.id,
78-
image: `${directusBaseUrl}/assets/${item.preview.image}`,
79-
width: item.preview.width,
80-
height: item.preview.height,
81-
altText: getPhotoVersionTranslation(item.preview.translations || [], lng),
82-
},
83-
full: {
84-
id: item.full.id,
85-
image: `${directusBaseUrl}/assets/${item.full.image}`,
86-
width: item.full.width,
87-
height: item.full.height,
88-
altText: getPhotoVersionTranslation(item.full.translations || [], lng),
89-
},
90-
},
91-
}));
92-
}, [poData, directusBaseUrl]);
69+
return poData.map((item) => {
70+
const texts = getPhotoObjectTexts(item.translations || [], lng);
71+
72+
return {
73+
id: item.id,
74+
title: texts.title || item.title,
75+
author: texts.author || item.author,
76+
description: texts.description || item.description,
77+
category: item.category
78+
? {
79+
id: item.category.id,
80+
translations: item.category.translations,
81+
}
82+
: undefined,
83+
versions:
84+
item.preview && item.full
85+
? {
86+
preview: {
87+
id: item.preview.id,
88+
image: `${directusBaseUrl}/assets/${item.preview.image}`,
89+
width: item.preview.width,
90+
height: item.preview.height,
91+
altText: getPhotoVersionTranslation(
92+
item.preview.translations || [],
93+
lng,
94+
),
95+
},
96+
full: {
97+
id: item.full.id,
98+
image: `${directusBaseUrl}/assets/${item.full.image}`,
99+
width: item.full.width,
100+
height: item.full.height,
101+
altText: getPhotoVersionTranslation(
102+
item.full.translations || [],
103+
lng,
104+
),
105+
},
106+
}
107+
: undefined,
108+
};
109+
});
110+
}, [poData, directusBaseUrl, lng]);
93111

94112
return { photoVersions, categories, photoObjects, error, isLoading };
95113
};

frontend-next-migration/src/entities/Gallery/types/gallery.d.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,15 @@ export interface PhotoVersionTranslations {
2929
altText: string;
3030
}
3131

32+
export interface PhotoObjectTranslations {
33+
id: string;
34+
languages_code: string;
35+
photo_object_id: string;
36+
title?: string;
37+
author?: string;
38+
description?: string;
39+
}
40+
3241
export interface Category {
3342
id: string;
3443
translations: CategoryTranslations[];
@@ -44,11 +53,12 @@ export interface PhotoVersion {
4453

4554
export interface PhotoObject {
4655
title?: string;
56+
author?: string;
4757
description?: string;
48-
supDescription?: string;
4958
frames?: string[][];
5059
id?: string;
5160
category?: Category;
61+
translations?: PhotoObjectTranslations[];
5262
versions?: {
5363
preview: PhotoVersion;
5464
full: PhotoVersion;

frontend-next-migration/src/preparedPages/PictureGalleryPages/ui/PictureGalleryPage.tsx

Lines changed: 65 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
'use client';
22
import React, { useState, useMemo } from 'react';
3-
import firstImg from '@/shared/assets/images/gallery/Frame 523.png';
4-
import secindtImg from '@/shared/assets/images/gallery/Frame 524.png';
5-
import thirdtImg from '@/shared/assets/images/gallery/Frame 525.png';
63
import { AnimationGallerySection } from '@/widgets/SectionGallery/ui/SectionGalleryV2/SectionGallery';
7-
import { PhotoObject } from '@/entities/Gallery';
4+
import {
5+
getLanguageCode,
6+
PhotoObject,
7+
useGetDirectusGalleryImages,
8+
getCategoryTranslation,
9+
} from '@/entities/Gallery';
810
import { Container } from '@/shared/ui/Container';
911
import cls from './PictureGalleryPage.module.scss';
1012
import { useClientTranslation } from '@/shared/i18n';
@@ -14,7 +16,7 @@ import { PageTitle } from '@/shared/ui/PageTitle';
1416
import { classNames } from '@/shared/lib/classNames/classNames';
1517
// import buttonImg from '@/shared/assets/images/gallery/Frame 526.png';
1618
// import { SectionGalleryV2 } from '@/widgets/SectionGallery';
17-
// import { useParams } from 'next/navigation';
19+
import { useParams } from 'next/navigation';
1820
// import { useGetDirectusGalleryImages, getLanguageCode, getCategoryTranslation, } from '@/entities/Gallery';
1921

2022
export interface Props {
@@ -51,51 +53,71 @@ export interface Props {
5153
if (isLoading) <p>Loading...</p>;
5254
*/
5355

54-
// Dummy data for filteredImages to avoid errors
55-
const images: PhotoObject[] = [
56-
{
57-
title: 'Valveenvälttelijä',
58-
description:
59-
'Tekijä: Netti-C Samu (tähän kohtaan tekijän oikea nimi tai haluttu taiteilijanimi)',
60-
supDescription: 'Tähän lisää tekstiä jos tekijä haluaa selittää prosessistaan.',
61-
frames: [[firstImg.src, secindtImg.src, thirdtImg.src]],
62-
},
63-
{
64-
title: 'Valveenvälttelijä',
65-
description:
66-
'Tekijä: Netti-C Samu (tähän kohtaan tekijän oikea nimi tai haluttu taiteilijanimi)',
67-
supDescription: 'Tähän lisää tekstiä jos tekijä haluaa selittää prosessistaan.',
68-
frames: [[firstImg.src, secindtImg.src, thirdtImg.src]],
69-
},
70-
{
71-
title: 'Valveenvälttelijä',
72-
description:
73-
'Tekijä: Netti-C Samu (tähän kohtaan tekijän oikea nimi tai haluttu taiteilijanimi)',
74-
supDescription: 'Tähän lisää tekstiä jos tekijä haluaa selittää prosessistaan.',
75-
frames: [[firstImg.src, secindtImg.src, thirdtImg.src]],
76-
},
77-
];
78-
7956
const PictureGalleryPage = () => {
8057
const { t } = useClientTranslation('picture-galleries');
8158
const { isMobileSize, isDesktopSize, isWidescreenSize } = useSizes();
8259
const [searchQuery, setSearchQuery] = useState('');
60+
61+
const params = useParams();
62+
const lng = params.lng as string;
63+
const categorySlug = params.category as string | undefined;
64+
65+
const languageCode = getLanguageCode(lng);
66+
const { photoObjects, isLoading, error } = useGetDirectusGalleryImages(languageCode);
67+
8368
const isBigDevice = isDesktopSize || isWidescreenSize;
69+
const allCategory = lng === 'en' ? 'all' : 'kaikki';
8470

8571
const showCreativity = useMemo(
8672
() => !isMobileSize && searchQuery.length === 0,
8773
[isMobileSize, searchQuery],
8874
);
8975

90-
// Filter images by search query matching title, description or supDescription (case-insensitive)
91-
const filteredImages = useMemo(() => {
76+
// filter images by category from URL params
77+
const categoryFilteredImages: PhotoObject[] = useMemo(() => {
78+
if (!photoObjects) return [];
79+
if (!categorySlug || categorySlug === allCategory) {
80+
return photoObjects;
81+
}
82+
83+
return photoObjects.filter((photo) => {
84+
if (!photo.category) return false;
85+
const translatedCategory = getCategoryTranslation(
86+
photo.category.translations,
87+
languageCode,
88+
);
89+
return translatedCategory === categorySlug;
90+
});
91+
}, [photoObjects, categorySlug, allCategory, languageCode]);
92+
93+
const filteredImages: PhotoObject[] = useMemo(() => {
9294
const query = searchQuery.trim().toLowerCase();
93-
if (!query) return images;
94-
return images.filter((photo) => {
95-
const fields = [photo.title, photo.description, photo.supDescription];
95+
if (!query) return categoryFilteredImages;
96+
return categoryFilteredImages.filter((photo) => {
97+
const fields = [photo.title, photo.author, photo.description];
9698
return fields.some((find) => (find || '').toLowerCase().includes(query));
9799
});
98-
}, [searchQuery]);
100+
}, [searchQuery, categoryFilteredImages]);
101+
102+
if (isLoading) {
103+
return (
104+
<div className={cls.Wrapper}>
105+
<Container className={cls.Container}>
106+
<p>{t('loading') ?? 'Loading...'}</p>
107+
</Container>
108+
</div>
109+
);
110+
}
111+
112+
if (error) {
113+
return (
114+
<div className={cls.Wrapper}>
115+
<Container className={cls.Container}>
116+
<p>{t('error-text') ?? 'Error loading gallery.'}</p>
117+
</Container>
118+
</div>
119+
);
120+
}
99121

100122
return (
101123
<div className={cls.Wrapper}>
@@ -130,9 +152,14 @@ const PictureGalleryPage = () => {
130152
animations={filteredImages.map((photo) => ({
131153
// Adjust these mappings as needed based on your PhotoObject and FrameSet definitions
132154
title: photo.title || '',
155+
author: photo.author || '',
133156
description: photo.description || '',
134-
supDescription: photo.supDescription || '',
135-
frames: photo.frames || [],
157+
frames:
158+
photo.frames && photo.frames.length > 0
159+
? photo.frames
160+
: photo.versions?.preview
161+
? [[photo.versions.preview.image]]
162+
: [],
136163
// Add other FrameSet properties if required
137164
}))}
138165
/>

frontend-next-migration/src/widgets/SectionGallery/ui/SectionGalleryV2/SectionGallery.tsx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ import Image from 'next/image';
77

88
interface FrameSet {
99
title: string;
10+
author: string;
1011
description: string;
11-
supDescription: string;
1212
frames: string[][]; // each row is an array of image paths
1313
}
1414

@@ -35,8 +35,8 @@ export const AnimationGallerySection = ({ animations }: AnimationGalleryProps) =
3535
>
3636
<div className={cls.textBlock}>
3737
<h1 className={cls.title}>{set.title}</h1>
38+
<p className={cls.author}>{set.author}</p>
3839
<p className={cls.description}>{set.description}</p>
39-
<p className={cls.supdescription}>{set.supDescription}</p>
4040
</div>
4141
<div className={cls.framesContainer}>
4242
{set.frames.map((row, rowIndex) => (
@@ -52,8 +52,8 @@ export const AnimationGallerySection = ({ animations }: AnimationGalleryProps) =
5252
<Image
5353
src={imgSrc}
5454
alt={`Frame ${imgIndex}`}
55-
width={1000}
56-
height={100}
55+
fill
56+
sizes="(max-width: 768px) 100vw, 33vw"
5757
className={cls.frameImage}
5858
/>
5959
</div>

0 commit comments

Comments
 (0)