Skip to content

Commit

Permalink
Merge pull request #209 from Arnei/add-typing-to-utils
Browse files Browse the repository at this point in the history
Typing /app/src/utils/utils.ts
  • Loading branch information
Arnei authored Mar 5, 2024
2 parents 46c95ec + 65a626c commit 3d9d88f
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 30 deletions.
6 changes: 1 addition & 5 deletions app/src/components/events/partials/modals/SeriesDetails.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ const SeriesDetails = ({
name: "statistics",
hidden: !hasStatistics,
},
{
tabNameTranslation: "Feeds",
name: "feeds",
},
];

// @ts-expect-error TS(7006): Parameter 'tabNr' implicitly has an 'any' type.
Expand Down Expand Up @@ -133,7 +129,7 @@ const SeriesDetails = ({
)}
{feeds.length > 0 && (
<button className={"button-like-anchor " + cn({ active: page === 5 })} onClick={() => openTab(5)}>
{t(tabs[5].tabNameTranslation)}
{"Feeds"}
</button>
)}
</nav>
Expand Down
2 changes: 1 addition & 1 deletion app/src/slices/userInfoSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ type UserInfoUser = {
username: string,
}

type UserInfoState = {
export type UserInfoState = {
status: 'uninitialized' | 'loading' | 'succeeded' | 'failed',
error: SerializedError | null,
statusOcVersion: 'uninitialized' | 'loading' | 'succeeded' | 'failed',
Expand Down
35 changes: 11 additions & 24 deletions app/src/utils/utils.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import languages from "../i18n/languages";
import i18n from "../i18n/i18n";
import { UserInfoState } from "../slices/userInfoSlice";

/**
* This File contains methods that are needed in more than one places
Expand All @@ -9,15 +10,10 @@ export const getTimezoneOffset = () => {
let d = new Date();
let offset = d.getTimezoneOffset() * -1;

if (offset >= 0) {
return "+" + offset / 60;
}

return offset / 60;
};

// @ts-expect-error TS(7006): Parameter 'offset' implicitly has an 'any' type.
export const getTimezoneString = (offset) => {
export const getTimezoneString = (offset: number) => {
return "UTC" + (offset < 0 ? "-" : "+") + offset;
};

Expand All @@ -32,8 +28,7 @@ export const getCurrentLanguageInformation = () => {
};

// fills an array from 00 to number of elements specified
// @ts-expect-error TS(7006): Parameter 'numberOfElements' implicitly has an 'an... Remove this comment to see the full error message
export const initArray = (numberOfElements) => {
export const initArray = (numberOfElements: number) => {
let i,
result = [];
for (i = 0; i < numberOfElements; i++) {
Expand All @@ -46,8 +41,7 @@ export const initArray = (numberOfElements) => {
};

// insert leading 0 for numbers smaller 10
// @ts-expect-error TS(7006): Parameter 'number' implicitly has an 'any' type.
export const makeTwoDigits = (number) => {
export const makeTwoDigits = (number: number) => {
if (number < 10) {
return "0" + number;
} else {
Expand All @@ -59,8 +53,7 @@ export const makeTwoDigits = (number) => {
* transforms an object of form { id1: value1, id2: value2 }
* to [{id: id1, value: value1},{id: id2, value: value2}]
*/
// @ts-expect-error TS(7006): Parameter 'data' implicitly has an 'any' type.
export const transformToIdValueArray = (data) => {
export const transformToIdValueArray = (data: {[key: string | number]: any}) => {
return Object.keys(data).map((key) => {
return {
id: key,
Expand All @@ -84,8 +77,7 @@ export const transformToIdValueArray = (data) => {
}
]
*/
// @ts-expect-error TS(7006): Parameter 'data' implicitly has an 'any' type.
export const transformToObjectArray = (data) => {
export const transformToObjectArray = (data: {[key: string | number]: object}) => {
return Object.keys(data).map((key) => {
return {
id: key,
Expand All @@ -98,12 +90,10 @@ export const transformToObjectArray = (data) => {
* iterates trough all attributes in an object and switches 'true'- and 'false'-Strings
* to their corresponding boolean value. All other values stay the same.
*/
// @ts-expect-error TS(7006): Parameter 'baseObject' implicitly has an 'any' typ... Remove this comment to see the full error message
export const parseBooleanInObject = (baseObject) => {
let parsedObject = {};
export const parseBooleanInObject = (baseObject: {[key: string]: any}) => {
let parsedObject: {[key: string]: any} = {};

Object.keys(baseObject).forEach((config) => {
// @ts-expect-error TS(7053): Element implicitly has an 'any' type because expre... Remove this comment to see the full error message
parsedObject[config] = parseValueForBooleanStrings(baseObject[config]);
});

Expand All @@ -114,8 +104,7 @@ export const parseBooleanInObject = (baseObject) => {
* switches 'true'- and 'false'-Strings
* to their corresponding boolean value. All other kinds of values stay the same.
*/
// @ts-expect-error TS(7006): Parameter 'value' implicitly has an 'any' type.
export const parseValueForBooleanStrings = (value) => {
export const parseValueForBooleanStrings = (value: any) => {
let parsedValue = value;
if (parsedValue === "true") {
parsedValue = true;
Expand All @@ -129,14 +118,12 @@ export const parseValueForBooleanStrings = (value) => {
/*
* checks if a user is admin or has the required role to access an ui element
*/
// @ts-expect-error TS(7006): Parameter 'role' implicitly has an 'any' type.
export const hasAccess = (role, userInfo) => {
export const hasAccess = (role: string, userInfo: UserInfoState) => {
return !!(userInfo.isAdmin || userInfo.roles.includes(role));
};

// checks, if a String is proper JSON
// @ts-expect-error TS(7006): Parameter 'text' implicitly has an 'any' type.
export const isJson = (text) => {
export const isJson = (text: string) => {
try {
const json = JSON.parse(text);
const type = Object.prototype.toString.call(json);
Expand Down

0 comments on commit 3d9d88f

Please sign in to comment.