Skip to content

Commit

Permalink
Merge pull request #195 from TouK/NU-1103/prettier
Browse files Browse the repository at this point in the history
NU-1103 prettier project
  • Loading branch information
bohdanprog authored Oct 2, 2023
2 parents 18319b9 + e71794a commit bcec68e
Show file tree
Hide file tree
Showing 57 changed files with 2,422 additions and 2,961 deletions.
14 changes: 7 additions & 7 deletions configs/webpack/prod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ import ZipPlugin from "zip-webpack-plugin";
import { commonConfig } from "./common";

export const prodConfig = merge(commonConfig, {
mode: "production",
output: {
filename: "js/[name]_[contenthash].min.js",
path: resolve(__dirname, "../../dist"),
},
devtool: "source-map",
plugins: [new ZipPlugin({ filename: "petasos.zip" })],
mode: "production",
output: {
filename: "js/[name]_[contenthash].min.js",
path: resolve(__dirname, "../../dist"),
},
devtool: "source-map",
plugins: [new ZipPlugin({ filename: "petasos.zip" })],
});

export default prodConfig;
91 changes: 40 additions & 51 deletions src/api.ts
Original file line number Diff line number Diff line change
@@ -1,68 +1,57 @@
const ACCESS_TOKEN_NAMESPACE = "accessToken";

const addHeaders =
(customHeaders: Record<string, string>) =>
({ headers, ...req }: RequestInit) => ({
...req,
headers: {
...headers,
...customHeaders,
},
});
(customHeaders: Record<string, string>) =>
({ headers, ...req }: RequestInit) => ({
...req,
headers: {
...headers,
...customHeaders,
},
});

const addTokenHeader = (token: string) =>
addHeaders({ Authorization: `Bearer ${token}` });
const addTokenHeader = (token: string) => addHeaders({ Authorization: `Bearer ${token}` });

const withContentType = addHeaders({ "Content-Type": "application/json" });

