Skip to content

Commit 8f72174

Browse files
committed
fix metadata payload structure for update request
1 parent a40db52 commit 8f72174

File tree

4 files changed

+56
-41
lines changed

4 files changed

+56
-41
lines changed

src/configs/tableConfigs/usersTableConfig.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import { User } from "../../slices/userSlice";
12
import { TableConfig } from "./aclsTableConfig";
23

34
/**

src/slices/userInfoSlice.ts

Lines changed: 25 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { PayloadAction, SerializedError, createSlice } from '@reduxjs/toolkit'
2-
import axios from 'axios';
2+
import axios, { AxiosError } from 'axios';
33
import { addNotification } from './notificationSlice';
44
import { createAppAsyncThunk } from '../createAsyncThunkWithTypes';
55

@@ -73,26 +73,31 @@ const initialState: UserInfoState = {
7373
},
7474
};
7575

76-
export const fetchUserInfo = createAppAsyncThunk('UserInfo/fetchUserInfo', async (_, { dispatch }) => {
77-
// Just make the async request here, and return the response.
78-
// This will automatically dispatch a `pending` action first,
79-
// and then `fulfilled` or `rejected` actions based on the promise.
80-
const res = await axios.get("/info/me.json")
81-
.then((response) => {
82-
return response.data;
83-
})
84-
.catch((response) => {
85-
console.error(response);
86-
dispatch(addNotification({type: "error", key: "USER_NOT_SAVED"}));
87-
});
88-
89-
// Redirect to login if not in ROLE_ADMIN_UI
90-
if (!(res.roles.includes('ROLE_ADMIN') || res.roles.includes('ROLE_ADMIN_UI'))) {
91-
window.location.href = "/login.html";
76+
export const fetchUserInfo = createAppAsyncThunk(
77+
'UserInfo/fetchUserInfo',
78+
async (_, { dispatch }) => {
79+
try {
80+
const response = await axios.get("/info/me.json");
81+
const res = response.data;
82+
if (!res || !(res.roles?.includes('ROLE_ADMIN') || res.roles?.includes('ROLE_ADMIN_UI'))) {
83+
window.location.href = "/login.html";
84+
}
85+
return res;
86+
} catch (err: unknown) {
87+
const error = err as AxiosError;
88+
// eslint-disable-next-line no-trailing-spaces
89+
console.error(error);
90+
const status = error?.response?.status;
91+
if (status === 401 || status === 403) {
92+
window.location.href = "/login.html";
93+
// eslint-disable-next-line no-trailing-spaces
94+
}
95+
dispatch(addNotification({ type: "error", key: "USER_NOT_SAVED" }));
96+
throw error;
97+
}
9298
}
93-
94-
return res;
95-
});
99+
// eslint-disable-next-line no-trailing-spaces
100+
);
96101

97102
export const fetchOcVersion = createAppAsyncThunk('UserInfo/fetchOcVersion', async () => {
98103
const res = await axios.get("/sysinfo/bundles/version?prefix=opencast");

src/slices/userSlice.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,7 @@ export const fetchUsers = createAppAsyncThunk('users/fetchUsers', async (_, { ge
6969
// This will automatically dispatch a `pending` action first,
7070
// and then `fulfilled` or `rejected` actions based on the promise.
7171
const res = await axios.get("/admin-ng/users/users.json", { params: params });
72+
console.log("this is the response", res);
7273
return res.data;
7374
});
7475

src/utils/resourceUtils.ts

Lines changed: 29 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -166,37 +166,45 @@ export const transformMetadataFields = (metadata: MetadataField[]) => {
166166
};
167167

168168
// transform metadata catalog for update via post request
169-
export const transformMetadataForUpdate = (catalog: MetadataCatalog, values: { [key: string]: MetadataCatalog["fields"][0]["value"] }) => {
169+
export const transformMetadataForUpdate = (
170+
catalog: MetadataCatalog,
171+
values: { [key: string]: MetadataCatalog["fields"][0]["value"] }
172+
) => {
170173
let fields: MetadataCatalog["fields"] = [];
171-
let updatedFields: MetadataCatalog["fields"] = [];
174+
let updatedFields: { id: string; value: unknown }[] = [];
172175

173176
catalog.fields.forEach((field) => {
174-
if (field.value !== values[field.id]) {
175-
let updatedField = {
176-
...field,
177-
value: values[field.id],
178-
};
179-
updatedFields.push(updatedField);
180-
fields.push(updatedField);
181-
} else {
182-
fields.push({ ...field });
183-
}
177+
const newValue = values[field.id];
178+
179+
// update UI state with full field (optional)
180+
const fullField = { ...field, value: newValue };
181+
fields.push(fullField);
182+
183+
// only include minimal clean data for backend if value changed
184+
if (field.value !== newValue) {
185+
updatedFields.push({
186+
id: field.id,
187+
value: newValue,
188+
});
189+
}
184190
});
191+
185192
let data = new URLSearchParams();
186193
data.append(
187-
"metadata",
188-
JSON.stringify([
189-
{
190-
flavor: catalog.flavor,
191-
title: catalog.title,
192-
fields: updatedFields,
193-
},
194-
])
194+
"metadata",
195+
JSON.stringify([
196+
{
197+
flavor: catalog.flavor,
198+
title: catalog.title,
199+
fields: updatedFields,
200+
},
201+
])
195202
);
203+
196204
const headers = getHttpHeaders();
197205

198206
return { fields, data, headers };
199-
};
207+
};
200208

201209
// Prepare metadata for post of new events or series
202210
export const prepareMetadataFieldsForPost = (

0 commit comments

Comments
 (0)