Implementing core of setting user languages and fetching language specific tasks

This commit is contained in:
Keith Stevens
2023-01-21 21:36:37 +09:00
parent 5f3f0776e9
commit 7274512c2f
10 changed files with 125 additions and 8 deletions
+2
View File
@@ -5,6 +5,7 @@ import { useSession } from "next-auth/react";
import { useTranslation } from "next-i18next";
import { Flags } from "react-feature-flags";
import { FaUser } from "react-icons/fa";
import { LanguageSelector } from "src/components/LanguageSelector";
import { UserMenu } from "./UserMenu";
@@ -45,6 +46,7 @@ export function Header() {
<Flags authorizedFlags={["flagTest"]}>
<Text>FlagTest</Text>
</Flags>
<LanguageSelector />
<AccountButton />
<UserMenu />
</Flex>
@@ -0,0 +1,40 @@
import { Select } from "@chakra-ui/react";
import { useRouter } from "next/router";
import { useTranslation } from "next-i18next";
import { useCallback, useMemo, useState } from "react";
import cookie from "react-cookies";
const LanguageSelector = () => {
const router = useRouter();
const { i18n } = useTranslation();
const { language: currentLanguage } = i18n;
const languageNames = useMemo(() => {
return new Intl.DisplayNames([currentLanguage], {
type: "language",
});
}, [currentLanguage]);
const languageChanged = useCallback(
async (option) => {
const locale = option.target.value;
cookie.save("NEXT_LOCALE", locale, { path: "/" });
const path = router.asPath;
return router.push(path, path, { locale });
},
[router]
);
const locales = router.locales;
return (
<Select onChange={languageChanged} defaultValue={currentLanguage}>
{locales.map((locale) => (
<option key={locale} value={locale}>
{languageNames.of(locale) ?? locale}
</option>
))}
</Select>
);
};
export { LanguageSelector };
@@ -0,0 +1 @@
export * from "./LanguageSelector";
+5 -2
View File
@@ -108,10 +108,11 @@ export class OasstApiClient {
// TODO return a strongly typed Task?
// This method is used to store a task in RegisteredTask.task.
// This is a raw Json type, so we can't use it to strongly type the task.
async fetchTask(taskType: string, user: BackendUserCore): Promise<any> {
async fetchTask(taskType: string, user: BackendUserCore, lang: string): Promise<any> {
return this.post("/api/v1/tasks/", {
type: taskType,
user,
lang,
});
}
@@ -136,7 +137,8 @@ export class OasstApiClient {
messageId: string,
userMessageId: string,
content: object,
user: BackendUserCore
user: BackendUserCore,
lang: string
): Promise<any> {
return this.post("/api/v1/tasks/interaction", {
type: updateType,
@@ -144,6 +146,7 @@ export class OasstApiClient {
task_id: taskId,
message_id: messageId,
user_message_id: userMessageId,
lang,
...content,
});
}
+15 -1
View File
@@ -1,6 +1,20 @@
import parser from "accept-language-parser";
import type { NextApiRequest } from "next";
import prisma from "src/lib/prismadb";
import type { BackendUserCore } from "src/types/Users";
const getUserLanguage = (req: NextApiRequest) => {
const cookieLanguage = req.cookies["NEXT_LOCALE"];
if (cookieLanguage) {
return cookieLanguage;
}
const headerLanguages = parser.parse(req.headers["accept-language"]);
if (headerLanguages.length > 0) {
return headerLanguages[0].code;
}
return "en";
};
/**
* Returns a `BackendUserCore` that can be used for interacting with the Backend service.
*
@@ -35,4 +49,4 @@ const getBackendUserCore = async (id: string) => {
} as BackendUserCore;
};
export { getBackendUserCore };
export { getBackendUserCore, getUserLanguage };
@@ -1,7 +1,7 @@
import { withoutRole } from "src/lib/auth";
import { oasstApiClient } from "src/lib/oasst_api_client";
import prisma from "src/lib/prismadb";
import { getBackendUserCore } from "src/lib/users";
import { getBackendUserCore, getUserLanguage } from "src/lib/users";
/**
* Returns a new task created from the Task Backend. We do a few things here:
@@ -14,11 +14,12 @@ import { getBackendUserCore } from "src/lib/users";
const handler = withoutRole("banned", async (req, res, token) => {
// Fetch the new task.
const { task_type } = req.query;
const userLanguage = getUserLanguage(req);
const user = await getBackendUserCore(token.sub);
let task;
try {
task = await oasstApiClient.fetchTask(task_type as string, user);
task = await oasstApiClient.fetchTask(task_type as string, user, userLanguage);
} catch (err) {
console.error(err);
res.status(500).json(err);
+11 -2
View File
@@ -2,7 +2,7 @@ import { Prisma } from "@prisma/client";
import { withoutRole } from "src/lib/auth";
import { oasstApiClient } from "src/lib/oasst_api_client";
import prisma from "src/lib/prismadb";
import { getBackendUserCore } from "src/lib/users";
import { getBackendUserCore, getUserLanguage } from "src/lib/users";
/**
* Stores the task interaction with the Task Backend and then returns the next task generated.
@@ -41,9 +41,18 @@ const handler = withoutRole("banned", async (req, res, token) => {
});
const user = await getBackendUserCore(token.sub);
const userLanguage = getUserLanguage(req);
let newTask;
try {
newTask = await oasstApiClient.interactTask(update_type, taskId, frontendId, interaction.id, content, user);
newTask = await oasstApiClient.interactTask(
update_type,
taskId,
frontendId,
interaction.id,
content,
user,
userLanguage
);
} catch (err) {
console.error(JSON.stringify(err));
return res.status(500).json(err);