Skip to content

Commit

Permalink
delete drafts on project deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
juandjara committed Oct 16, 2023
1 parent f3acb3b commit c6a38a6
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
32 changes: 21 additions & 11 deletions app/lib/projects.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,15 @@ export async function updateProject(project: Project) {

export async function deleteProject(project: Project) {
return withRedis(async (db) => {
const deleteDraftsCommand = db.pipeline()
const draftKeys = await db.smembers(`drafts:${project.repo}`)
draftKeys.forEach((key) => deleteDraftsCommand.del(key))
await deleteDraftsCommand.exec()

await Promise.all([
db.srem(`projects:${project.user}`, project.id),
db.del(`project:${project.id}`),
db.del(`repo:${project.repo}`)
db.del(`repo:${project.repo}`),
])
})
}
Expand Down Expand Up @@ -268,6 +273,7 @@ export async function saveDraft(params: SaveDraftParams) {
return withRedis(async (db) => {
const { project, file } = params
const key = `draft:${project.id}:${encodeURIComponent(file.path)}`
await db.sadd(`drafts:${project.repo}`, key)
await db.set(key, JSON.stringify(file))
return key
})
Expand All @@ -280,22 +286,26 @@ export async function getDraft(projectId: number, path: string) {
})
}

export async function deleteDraft(projectId: number, path: string) {
export async function deleteDraft(project: Project, path: string) {
return withRedis(async (db) => {
await db.del(`draft:${projectId}:${encodeURIComponent(path)}`)
const key = `draft:${project.id}:${encodeURIComponent(path)}`
await db.srem(`drafts:${project.repo}`, key)
await db.del(key)
})
}

export async function renameDraft(projectId: number, oldPath: string, newPath: string) {
export async function renameDraft(project: Project, oldPath: string, newPath: string) {
const draft = await getDraft(project.id, oldPath)
if (!draft) {
return
}

return withRedis(async (db) => {
const draft = await getDraft(projectId, oldPath)
if (!draft) {
return
}

await Promise.all([
db.set(`draft:${projectId}:${encodeURIComponent(newPath)}`, JSON.stringify(draft)),
db.del(`draft:${projectId}:${encodeURIComponent(oldPath)}`),
db.set(`draft:${project.id}:${encodeURIComponent(newPath)}`, JSON.stringify(draft)),
db.sadd(`drafts:${project.repo}`, `draft:${project.id}:${encodeURIComponent(newPath)}`),
db.del(`draft:${project.id}:${encodeURIComponent(oldPath)}`),
db.srem(`drafts:${project.repo}`, `draft:${project.id}:${encodeURIComponent(oldPath)}`),
])
})
}
4 changes: 2 additions & 2 deletions app/routes/api/files.$project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export async function action({ params, request }: ActionArgs) {
message
})

await renameDraft(project.id, path, newPath)
await renameDraft(project, path, newPath)

const cookie = await setFlashMessage(request, `Pushed commit "${message}" successfully`)
if (redirectTarget === 'source') {
Expand Down Expand Up @@ -74,7 +74,7 @@ export async function action({ params, request }: ActionArgs) {
path,
})

await deleteDraft(project.id, path)
await deleteDraft(project, path)

const cookie = await setFlashMessage(request, `Pushed commit "${message}" successfully`)
if (redirectTarget === 'source') {
Expand Down
4 changes: 2 additions & 2 deletions app/routes/p/$project/$cid/$pid.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ export async function action({ request, params }: ActionArgs) {

const isDeleteDraft = formData.get('delete_draft') === 'true'
if (isDeleteDraft) {
await deleteDraft(project.id, fullPath)
await deleteDraft(project, fullPath)
const cookie = await setFlashMessage(request, `Draft for "${getBasename(fullPath)}" deleted successfully`)
return json({ ok: true }, {
headers: {
Expand All @@ -145,7 +145,7 @@ export async function action({ request, params }: ActionArgs) {
})
}

await deleteDraft(project.id, fullPath)
await deleteDraft(project, fullPath)
}

try {
Expand Down

0 comments on commit c6a38a6

Please sign in to comment.