Skip to content

Commit 4b6fc4d

Browse files
committed
keydata upload form and route
1 parent c1762b7 commit 4b6fc4d

File tree

7 files changed

+242
-61
lines changed

7 files changed

+242
-61
lines changed

client/components/UsersPage/index.jsx

Lines changed: 37 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,18 @@
11
import React, { useEffect } from 'react'
2+
import axios from 'axios'
23
import { useDispatch, useSelector } from 'react-redux'
34
import { Tab } from 'semantic-ui-react'
45
import { useTranslation } from 'react-i18next'
56
import { getAllUsersAction } from '../../util/redux/usersReducer'
6-
import { isSuperAdmin } from '../../../config/common'
7+
import { basePath, isSuperAdmin } from '../../../config/common'
78
import UserTable from './UserTable'
89
import IamTable from './IamTable'
910
import DeadlineInfo from './DeadlineInfo'
1011
import DeadlineSetting from './DeadlineSetting'
1112
import UpdateStudyprogrammes from './UpdateStudyprogrammes'
1213
import TempAccess from './TempAccess'
1314
import Debug from './Debug'
15+
import { uploadKeyData } from '@/client/util/redux/keyDataReducer'
1416

1517
export default () => {
1618
const { t } = useTranslation()
@@ -65,28 +67,44 @@ export default () => {
6567
panes = [
6668
...panes,
6769
{
68-
menuItem: t('users:updateStudyprogrammes'),
69-
render: () => (
70-
<Tab.Pane>
71-
<UpdateStudyprogrammes />
72-
</Tab.Pane>
73-
),
70+
menuItem: t('users:updateStudyprogrammes'),
71+
render: () => (
72+
<Tab.Pane>
73+
<UpdateStudyprogrammes />
74+
</Tab.Pane>
75+
),
76+
},
77+
{
78+
menuItem: t('users:deadlineSettings'),
79+
render: () => (
80+
<Tab.Pane>
81+
<DeadlineSetting />
82+
</Tab.Pane>
83+
),
7484
},
7585
{
76-
menuItem: t('users:deadlineSettings'),
77-
render: () => (
78-
<Tab.Pane>
79-
<DeadlineSetting />
80-
</Tab.Pane>
81-
),
86+
menuItem: 'Debug',
87+
render: () => (
88+
<Tab.Pane>
89+
<Debug />
90+
</Tab.Pane>
91+
),
8292
},
8393
{
84-
menuItem: 'Debug',
85-
render: () => (
86-
<Tab.Pane>
87-
<Debug />
88-
</Tab.Pane>
89-
),
94+
menuItem: t('users:uploadKeydata'),
95+
render: () => (
96+
<Tab.Pane>
97+
<form
98+
onSubmit={async e => {
99+
e.preventDefault()
100+
const file = e.target.elements.file.files[0]
101+
dispatch(uploadKeyData(file))}}
102+
>
103+
<input type="file" name="file" />
104+
<button type="submit">Upload</button>
105+
</form>
106+
</Tab.Pane>
107+
),
90108
},
91109
]
92110
}

client/util/redux/keyDataReducer.ts

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1+
import axios from 'axios'
12
import callBuilder from '../apiConnection'
3+
import { inProduction, basePath } from '../common'
4+
import { getHeaders } from '../../../config/mockHeaders'
25
interface State {
36
data: any;
47
pending?: boolean;
@@ -16,6 +19,30 @@ export const fetchKeyData = () => {
1619
return callBuilder(route, prefix)
1720
}
1821

22+
export const uploadKeyData = (file: any) => {
23+
return async (dispatch: any) => {
24+
const route = `${basePath}api/keydata`
25+
const defaultHeaders = !inProduction ? getHeaders() : {}
26+
const prefix = 'POST_KEY_DATA'
27+
const formData = new FormData()
28+
formData.append('file', file)
29+
30+
dispatch({ type: `${prefix}_ATTEMPT` })
31+
32+
try {
33+
await axios.post(route, formData, {
34+
headers: {
35+
'Content-Type': 'multipart/form-data',
36+
...defaultHeaders,
37+
},
38+
})
39+
dispatch({ type: `${prefix}_SUCCESS` })
40+
} catch (error) {
41+
console.error(error);
42+
dispatch({ type: `${prefix}_FAILURE` })
43+
}
44+
};
45+
};
1946
export default (state: State = { data: null }, action: Action) => {
2047
switch (action.type) {
2148
case 'GET_KEY_DATA_ATTEMPT':

package-lock.json

Lines changed: 130 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
"module-alias": "^2.2.2",
5454
"moment": "^2.29.4",
5555
"morgan": "^1.10.0",
56+
"multer": "^1.4.5-lts.1",
5657
"node-cron": "^3.0.3",
5758
"pg": "^8.6.0",
5859
"pg-hstore": "^2.3.3",
@@ -91,6 +92,7 @@
9192
"devDependencies": {
9293
"@types/compression": "^1.7.5",
9394
"@types/lodash": "^4.17.14",
95+
"@types/multer": "^1.4.12",
9496
"@types/node": "^22.10.6",
9597
"@types/react": "^19.0.7",
9698
"@types/react-dom": "^19.0.3",

0 commit comments

Comments
 (0)