mirror of
https://github.com/ahmadk953/tasko.git
synced 2025-02-12 05:42:07 +00:00
Updated how Images are Handeled to Comply With Unsplash API Guidelines
This commit is contained in:
parent
d99f17c9d1
commit
01bebf0d83
3 changed files with 42 additions and 13 deletions
|
@ -34,15 +34,22 @@ const handler = async (data: InputType): Promise<ReturnType> => {
|
||||||
|
|
||||||
const { title, image } = data;
|
const { title, image } = data;
|
||||||
|
|
||||||
const [imageId, imageThumbUrl, imageFullUrl, imageLinkHTML, imageUserName] =
|
const [
|
||||||
image.split('|');
|
imageId,
|
||||||
|
imageThumbUrl,
|
||||||
|
imageFullUrl,
|
||||||
|
imageLinkHTML,
|
||||||
|
imageUserName,
|
||||||
|
imageDownloadUrl,
|
||||||
|
] = image.split('|');
|
||||||
|
|
||||||
if (
|
if (
|
||||||
!imageId ||
|
!imageId ||
|
||||||
!imageThumbUrl ||
|
!imageThumbUrl ||
|
||||||
!imageFullUrl ||
|
!imageFullUrl ||
|
||||||
!imageUserName ||
|
!imageUserName ||
|
||||||
!imageLinkHTML
|
!imageLinkHTML ||
|
||||||
|
!imageDownloadUrl
|
||||||
) {
|
) {
|
||||||
return {
|
return {
|
||||||
error: 'Missing fields. Failed to create board.',
|
error: 'Missing fields. Failed to create board.',
|
||||||
|
@ -52,6 +59,13 @@ const handler = async (data: InputType): Promise<ReturnType> => {
|
||||||
let board;
|
let board;
|
||||||
|
|
||||||
try {
|
try {
|
||||||
|
await fetch(imageDownloadUrl, {
|
||||||
|
method: 'GET',
|
||||||
|
headers: {
|
||||||
|
Authorization: `Client-ID ${process.env.UNSPLASH_ACCESS_KEY}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
board = await db.board.create({
|
board = await db.board.create({
|
||||||
data: {
|
data: {
|
||||||
title,
|
title,
|
||||||
|
|
20
app/api/unsplash/route.ts
Normal file
20
app/api/unsplash/route.ts
Normal file
|
@ -0,0 +1,20 @@
|
||||||
|
import { unsplash } from '@/lib/unsplash';
|
||||||
|
import { NextResponse } from 'next/server';
|
||||||
|
|
||||||
|
export async function GET() {
|
||||||
|
try {
|
||||||
|
const result = await unsplash.photos.getRandom({
|
||||||
|
collectionIds: ['317099'],
|
||||||
|
count: 9,
|
||||||
|
});
|
||||||
|
|
||||||
|
if (result?.response) {
|
||||||
|
const newImages = result.response as Array<Record<string, any>>;
|
||||||
|
return new NextResponse(JSON.stringify(newImages), { status: 200 });
|
||||||
|
} else {
|
||||||
|
return new NextResponse('Failed to get images', { status: 500 });
|
||||||
|
}
|
||||||
|
} catch (error) {
|
||||||
|
return new NextResponse(JSON.stringify(error), { status: 500 });
|
||||||
|
}
|
||||||
|
}
|
|
@ -7,7 +7,6 @@ import { useEffect, useState } from 'react';
|
||||||
import { useFormStatus } from 'react-dom';
|
import { useFormStatus } from 'react-dom';
|
||||||
import { Check, Loader2 } from 'lucide-react';
|
import { Check, Loader2 } from 'lucide-react';
|
||||||
|
|
||||||
import { unsplash } from '@/lib/unsplash';
|
|
||||||
import { cn } from '@/lib/utils';
|
import { cn } from '@/lib/utils';
|
||||||
import { defaultImages } from '@/constants/images';
|
import { defaultImages } from '@/constants/images';
|
||||||
import { FormErrors } from './form-errors';
|
import { FormErrors } from './form-errors';
|
||||||
|
@ -28,16 +27,12 @@ export const FormPicker = ({ id, errors }: FormPickerProps) => {
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
const fetchImages = async () => {
|
const fetchImages = async () => {
|
||||||
try {
|
try {
|
||||||
const result = await unsplash.photos.getRandom({
|
const response = await fetch('/api/unsplash');
|
||||||
collectionIds: ['317099'],
|
if (response.ok) {
|
||||||
count: 9,
|
const newImages = await response.json();
|
||||||
});
|
|
||||||
|
|
||||||
if (result?.response) {
|
|
||||||
const newImages = result.response as Array<Record<string, any>>;
|
|
||||||
setImages(newImages);
|
setImages(newImages);
|
||||||
} else {
|
} else {
|
||||||
console.error('Failed to get images.');
|
console.error('Failed to fetch images');
|
||||||
}
|
}
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
console.log(error);
|
console.log(error);
|
||||||
|
@ -80,7 +75,7 @@ export const FormPicker = ({ id, errors }: FormPickerProps) => {
|
||||||
className='hidden'
|
className='hidden'
|
||||||
checked={selectedImageId === image.id}
|
checked={selectedImageId === image.id}
|
||||||
disabled={pending}
|
disabled={pending}
|
||||||
value={`${image.id}|${image.urls.thumb}|${image.urls.full}|${image.links.html}|${image.user.name}`}
|
value={`${image.id}|${image.urls.thumb}|${image.urls.full}|${image.links.html}|${image.user.name}|${image.links.download_location}`}
|
||||||
/>
|
/>
|
||||||
<Image
|
<Image
|
||||||
src={image.urls.thumb}
|
src={image.urls.thumb}
|
||||||
|
|
Loading…
Add table
Reference in a new issue