Skip to content

Commit

Permalink
🔧 Add dynamic links for easier access
Browse files Browse the repository at this point in the history
  • Loading branch information
yagiziskirik committed Mar 24, 2024
1 parent 790268f commit 4e72907
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 1 deletion.
26 changes: 26 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
"react-tag-input": "^6.8.1",
"react-toastify": "^9.1.3",
"tailwind-merge": "^1.12.0",
"unsplash-js": "^7.0.18",
"uuidv4": "^6.2.13",
"youtube-transcript": "^1.0.6"
},
Expand Down
18 changes: 18 additions & 0 deletions src/components/UnsplashApi.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// Copyright (c) 2023 Yağız Işkırık
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT

import { createApi } from 'unsplash-js';

export default function Unsplash() {
const unsplash = createApi({
accessKey: process.env.UNSPLASH_API_KEY || '',
});
const results = unsplash.search.getPhotos({
query: 'cat',
page: 1,
perPage: 10,
});
return <p>{JSON.stringify(results)}</p>;
}
9 changes: 9 additions & 0 deletions src/components/articleCard.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,15 @@ export default function ArticleCard({
<Link
className='text-neutral-900 dark:text-neutral-100'
href={'/' + postOrDraft + '/' + id}
as={
postOrDraft === 'posts'
? `/posts/${encodeURIComponent(
header
.replace(/[^a-zA-Z0-9 - _ . ~]/g, '')
.replace(/ /g, '-')
).toLowerCase()}`
: `/${postOrDraft}/${id}`
}
>
{header}
</Link>
Expand Down
2 changes: 2 additions & 0 deletions src/pages/drafts/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import Interval from '@/components/Autosave';
import Button from '@/components/buttons/Button';
import IconButton from '@/components/buttons/IconButton';
import Editor from '@/components/Editor';
import Unsplash from '@/components/UnsplashApi';
import Wrapper from '@/components/wrapper';

type DraftType = {
Expand Down Expand Up @@ -285,6 +286,7 @@ export default function Draft({ draft }: DraftType) {
autocomplete
/>
</div>
<Unsplash />
<Editor content={content} setContent={setContentFunc} />
<div className='mt-24 flex justify-end gap-2 md:mt-14'>
<Button
Expand Down
22 changes: 21 additions & 1 deletion src/pages/posts/[id].tsx
Original file line number Diff line number Diff line change
Expand Up @@ -230,14 +230,34 @@ export default function Posts({ post }: DraftType) {

export const getServerSideProps: GetServerSideProps = async (ctx) => {
const selId = ctx.query.id as string;
const post = await prisma.post.findUnique({
let post = await prisma.post.findUnique({
where: {
id: selId,
},
include: {
user: true,
},
});
if (!post) {
const posts = await prisma.post.findMany({
include: {
user: true,
},
});
for (let i = 0; i < posts.length; i++) {
if (
encodeURIComponent(
posts[i].header
.replace(/[^a-zA-Z0-9 - _ . ~]/g, '')
.replace(/ /g, '-')
.toLowerCase()
) === selId
) {
post = posts[i];
break;
}
}
}
if (post && post.published) {
return {
props: { post: JSON.parse(JSON.stringify(post)) },
Expand Down

0 comments on commit 4e72907

Please sign in to comment.