Skip to content

Conversation

zacjones93
Copy link
Contributor

This follows the same pattern used to remove posts as implemented for adding posts.

Another PR should be considered to consolidate this syncing logic into it's own lib file

gif

Copy link

vercel bot commented Jan 27, 2025

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
ai-hero ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 28, 2025 5:10pm
course-builder-egghead ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 28, 2025 5:10pm
course-builder-poc ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 28, 2025 5:10pm
epic-react-builder ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 28, 2025 5:10pm
go-local-first ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jan 28, 2025 5:10pm
1 Skipped Deployment
Name Status Preview Comments Updated (UTC)
astro-party ⬜️ Ignored (Inspect) Visit Preview Jan 28, 2025 5:10pm

Comment on lines 133 to 149
onDelete={async ({ itemId }: { itemId: string }) => {
const post = await getPost(itemId)

if (!post || !post.fields?.eggheadLessonId) {
throw new Error('eggheadLessonId is required')
}

await removePostFromCoursePost({
postId: itemId,
resourceOfId: resource.id,
})

await removeEggheadLessonFromPlaylist({
eggheadLessonId: post.fields?.eggheadLessonId,
eggheadPlaylistId: resource.fields?.eggheadPlaylistId,
})
}}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

following pattern seen in EditResourcesForm where we define logic at the top level and pass function down to where it is called

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this whole stack might move into a single function in post-query or similar instead of being buried in a component so it'd be a si ngle call here

Comment on lines 14 to 21
return await db
.delete(contentResourceResource)
.where(
and(
eq(contentResourceResource.resourceOfId, resourceOfId),
eq(contentResourceResource.resourceId, postId),
),
)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs to run on the server so put function in actions folder. Reviewing it now, this should probably be in posts-query?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe, actions need to absolutely do auth checks though, regardless. review everything for that plz. As is a smart haxx0r could probably trigger this

Comment on lines -407 to +409
{item.type === 'section' && item.children.length === 0 && (
{item.type === 'post' && (
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we don't have sections in egghead right now so switched this block for post deletion

Comment on lines +643 to +665
const response = await fetch(
`${EGGHEAD_API_V1_BASE_URL}/playlists/${eggheadPlaylistId}/items/remove`,
{
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${eggheadToken}`,
'User-Agent': 'authjs',
},
body: JSON.stringify({
tracklistable: {
tracklistable_type: 'Lesson',
tracklistable_id: eggheadLessonId,
},
}),
},
).then(async (res) => {
if (!res.ok) {
console.log('Error removing lesson from playlist', res)
throw new EggheadApiError(res.statusText, res.status)
}
return await res.json()
})
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the syncing work

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't critical, but unless it provides added benefit I'd like to move away from using the API and do the work directly against the DB, but there might be advantages to the API (related business processes that occur etc), but for basic crud...

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@Creeland and I went this initial route because of the tracklist table/logic involved with playlists but we can look at refactoring these api calls to DB operations as a next step

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ya it's definitely simpler/safer but more coupled to rails so tradeoff is a thing. It's fine, just something to consider. Wouldn't change for this PR for sure.

Copy link
Collaborator

@joelhooks joelhooks left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the authorization stuff needs to be addressed

Comment on lines +643 to +665
const response = await fetch(
`${EGGHEAD_API_V1_BASE_URL}/playlists/${eggheadPlaylistId}/items/remove`,
{
method: 'PUT',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${eggheadToken}`,
'User-Agent': 'authjs',
},
body: JSON.stringify({
tracklistable: {
tracklistable_type: 'Lesson',
tracklistable_id: eggheadLessonId,
},
}),
},
).then(async (res) => {
if (!res.ok) {
console.log('Error removing lesson from playlist', res)
throw new EggheadApiError(res.statusText, res.status)
}
return await res.json()
})
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this isn't critical, but unless it provides added benefit I'd like to move away from using the API and do the work directly against the DB, but there might be advantages to the API (related business processes that occur etc), but for basic crud...

Comment on lines 14 to 21
return await db
.delete(contentResourceResource)
.where(
and(
eq(contentResourceResource.resourceOfId, resourceOfId),
eq(contentResourceResource.resourceId, postId),
),
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe, actions need to absolutely do auth checks though, regardless. review everything for that plz. As is a smart haxx0r could probably trigger this

Comment on lines 133 to 149
onDelete={async ({ itemId }: { itemId: string }) => {
const post = await getPost(itemId)

if (!post || !post.fields?.eggheadLessonId) {
throw new Error('eggheadLessonId is required')
}

await removePostFromCoursePost({
postId: itemId,
resourceOfId: resource.id,
})

await removeEggheadLessonFromPlaylist({
eggheadLessonId: post.fields?.eggheadLessonId,
eggheadPlaylistId: resource.fields?.eggheadPlaylistId,
})
}}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this whole stack might move into a single function in post-query or similar instead of being buried in a component so it'd be a si ngle call here

Comment on lines +134 to +146
try {
await removePostFromCoursePost({
postId: itemId,
resourceOfId: resource.id,
})
} catch (error) {
console.error('Error removing lesson from playlist', error)
toast({
title: 'Error removing lesson from playlist',
description: 'Please refresh the page and try again.',
variant: 'destructive',
})
}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

https://www.loom.com/share/3dd17151bdd94a559db99770466c2293?sid=a6ecd7b2-df5f-404d-9bf4-e86e4d18c7f6

Added some error handling that lets user know something went wrong in a toast

@kodiakhq kodiakhq bot merged commit 8669292 into main Jan 28, 2025
18 checks passed
@kodiakhq kodiakhq bot deleted the zac/remove-post-from-course branch January 28, 2025 17:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants