-
Notifications
You must be signed in to change notification settings - Fork 42
feat(egh): remove post from course post #397
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎
1 Skipped Deployment
|
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, | ||
}) | ||
}} |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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
return await db | ||
.delete(contentResourceResource) | ||
.where( | ||
and( | ||
eq(contentResourceResource.resourceOfId, resourceOfId), | ||
eq(contentResourceResource.resourceId, postId), | ||
), | ||
) |
There was a problem hiding this comment.
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
?
There was a problem hiding this comment.
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
{item.type === 'section' && item.children.length === 0 && ( | ||
{item.type === 'post' && ( |
There was a problem hiding this comment.
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
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() | ||
}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
the syncing work
There was a problem hiding this comment.
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...
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.
There was a problem hiding this 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
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() | ||
}) |
There was a problem hiding this comment.
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...
return await db | ||
.delete(contentResourceResource) | ||
.where( | ||
and( | ||
eq(contentResourceResource.resourceOfId, resourceOfId), | ||
eq(contentResourceResource.resourceId, postId), | ||
), | ||
) |
There was a problem hiding this comment.
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
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, | ||
}) | ||
}} |
There was a problem hiding this comment.
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
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', | ||
}) | ||
} |
There was a problem hiding this comment.
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
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