Skip to content

Commit

Permalink
feat: wrap next approved verse around
Browse files Browse the repository at this point in the history
  • Loading branch information
arrocke committed Nov 17, 2024
1 parent 2a3d767 commit d259bfc
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { bookFirstVerseId, bookLastVerseId, decrementVerseId, incrementVerseId }
import { useTranslationClientState } from "./TranslationClientState";
import TranslationProgressBar from "./TranslationProgressBar";
import { useSWRConfig } from "swr";
import { useFlash } from "@/app/flash";

export interface TranslationToolbarProps {
languages: { name: string; code: string }[];
Expand All @@ -27,6 +28,7 @@ export default function TranslationToolbar({
const { verseId, code, locale } = useParams<{ locale: string, code: string, verseId: string }>()
const router = useRouter()
const { mutate } = useSWRConfig()
const flash = useFlash()

const isTranslator = !!currentLanguage?.roles.includes('TRANSLATOR');
const isAdmin = !!currentLanguage?.roles.includes('ADMIN');
Expand All @@ -45,11 +47,14 @@ export default function TranslationToolbar({
setReference(t('verse_reference', { bookId, chapter, verse }))
}, [verseId, t])

const navigateToNextUnapprovedVerse = useCallback(() => {
const navigateToNextUnapprovedVerse = useCallback(async () => {
const form = new FormData()
form.set('verseId', verseId)
form.set('code', code)
redirectToUnapproved(form)
const error = await redirectToUnapproved(form)
if (error) {
flash.success(error)
}
}, [verseId, code])

const approveAllGlosses = useCallback(async () => {
Expand Down
30 changes: 28 additions & 2 deletions app/[locale]/(authenticated)/translate/[code]/actions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ const redirectToUnapprovedSchema = z.object({
verseId: z.string()
})

export async function redirectToUnapproved(formData: FormData): Promise<void> {
export async function redirectToUnapproved(formData: FormData): Promise<void | string> {
const request = redirectToUnapprovedSchema.safeParse(parseForm(formData));
if (!request.success) {
return
}

const result = await query<{ nextUnapprovedVerseId: string }>(
let result = await query<{ nextUnapprovedVerseId: string }>(
`
SELECT w."verseId" as "nextUnapprovedVerseId"
FROM "Word" AS w
Expand All @@ -65,6 +65,32 @@ export async function redirectToUnapproved(formData: FormData): Promise<void> {
[request.data.code, request.data.verseId]
)

if (result.rows.length === 0) {
result = await query<{ nextUnapprovedVerseId: string }>(
`
SELECT w."verseId" as "nextUnapprovedVerseId"
FROM "Word" AS w
LEFT JOIN LATERAL (
SELECT g.state AS state FROM "PhraseWord" AS phw
JOIN "Phrase" AS ph ON ph.id = phw."phraseId"
LEFT JOIN "Gloss" AS g ON g."phraseId" = ph.id
WHERE phw."wordId" = w.id
AND ph."languageId" = (SELECT id FROM "Language" WHERE code = $1)
AND ph."deletedAt" IS NULL
) AS g ON true
WHERE (g."state" = 'UNAPPROVED' OR g."state" IS NULL)
ORDER BY w."id"
LIMIT 1
`,
[request.data.code]
)

if (result.rows.length === 0) {
const t = await getTranslations('TranslationToolbar')
return t('errors.all_approved')
}
}

const locale = await getLocale();
redirect(`/${locale}/translate/${request.data.code}/${result.rows[0].nextUnapprovedVerseId}`)
}
Expand Down
3 changes: 3 additions & 0 deletions messages/ar.json
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,9 @@
]
},
"TranslationToolbar": {
"errors": {
"all_approved": "تمت الموافقة على جميع التفسيرات"
},
"verse": "الآية",
"previous_verse": "الآية السابقة",
"next_verse": "الآية التالية",
Expand Down
5 changes: 4 additions & 1 deletion messages/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -527,6 +527,9 @@
]
},
"TranslationToolbar": {
"errors": {
"all_approved": "All glosses have been approved"
},
"verse": "Verse",
"previous_verse": "Previous Verse",
"next_verse": "Next Verse",
Expand Down Expand Up @@ -611,4 +614,4 @@
"not_found": "Not Found",
"close": "Close"
}
}
}

0 comments on commit d259bfc

Please sign in to comment.