mirror of
				https://github.com/r-freeman/portfolio.git
				synced 2025-11-04 10:51:11 +00:00 
			
		
		
		
	Add article title to comment notifications
	
		
			
	
		
	
	
		
	
		
			All checks were successful
		
		
	
	
		
			
				
	
				Build And Publish / BuildAndPublish (push) Successful in 3m8s
				
			
		
		
	
	
				
					
				
			
		
			All checks were successful
		
		
	
	Build And Publish / BuildAndPublish (push) Successful in 3m8s
				
			This commit is contained in:
		
							parent
							
								
									e9564826d0
								
							
						
					
					
						commit
						d69bea5163
					
				@ -9,10 +9,10 @@ export async function loginWithGitHub() {
 | 
				
			|||||||
    await signIn('github')
 | 
					    await signIn('github')
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
const notificationBody = (comment: { id: number, content: string }, user: { name: string }) => {
 | 
					const notificationBody = (comment: { id: number, content: string }, user: { name: string }, article: { title: string }) => {
 | 
				
			||||||
    return {
 | 
					    return {
 | 
				
			||||||
        topic: 'comments',
 | 
					        topic: 'portfolio',
 | 
				
			||||||
        message: `You've got a new comment from ${user.name}:\n${comment.content}`,
 | 
					        message: `New comment on ${article.title} from ${user.name}:\n${comment.content}`,
 | 
				
			||||||
        actions: [
 | 
					        actions: [
 | 
				
			||||||
            {
 | 
					            {
 | 
				
			||||||
                action: 'http',
 | 
					                action: 'http',
 | 
				
			||||||
@ -60,7 +60,7 @@ export async function addComment(prevState: { message: string }, formData: FormD
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
        const [{data: user}, {data: article}] = await Promise.all([
 | 
					        const [{data: user}, {data: article}] = await Promise.all([
 | 
				
			||||||
            supabase.from('users').upsert({name, email, image}, {onConflict: 'email'}).select('*').single(),
 | 
					            supabase.from('users').upsert({name, email, image}, {onConflict: 'email'}).select('*').single(),
 | 
				
			||||||
            supabase.from('articles').select('id').eq('slug', slug).single()
 | 
					            supabase.from('articles').select('*').eq('slug', slug).single()
 | 
				
			||||||
        ])
 | 
					        ])
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        const {data: newComment, error} = await supabase
 | 
					        const {data: newComment, error} = await supabase
 | 
				
			||||||
@ -73,7 +73,7 @@ export async function addComment(prevState: { message: string }, formData: FormD
 | 
				
			|||||||
            return {message: general_error}
 | 
					            return {message: general_error}
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        await sendNotification(notificationBody(newComment, user))
 | 
					        await sendNotification(notificationBody(newComment, user, article))
 | 
				
			||||||
 | 
					
 | 
				
			||||||
        return {message: success_message}
 | 
					        return {message: success_message}
 | 
				
			||||||
    } catch (error) {
 | 
					    } catch (error) {
 | 
				
			||||||
 | 
				
			|||||||
@ -2,9 +2,9 @@
 | 
				
			|||||||
 | 
					
 | 
				
			||||||
import {createClient} from '@/lib/supabase/server'
 | 
					import {createClient} from '@/lib/supabase/server'
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export async function incrementViews(slug: string) {
 | 
					export async function incrementViews(slug: string, title: string) {
 | 
				
			||||||
    if (slug !== null) {
 | 
					    if (slug !== null && title !== null) {
 | 
				
			||||||
        const supabase = await createClient()
 | 
					        const supabase = await createClient()
 | 
				
			||||||
        await supabase.rpc('increment_views', {param_slug: slug})
 | 
					        await supabase.rpc('increment_views', {param_slug: slug, param_title: title})
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
@ -14,7 +14,7 @@ export async function PATCH(request: Request, {params}: { params: Promise<{ id:
 | 
				
			|||||||
                .update({published: true})
 | 
					                .update({published: true})
 | 
				
			||||||
                .eq('id', id)
 | 
					                .eq('id', id)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
            return new Response(null, {status: 204})
 | 
					            return new Response(JSON.stringify({}), {status: 200})
 | 
				
			||||||
        }
 | 
					        }
 | 
				
			||||||
        return new Response(JSON.stringify({status: 'Not Found'}), {status: 404})
 | 
					        return new Response(JSON.stringify({status: 'Not Found'}), {status: 404})
 | 
				
			||||||
    }
 | 
					    }
 | 
				
			||||||
 | 
				
			|||||||
@ -49,6 +49,7 @@ function Article(article: Article) {
 | 
				
			|||||||
                    </Card.Eyebrow>
 | 
					                    </Card.Eyebrow>
 | 
				
			||||||
                    <Views
 | 
					                    <Views
 | 
				
			||||||
                        slug={article.slug}
 | 
					                        slug={article.slug}
 | 
				
			||||||
 | 
					                        title={article.title}
 | 
				
			||||||
                        className="text-sm text-zinc-500 dark:text-zinc-400"
 | 
					                        className="text-sm text-zinc-500 dark:text-zinc-400"
 | 
				
			||||||
                        shouldUpdateViews={false}
 | 
					                        shouldUpdateViews={false}
 | 
				
			||||||
                    />
 | 
					                    />
 | 
				
			||||||
 | 
				
			|||||||
@ -46,6 +46,7 @@ function Article({article}: { article: Article }) {
 | 
				
			|||||||
                    </Card.Eyebrow>
 | 
					                    </Card.Eyebrow>
 | 
				
			||||||
                    <Views
 | 
					                    <Views
 | 
				
			||||||
                        slug={article.slug}
 | 
					                        slug={article.slug}
 | 
				
			||||||
 | 
					                        title={article.title}
 | 
				
			||||||
                        className="text-sm text-zinc-500 dark:text-zinc-400"
 | 
					                        className="text-sm text-zinc-500 dark:text-zinc-400"
 | 
				
			||||||
                        shouldUpdateViews={false}
 | 
					                        shouldUpdateViews={false}
 | 
				
			||||||
                    />
 | 
					                    />
 | 
				
			||||||
 | 
				
			|||||||
@ -54,7 +54,7 @@ export function ArticleLayout({
 | 
				
			|||||||
                                <time dateTime={date}>
 | 
					                                <time dateTime={date}>
 | 
				
			||||||
                                    <span>{formatDate(date)}</span>
 | 
					                                    <span>{formatDate(date)}</span>
 | 
				
			||||||
                                </time>
 | 
					                                </time>
 | 
				
			||||||
                                <Views slug={slug} shouldUpdateViews={true}/>
 | 
					                                <Views slug={slug} title={title} shouldUpdateViews={true}/>
 | 
				
			||||||
                            </p>
 | 
					                            </p>
 | 
				
			||||||
                        </header>
 | 
					                        </header>
 | 
				
			||||||
                        <Prose className="mt-8" data-mdx-content>{children}</Prose>
 | 
					                        <Prose className="mt-8" data-mdx-content>{children}</Prose>
 | 
				
			||||||
 | 
				
			|||||||
@ -9,11 +9,12 @@ import {incrementViews} from '@/app/actions/views'
 | 
				
			|||||||
type ViewsProps = {
 | 
					type ViewsProps = {
 | 
				
			||||||
    as?: ElementType
 | 
					    as?: ElementType
 | 
				
			||||||
    slug: string
 | 
					    slug: string
 | 
				
			||||||
 | 
					    title: string
 | 
				
			||||||
    className?: string
 | 
					    className?: string
 | 
				
			||||||
    shouldUpdateViews?: boolean
 | 
					    shouldUpdateViews?: boolean
 | 
				
			||||||
}
 | 
					}
 | 
				
			||||||
 | 
					
 | 
				
			||||||
export function Views({as: Component = 'span', slug, className, shouldUpdateViews = true}: ViewsProps) {
 | 
					export function Views({as: Component = 'span', slug, title, className, shouldUpdateViews = true}: ViewsProps) {
 | 
				
			||||||
    const {data} = useSWR(`/api/views/${slug}`, fetcher) as { data: { views: number } }
 | 
					    const {data} = useSWR(`/api/views/${slug}`, fetcher) as { data: { views: number } }
 | 
				
			||||||
    const {mutate} = useSWRConfig()
 | 
					    const {mutate} = useSWRConfig()
 | 
				
			||||||
 | 
					
 | 
				
			||||||
@ -22,7 +23,7 @@ export function Views({as: Component = 'span', slug, className, shouldUpdateView
 | 
				
			|||||||
            const updateViews = async () => {
 | 
					            const updateViews = async () => {
 | 
				
			||||||
                const hasViewed = sessionStorage.getItem(`has-viewed-${slug}`)
 | 
					                const hasViewed = sessionStorage.getItem(`has-viewed-${slug}`)
 | 
				
			||||||
                if (!hasViewed) {
 | 
					                if (!hasViewed) {
 | 
				
			||||||
                    await incrementViews(slug)
 | 
					                    await incrementViews(slug, title)
 | 
				
			||||||
 | 
					
 | 
				
			||||||
                    sessionStorage.setItem(`has-viewed-${slug}`, 'true')
 | 
					                    sessionStorage.setItem(`has-viewed-${slug}`, 'true')
 | 
				
			||||||
                }
 | 
					                }
 | 
				
			||||||
 | 
				
			|||||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user