Newer
Older
import axios from "axios";
import i18next from "i18next";
import configApiCall from "api";
import { api_path_blueprints } from "globalUrls";
import { debounce } from "lodash";
import { DEFAULT_UI_CUSTOMIZATION, PolicyData } from "./policyData.constants";
import { Dispatch, SetStateAction } from "react";
import { SnackbarProps } from "./PolicyDataContext";
import { ServerUiCustomization } from "./ServerUiCustomization";
const updatePerms = (data, field: string, value: any) => {
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
data.defaultModerators = data.blueprintModerators
.map((moderator) => moderator.id)
.join("/");
delete data.blueprintModerators;
if (field === "peerDiscovery") {
data.accountDiscovery = value;
data.accountPublish = value;
}
return data;
};
const updateConfig = (data) => {
const { selectedTurnOption, selectedDHTProxyOption } = data;
// if undefined, it means we want the default values
if (selectedTurnOption !== "customTurn") {
delete data.turnServer;
delete data.turnServerUserName;
delete data.turnServerPassword;
}
if (selectedDHTProxyOption !== "customDHTProxy") {
delete data.proxyServer;
delete data.dhtProxyListUrl;
}
if (selectedTurnOption === "customTurn") {
data.turnEnabled = true;
} else if (selectedTurnOption === "disabledTurn") {
data.turnEnabled = false;
}
delete data.selectedTurnOption;
if (selectedDHTProxyOption === "customDHTProxy") {
data.proxyEnabled = true;
} else if (selectedDHTProxyOption === "disabledDHTProxy") {
data.proxyEnabled = false;
}
delete data.selectedDHTProxyOption;
return data;
};
const convertRgbaToArgb = (rgba: string) => {
if (rgba.length === 7) {
return rgba;
}
if (rgba.length !== 9) {
throw new Error(`Invalid rgba value: "${rgba}}"`);
}
return "#" + rgba.substring(7, 9) + rgba.substring(1, 7);
const updateUiCustomization = (data) => {
if (data.uiCustomization.isCustomizationEnabled === false) {
return data;
}
const {
hasTitle,
title,
hasDescription,
description,
hasTips,
hasBackground,
backgroundColor,
backgroundUrl,
tipBoxAndIdColor,
hasMainBoxColor,
mainBoxColor,
} = data.uiCustomization;
const ui = {};
if (hasTitle === false) {
ui.title = "";
} else if (title !== "") {
ui.title = title;
}
if (hasDescription === false) {
ui.description = "";
} else if (description !== "") {
ui.description = description;
}
ui.areTipsEnabled = hasTips;
if (hasBackground === false) {
ui.backgroundType = "default";
ui.backgroundType = "image";
ui.backgroundColorOrUrl = backgroundUrl;
} else if (backgroundColor !== "") {
ui.backgroundType = "color";
ui.backgroundColorOrUrl = backgroundColor || "#ffffff";
} else {
ui.backgroundType = "default";
}
if (tipBoxAndIdColor !== DEFAULT_UI_CUSTOMIZATION.tipBoxAndIdColor) {
ui.tipBoxAndIdColor = convertRgbaToArgb(tipBoxAndIdColor);
}
if (hasMainBoxColor === true && mainBoxColor) {
ui.mainBoxColor = convertRgbaToArgb(mainBoxColor);
}
if (hasLogo === false) {
ui.logoUrl = "";
} else if (logoUrl !== "") {
ui.logoUrl = logoUrl;
}
if (logoSize !== DEFAULT_UI_CUSTOMIZATION.logoSize) {
ui.logoSize = logoSize;
}
console.log("updateUiCustomization", ui);
data.uiCustomization = JSON.stringify(ui);
return data;
};
const sendPutRequest = (
blueprintName: string,
data: ServerUiCustomization,
setSnackbar: (snackbar: SnackbarProps) => void
) => {
console.log("PUT", data);
axios(
configApiCall(
api_path_blueprints + "?name=" + blueprintName,
"PUT",
data,
null
)
)
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
const message = i18next.t(
"updated_blueprint_permissions_successfully",
"Blueprint permissions successfully updated."
);
setSnackbar({
open: true,
severity: "success",
message,
});
})
.catch((error) => {
const msg = i18next.t(
"error_updating_blueprint_permissions",
"Error occurred while updating blueprint permissions."
);
setSnackbar({
open: true,
severity: "error",
message: `${msg} ${error}!`,
});
});
};
const debouncedSendPutRequest = debounce(sendPutRequest, 200, {
trailing: true,
});
export const _updatePolicyData = (
blueprintName: string,
policyData: PolicyData,
setPolicyData: Dispatch<SetStateAction<PolicyData>>,
field: string,
value: string,
setSnackbar: (snackbar: any) => void
) => {
setPolicyData((state) => ({ ...state, [field]: value }));
let data: any = { ...policyData, [field]: value };
data = updatePerms(data, field, value);
data = updateConfig(data);
data = updateUiCustomization(data);
const ui: ServerUiCustomization = data;
console.log("updatePolicyData", { field, value });
debouncedSendPutRequest(blueprintName, ui, setSnackbar);