export const fetchSecured = async <R>(
url: string,
init: RequestInit = {}
): Promise<R> => {
const storageToken = localStorage.getItem(ACCESS_TOKEN_NAMESPACE);
const queryParamToken = getQueryParameters()[ACCESS_TOKEN_NAMESPACE];
const token = queryParamToken ? queryParamToken : storageToken;
if (queryParamToken) {
localStorage.setItem(ACCESS_TOKEN_NAMESPACE, queryParamToken);
window.history.replaceState(null, null, window.location.pathname);
}
const withAuth = addTokenHeader(token);
return await fetchJson(url, withAuth(init));
export const fetchSecured = async <R>(url: string, init: RequestInit = {}): Promise<R> => {
const storageToken = localStorage.getItem(ACCESS_TOKEN_NAMESPACE);
const queryParamToken = getQueryParameters()[ACCESS_TOKEN_NAMESPACE];
const token = queryParamToken ? queryParamToken : storageToken;
if (queryParamToken) {
localStorage.setItem(ACCESS_TOKEN_NAMESPACE, queryParamToken);
window.history.replaceState(null, null, window.location.pathname);
}
const withAuth = addTokenHeader(token);
return await fetchJson(url, withAuth(init));
};

export const fetchJson = async <R>(
url: string,
init: RequestInit = {}
): Promise<R> => {
const response = await fetch(url, withContentType(init));
export const fetchJson = async <R>(url: string, init: RequestInit = {}): Promise<R> => {
const response = await fetch(url, withContentType(init));

let json: R & { error?: unknown; message?: unknown };
let json: R & { error?: unknown; message?: unknown };

try {
json = await response.json();
} catch {
return;
}
try {
json = await response.json();
} catch {
return;
}

if (!response.ok || json.error) {
throw json.message || json.error;
}
return json as R;
if (!response.ok || json.error) {
throw json.message || json.error;
}
return json as R;
};

export const getQueryParameters = () => {
const queryStringKeyValue = window.location.search
.replace("?", "")
.split("&");
return queryStringKeyValue.reduce((acc, curr) => {
const [key, value] = curr.split("=");
return {
...acc,
[key]: value,
};
}, {});
const queryStringKeyValue = window.location.search.replace("?", "").split("&");
return queryStringKeyValue.reduce((acc, curr) => {
const [key, value] = curr.split("=");
return {
...acc,
[key]: value,
};
}, {});
};

export const fetchFn = Object.keys(getQueryParameters()).length
? fetchSecured
: fetchJson;
export const fetchFn = Object.keys(getQueryParameters()).length ? fetchSecured : fetchJson;
14 changes: 7 additions & 7 deletions src/components/InfoPopover.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import React from "react";
import { DarkTooltip, SmallIconButton } from "./styledMuiComponents";

export const InfoPopover = ({ info }: { info: string }) => {
return (
<DarkTooltip title={info} placement="left">
<SmallIconButton size="small" type="button" color="primary">
<HelpIcon />
</SmallIconButton>
</DarkTooltip>
);
return (
<DarkTooltip title={info} placement="left">
<SmallIconButton size="small" type="button" color="primary">
<HelpIcon />
</SmallIconButton>
</DarkTooltip>
);
};
46 changes: 23 additions & 23 deletions src/components/addClonedTopicDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,29 @@ import { DEFAULT_TOPIC_VALUES } from "./addTopicDialog";
import { TopicDialog } from "./topicDialog";

export const AddClonedTopicDialog = observer(() => {
const { groups, dialogs } = useStore();
const dialog = dialogs.addClonedTopic;
const { topic } = dialog.params;
const { groups, dialogs } = useStore();
const dialog = dialogs.addClonedTopic;
const { topic } = dialog.params;

const initialValues = (): TopicFormikValues =>
topic
? {
advancedValues: {
acknowledgement: topic.ack,
trackingEnabled: topic.trackingEnabled,
maxMessageSize: topic.maxMessageSize,
retentionTime: topic.retentionTime.duration,
},
topic: "",
schema: topic.schemaPrettified,
group: groups.getGroupOfTopic(topic.name),
description: topic.description,
...getTopicData(topic),
}
: {
...DEFAULT_TOPIC_VALUES,
group: groups.defaultGroup,
};
const initialValues = (): TopicFormikValues =>
topic
? {
advancedValues: {
acknowledgement: topic.ack,
trackingEnabled: topic.trackingEnabled,
maxMessageSize: topic.maxMessageSize,
retentionTime: topic.retentionTime.duration,
},
topic: "",
schema: topic.schemaPrettified,
group: groups.getGroupOfTopic(topic.name),
description: topic.description,
...getTopicData(topic),
}
: {
...DEFAULT_TOPIC_VALUES,
group: groups.defaultGroup,
};

return <TopicDialog initialValues={initialValues} dialog={dialog} />;
return <TopicDialog initialValues={initialValues} dialog={dialog} />;
});
97 changes: 43 additions & 54 deletions src/components/addGroupDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,64 +9,53 @@ import { ValidationError } from "../store/topics";
import { DialogTemplate } from "./dialogTemplate";

export const AddGroupDialog = () => {
const { dialogs, groups } = useStore();
const { dialogs, groups } = useStore();

const basicFields = (): JSX.Element[] => [
<Field
required
component={TextField}
label="Group name"
name="name"
key="name"
style={{ width: "100%" }}
/>,
];
const basicFields = (): JSX.Element[] => [
<Field required component={TextField} label="Group name" name="name" key="name" style={{ width: "100%" }} />,
];

return useObserver(() => {
const validateFunc = (values: GroupFormValues) => {
const errors: FormikErrors<GroupFormValues> = {};
if (!values.name) {
errors.name = "Required";
}
return errors;
};
return useObserver(() => {
const validateFunc = (values: GroupFormValues) => {
const errors: FormikErrors<GroupFormValues> = {};
if (!values.name) {
errors.name = "Required";
}
return errors;
};

const taskOnSubmit = async (
values: GroupFormValues
): Promise<void | ValidationError> => {
if (!values.name) {
console.error("Something went extremely wrong.");
return;
}
return groups.addTask(values.name);
};
const taskOnSubmit = async (values: GroupFormValues): Promise<void | ValidationError> => {
if (!values.name) {
console.error("Something went extremely wrong.");
return;
}
return groups.addTask(values.name);
};

const onSubmitSuccess = async (
values: GroupFormValues
): Promise<string> => {
await groups.fetchTask();
return values.name;
};
const onSubmitSuccess = async (values: GroupFormValues): Promise<string> => {
await groups.fetchTask();
return values.name;
};

const initialValues = (): GroupFormValues => ({
name: "",
...getGroupData(),
});
const initialValues = (): GroupFormValues => ({
name: "",
...getGroupData(),
});

const dialog = dialogs.group;
return (
<DialogTemplate<GroupFormValues, string>
advancedFields={() => []}
basicFields={basicFields}
dialog={dialog}
dialogTitle={"Add new group"}
initialValues={initialValues()}
submitButtonText={"Add group"}
onSubmitSuccess={onSubmitSuccess}
taskOnSubmit={taskOnSubmit}
validateFunc={validateFunc}
maxWidth="xs"
/>
);
});
const dialog = dialogs.group;
return (
<DialogTemplate<GroupFormValues, string>
advancedFields={() => []}
basicFields={basicFields}
dialog={dialog}
dialogTitle={"Add new group"}
initialValues={initialValues()}
submitButtonText={"Add group"}
onSubmitSuccess={onSubmitSuccess}
taskOnSubmit={taskOnSubmit}
validateFunc={validateFunc}
maxWidth="xs"
/>
);
});
};
36 changes: 18 additions & 18 deletions src/components/addTopicDialog.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,25 @@ import { useStore } from "../store/storeProvider";
import { TopicDialog } from "./topicDialog";

export const DEFAULT_TOPIC_VALUES: TopicFormikValues = {
advancedValues: {
acknowledgement: "LEADER",
trackingEnabled: false,
maxMessageSize: 10240,
retentionTime: 1,
},
topic: "",
schema: "",
group: "",
description: "",
advancedValues: {
acknowledgement: "LEADER",
trackingEnabled: false,
maxMessageSize: 10240,
retentionTime: 1,
},
topic: "",
schema: "",
group: "",
description: "",
};

export const AddTopicDialog = observer(() => {
const { groups, dialogs } = useStore();
const dialog = dialogs.topic;
const initialValues = (): TopicFormikValues => ({
...DEFAULT_TOPIC_VALUES,
group: dialog.params?.group || groups.defaultGroup,
...getTopicData(),
});
return <TopicDialog initialValues={initialValues} dialog={dialog} />;
const { groups, dialogs } = useStore();
const dialog = dialogs.topic;
const initialValues = (): TopicFormikValues => ({
...DEFAULT_TOPIC_VALUES,
group: dialog.params?.group || groups.defaultGroup,
...getTopicData(),
});
return <TopicDialog initialValues={initialValues} dialog={dialog} />;
});
14 changes: 7 additions & 7 deletions src/components/backendValidation.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,11 @@ import { Slide } from "@mui/material";
import React from "react";

export function BackendValidation({ text }: { text?: string }): JSX.Element {
return (
<Slide in={!!text} unmountOnExit mountOnEnter direction="up">
<Alert variant="outlined" severity="error">
{text}
</Alert>
</Slide>
);
return (
<Slide in={!!text} unmountOnExit mountOnEnter direction="up">
<Alert variant="outlined" severity="error">
{text}
</Alert>
</Slide>
);
}
Loading

0 comments on commit bcec68e

Please sign in to comment.