Added internationalization

This commit is contained in:
Sebastian Seedorf
2022-08-19 19:06:36 +02:00
parent 8227cc631d
commit 33a5b10fe3
13 changed files with 187 additions and 39 deletions

View File

@@ -10,7 +10,7 @@ import {
SetFactoryArrayBody,
UploadDataBody
} from '../../src/types/ApiSchemasFrontend'
import { NextRouter, useRouter } from 'next/router'
import { useRouter } from 'next/router'
interface Props {
children: ReactNodeLike
@@ -93,8 +93,8 @@ interface StoredFile {
excludedSuggestions: string[]
}
export const postFetchJson = async (router: NextRouter, url: string, body: unknown) => {
const res = await fetch(router.basePath + url, {
export const postFetchJson = async (base: string | undefined, url: string, body: unknown) => {
const res = await fetch((base ?? '') + url, {
method: 'POST',
headers: {
'Content-Type': 'application/json'
@@ -105,7 +105,7 @@ export const postFetchJson = async (router: NextRouter, url: string, body: unkno
}
export const GroupProvider: FC<Props> = ({ children, id, initial }) => {
const router = useRouter()
const { basePath, reload, query } = useRouter()
const [excludedSuggestions, _setExcludedSuggestions] = useState<string[]>(initial.ignored)
const [basicValues, _setBasicValues] = useState<string[]>(initial.base)
const [groups, setGroups] = useState<Dict<Group>>(initial.groups)
@@ -125,23 +125,23 @@ export const GroupProvider: FC<Props> = ({ children, id, initial }) => {
const setExcludedSuggestions = useCallback<typeof _setExcludedSuggestions>(
val => {
_setExcludedSuggestions(val)
postFetchJson(router, `/api/${fixedEncodeURIComponent(id)}/factories`, {
postFetchJson(basePath, `/api/${fixedEncodeURIComponent(id)}/factories`, {
type: 'ignored',
factories: val
} as SetFactoryArrayBody).catch(console.error)
},
[id, router]
[id, basePath]
)
const setBasicValues = useCallback<typeof _setBasicValues>(
val => {
_setBasicValues(val)
postFetchJson(router, `/api/${fixedEncodeURIComponent(id)}/factories`, {
postFetchJson(basePath, `/api/${fixedEncodeURIComponent(id)}/factories`, {
type: 'base',
factories: val
} as SetFactoryArrayBody).catch(console.error)
},
[id, router]
[id, basePath]
)
const addGroup = useCallback(
@@ -154,13 +154,13 @@ export const GroupProvider: FC<Props> = ({ children, id, initial }) => {
})
;(async () => {
await postFetchJson(
router,
basePath,
`/api/${fixedEncodeURIComponent(id)}/group/${fixedEncodeURIComponent(name)}/add`,
{}
)
if (exports.length) {
await postFetchJson(
router,
basePath,
`/api/${fixedEncodeURIComponent(id)}/group/${fixedEncodeURIComponent(name)}/factories`,
{
type: 'exports',
@@ -170,7 +170,7 @@ export const GroupProvider: FC<Props> = ({ children, id, initial }) => {
}
if (malls.length) {
await postFetchJson(
router,
basePath,
`/api/${fixedEncodeURIComponent(id)}/group/${fixedEncodeURIComponent(name)}/factories`,
{
type: 'malls',
@@ -181,7 +181,7 @@ export const GroupProvider: FC<Props> = ({ children, id, initial }) => {
})().catch(console.error)
return true
},
[groups, id, router]
[groups, id, basePath]
)
const removeGroup = useCallback(
(name: string) => {
@@ -191,12 +191,12 @@ export const GroupProvider: FC<Props> = ({ children, id, initial }) => {
return { ...g }
})
postFetchJson(
router,
basePath,
`/api/${fixedEncodeURIComponent(id)}/group/${fixedEncodeURIComponent(name)}/remove`,
{}
).catch(console.error)
},
[id, router]
[id, basePath]
)
const renameGroup = useCallback(
(name: string, newName: string) => {
@@ -209,14 +209,14 @@ export const GroupProvider: FC<Props> = ({ children, id, initial }) => {
return { ...g }
})
postFetchJson(
router,
basePath,
`/api/${fixedEncodeURIComponent(id)}/group/${fixedEncodeURIComponent(name)}/rename`,
{
newName
} as GroupRenameBody
).catch(console.error)
},
[groups, id, router]
[groups, id, basePath]
)
const setFactories = useCallback(
@@ -227,7 +227,7 @@ export const GroupProvider: FC<Props> = ({ children, id, initial }) => {
return { ...g }
})
postFetchJson(
router,
basePath,
`/api/${fixedEncodeURIComponent(id)}/group/${fixedEncodeURIComponent(name)}/factories`,
{
type,
@@ -235,7 +235,7 @@ export const GroupProvider: FC<Props> = ({ children, id, initial }) => {
} as GroupSetFactoryArrayBody
).catch(console.error)
},
[id, router]
[id, basePath]
)
const getInputType = useCallback(
(uid: string) => {
@@ -263,14 +263,14 @@ export const GroupProvider: FC<Props> = ({ children, id, initial }) => {
const uncompressed = pako.inflate(compressed, { to: 'string' })
const value: StoredFile = JSON.parse(uncompressed)
if (!value.groups || !value.basicValues || !value.excludedSuggestions) return
await postFetchJson(router, `/api/${router.query.id}/upload`, {
await postFetchJson(basePath, `/api/${query.id}/upload`, {
groups: value.groups,
ignored: value.excludedSuggestions,
base: value.basicValues
} as UploadDataBody)
router.reload()
reload()
},
[router]
[basePath, query.id, reload]
)
const value: GroupContextType = useMemo(