Skip to content

Commit

Permalink
add redirect on delete
Browse files Browse the repository at this point in the history
  • Loading branch information
juandjara committed Oct 7, 2023
1 parent 52bfbba commit 3f358c3
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion app/routes/api/files.$project.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@ import { getProject, getProjectConfig } from "@/lib/projects.server"
import { requireUserSession, setFlashMessage } from "@/lib/session.server"
import { uploadImage } from "@/lib/uploadImage"
import type { ActionArgs, UploadHandlerPart } from "@remix-run/node"
import { json, unstable_composeUploadHandlers, unstable_createMemoryUploadHandler, unstable_parseMultipartFormData } from "@remix-run/node"
import { json, redirect, unstable_composeUploadHandlers, unstable_createMemoryUploadHandler, unstable_parseMultipartFormData } from "@remix-run/node"

export async function action({ params, request }: ActionArgs) {
const { token } = await requireUserSession(request)
const project = await getProject(Number(params.project))
const conf = await getProjectConfig(token, project)
const folder = conf.mediaFolder === '/' ? '' : conf.mediaFolder || ''
const hasRedirect = !!new URL(request.url).searchParams.get('redirect')

// differiantiate between "file upload" and "file edit / delete" using http method to not affect the reading of form data

Expand Down Expand Up @@ -62,6 +63,11 @@ export async function action({ params, request }: ActionArgs) {
newPath = `${getDirname(path)}/${name}`
}

if (path === newPath) {
const cookie = await setFlashMessage(request, `Not moving from ${path} to ${newPath} because it's the same path`)
return json({ ok: true }, { status: 204, headers: { 'Set-Cookie': cookie }})
}

const message = `Move file ${path} to ${newPath}`
await renameFile(token, {
repo: project.repo,
Expand Down Expand Up @@ -90,6 +96,9 @@ export async function action({ params, request }: ActionArgs) {
})

const cookie = await setFlashMessage(request, `Pushed commit "${message}" successfully`)
if (hasRedirect) {
return redirect(`${request.url}/..`, { headers: { 'Set-Cookie': cookie }})
}
return json({ ok: true }, { headers: { 'Set-Cookie': cookie }})
}
}

0 comments on commit 3f358c3

Please sign in to comment.