Skip to content

Commit

Permalink
[Shared] Add language codes
Browse files Browse the repository at this point in the history
  • Loading branch information
rikurauhala committed Aug 20, 2024
1 parent 8b8f80b commit 2d3372d
Show file tree
Hide file tree
Showing 7 changed files with 10 additions and 10 deletions.
6 changes: 4 additions & 2 deletions services/backend/src/routes/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { roles } from '../config/roles'
import * as auth from '../middleware/auth'
import { sendNotificationAboutAccessToUser, previewNotificationAboutAccessToUser } from '../services/mailService'
import * as userService from '../services/userService'
import { LANGUAGE_CODES } from '../shared/language'
import { Language } from '../types'
import logger from '../util/logger'

const router = Router()
Expand Down Expand Up @@ -101,15 +103,15 @@ router.delete('/:uid/elements', auth.roles(['admin']), async (req: ElementsReque

interface ChangeLanguageRequest extends Request {
body: {
language: string
language: Language
}
}

router.post('/language', async (req: ChangeLanguageRequest, res: Response) => {
const {
body: { language },
} = req
if (!['fi', 'sv', 'en'].includes(language)) {
if (!LANGUAGE_CODES.includes(language)) {
return res.status(400).json('Invalid language')
}
try {
Expand Down
2 changes: 1 addition & 1 deletion services/frontend/src/components/LanguagePicker/index.jsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Dropdown } from 'semantic-ui-react'

import { LANGUAGE_CODES } from '@/constants'
import { LANGUAGE_CODES } from '@/shared/language'
import { useLanguage } from './useLanguage'

export const LanguagePicker = () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import { createContext, useState, useCallback, useContext, useEffect } from 'react'

import { DEFAULT_LANG, LANGUAGE_CODES } from '@/constants'
import { useGetAuthorizedUserQuery } from '@/redux/auth'
import { useModifyLanguageMutation } from '@/redux/users'
import { DEFAULT_LANG, LANGUAGE_CODES } from '@/shared/language'

const LanguageContext = createContext([[], () => {}])
LanguageContext.displayName = 'Language'
Expand Down
4 changes: 0 additions & 4 deletions services/frontend/src/constants/index.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
export const DEFAULT_LANG = 'fi'

export const PRIORITYCODE_TEXTS = {
1: 'Main',
2: 'Secondary',
Expand All @@ -10,5 +8,3 @@ export const PRIORITYCODE_TEXTS = {
// Increment this if search history code changes
// so it will be reseted for all users.
export const SEARCH_HISTORY_VERSION = '1.0'

export const LANGUAGE_CODES = ['fi', 'en', 'sv']
2 changes: 1 addition & 1 deletion services/frontend/src/redux/settings.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DEFAULT_LANG } from '@/constants'
import { DEFAULT_LANG } from '@/shared/language'

const initialState = {
language: DEFAULT_LANG,
Expand Down
2 changes: 2 additions & 0 deletions services/shared/language.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const DEFAULT_LANG = 'fi'
export const LANGUAGE_CODES = ['fi', 'en', 'sv'] as const
2 changes: 1 addition & 1 deletion services/shared/tsconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,5 @@
"inlineSources": true,
"resolveJsonModule": true
},
"include": ["util/**/*", "index.ts"]
"include": ["**/*.ts"]
}

0 comments on commit 2d3372d

Please sign in to comment.