-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
bc59f38
commit 710da1e
Showing
14 changed files
with
816 additions
and
136 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import prisma from '@/server/db'; | ||
|
||
export type Diary = Record< | ||
string, | ||
{ | ||
title: string; | ||
day: number; | ||
}[] | ||
>; | ||
|
||
export const getUserDiary = async (userId: number): Promise<Diary> => { | ||
const userEntries = await prisma.userEntry.findMany({ | ||
where: { | ||
userId, | ||
status: 'completed', | ||
}, | ||
orderBy: { | ||
id: 'desc', | ||
}, | ||
take: 10, | ||
include: { | ||
entry: true, | ||
}, | ||
}); | ||
|
||
const diary: Diary = {}; | ||
|
||
userEntries.forEach(userEntry => { | ||
const month = userEntry | ||
.watchedAt!.toLocaleString('default', { month: 'short' }) | ||
.toUpperCase() | ||
.slice(0, 3); | ||
if (!diary[month]) { | ||
diary[month] = []; | ||
} | ||
diary[month]!.push({ | ||
title: userEntry.entry.originalTitle, | ||
day: userEntry.watchedAt!.getDate(), | ||
}); | ||
}); | ||
|
||
const sortedDiary: Diary = {}; | ||
for (const key in diary) { | ||
sortedDiary[key] = diary[key]!.sort((a, b) => b.day - a.day); | ||
} | ||
|
||
return sortedDiary; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
'use client'; | ||
|
||
import { Button } from '@/components/ui/button'; | ||
import { User, UserFollow } from '@prisma/client'; | ||
import React, { useEffect, useState } from 'react'; | ||
import { toast } from 'sonner'; | ||
|
||
const FollowButton = ({ | ||
authUser, | ||
user, | ||
}: { | ||
authUser: User; | ||
user: User & { followers: UserFollow[] }; | ||
}) => { | ||
const [following, setFollowing] = useState(false); | ||
|
||
const toggleFollow = async () => { | ||
if (!authUser) { | ||
return; | ||
} | ||
|
||
const oldFollowingState = Boolean(following); | ||
|
||
setFollowing(!following); | ||
|
||
let response = await fetch(`/api/user/follows/${user.id}`, { | ||
method: following ? 'DELETE' : 'POST', | ||
headers: { | ||
'Content-Type': 'application/json', | ||
}, | ||
}); | ||
|
||
if (response.status !== 200) { | ||
toast.error( | ||
`Failed ${following ? 'unfollowing' : 'following'} user with error "${(await response.json()).error}"` | ||
); | ||
setFollowing(oldFollowingState); | ||
} else { | ||
toast.success( | ||
`Successfully ${following ? 'unfollowed' : 'followed'} user` | ||
); | ||
} | ||
}; | ||
|
||
useEffect(() => { | ||
setFollowing( | ||
authUser | ||
? user.followers.find( | ||
e => e.userId === authUser.id && e.isFollowing | ||
) !== undefined | ||
: false | ||
); | ||
}, [authUser, user]); | ||
|
||
return ( | ||
<Button | ||
variant={following ? 'destructive' : 'outline'} | ||
onClick={() => toggleFollow()} | ||
> | ||
{following ? 'Unfollow' : 'Follow'} | ||
</Button> | ||
); | ||
}; | ||
|
||
export default FollowButton; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.