Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add custom image loader for backend images #158

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ module.exports = withPWA({
includePaths: [path.join(__dirname, 'styles')],
prependData: '@import "~@styles/variables.scss";',
},
images: { domains: ['dev-api.mensatt.de', 'api.mensatt.de'] },
images: {
domains: ['dev-api.mensatt.de', 'api.mensatt.de'],
loader: 'custom',
},
i18n,
});
6 changes: 4 additions & 2 deletions src/components/occurrence/Occurrence.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import Popup from 'reactjs-popup';
import { GetOccurrencesByDateQuery } from 'src/graphql/graphql-types';
import { GET_NAVIGATION, Navigation } from 'src/graphql/queries';
import { DATE_FORMAT, currentDate } from 'src/util';
import { backendImageLoader } from 'src/util/imageLoader';

import { useQuery } from '@apollo/client';

Expand Down Expand Up @@ -50,7 +51,7 @@ const Occurrence = ({ occurrence }: Props) => {
// Get "URL-Base" from currently active backend
// Example: https://api.mensatt.de/v1/graphql => https://api.mensatt.de
const { data: navData } = useQuery<Navigation>(GET_NAVIGATION);
const backendURLBase = useMemo(() => {
const backendUrlBase = useMemo(() => {
const { protocol, hostname } = new URL(
navData?.backends[navData.activeBackendIdx].url ||
'https://api.mensatt.de/v1/graphql',
Expand Down Expand Up @@ -132,12 +133,13 @@ const Occurrence = ({ occurrence }: Props) => {
{filteredDishImages.length > 0 ? (
<Image
key={randomImage.id}
src={backendURLBase + randomImage.imageUrl}
src={backendUrlBase + randomImage.imageUrl}
alt={t('imageDescription', {
name: occurrenceName,
author: randomImage.displayName || t('noAuthorName'),
})}
layout={'fill'}
loader={backendImageLoader}
/>
) : (
<span>{t('noImagesMsg')}</span>
Expand Down
2 changes: 2 additions & 0 deletions src/pages/admin/reviews/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import {
SetReviewApprovalStatusMutationVariables,
} from 'src/graphql/graphql-types';
import { GET_NAVIGATION, Navigation } from 'src/graphql/queries';
import { backendImageLoader } from 'src/util/imageLoader';

import { useMutation, useQuery } from '@apollo/client';

Expand Down Expand Up @@ -126,6 +127,7 @@ const AdminReviewsPage: NextPage = () => {
})}
layout={'fill'}
objectFit="contain"
loader={backendImageLoader}
/>
</div>
</Modal>
Expand Down
9 changes: 9 additions & 0 deletions src/util/imageLoader.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
type CustomLoaderParams = {
src: string;
width: number;
};

export const backendImageLoader = ({ src, width }: CustomLoaderParams) => {
if (width) src += `&width=${width}`;
return src;
};