From 44ccdd87de0cf703703606803083709dc89995be Mon Sep 17 00:00:00 2001 From: Aarni Halinen Date: Fri, 14 Jan 2022 00:46:41 +0200 Subject: [PATCH] arrow functions --- src/api/auth.ts | 8 ++-- src/api/backend.ts | 30 ++++++------ src/api/eventApi.ts | 22 ++++----- src/api/feedApi.ts | 20 ++++---- src/api/jobAdApi.ts | 22 ++++----- src/api/signupApi.ts | 48 +++++++++---------- src/api/tagApi.ts | 4 +- .../Widgets/Checkbox/Checkboxes.tsx | 8 ++-- src/hooks/useFetchBackend.ts | 8 ++-- src/pages/_document.tsx | 4 +- src/pages/admin/login.tsx | 6 +-- src/utils/auth.ts | 12 ++--- src/views/common/AdminPageWrapper.tsx | 6 +-- 13 files changed, 97 insertions(+), 101 deletions(-) diff --git a/src/api/auth.ts b/src/api/auth.ts index cdc3df8..16fa3a5 100644 --- a/src/api/auth.ts +++ b/src/api/auth.ts @@ -1,12 +1,12 @@ import { deleteTokenCookie, getTokenCookie } from "@utils/auth"; import { APIPath, postBackendAPI } from "./backend"; -export async function generateToken(username: string, password: string): Promise { +export const generateToken = async (username: string, password: string): Promise => { const { token } = await postBackendAPI<{ username: string, password: string }, { token: string }>({ path: APIPath.AUTH_TOKEN }, { username, password }); return token; -} +}; -export async function isAuthenticated(): Promise { +export const authenticate = async (): Promise => { try { const token = getTokenCookie(); await postBackendAPI({ path: APIPath.AUTH_TOKEN_VERIFY }, { token }); @@ -16,4 +16,4 @@ export async function isAuthenticated(): Promise { deleteTokenCookie(); return false; } -} +}; diff --git a/src/api/backend.ts b/src/api/backend.ts index c138cac..cf6c927 100644 --- a/src/api/backend.ts +++ b/src/api/backend.ts @@ -71,14 +71,14 @@ const fillUrlParams = (apiPath: APIPath, params: API["urlParams"] = {}): string return `/${path}`; }; -async function callBackendAPI( +const callBackendAPI = async ( path: APIPath, urlParams: API["urlParams"] = {}, queryParams: API["queryParams"] = {}, method: AxiosRequestConfig["method"], headers: Headers, requestBody: RequestType, -): Promise { +): Promise => { const url = fillUrlParams(path, urlParams); const request: AxiosRequestConfig = { url, @@ -95,32 +95,32 @@ async function callBackendAPI( return arrayResp.results; } return response.data; -} +}; -export async function getBackendAPI({ +export const getBackendAPI = async ({ path, urlParams, queryParams, authenticated, -}: API): Promise { +}: API): Promise => { const headers = getHeaders(authenticated); return callBackendAPI(path, urlParams, queryParams, "GET", headers, undefined); -} +}; -export async function postBackendAPI({ +export const postBackendAPI = async ({ path, urlParams, queryParams, authenticated, -}: API, body: RequestType): Promise { +}: API, body: RequestType): Promise => { const headers = getHeaders(authenticated); return callBackendAPI(path, urlParams, queryParams, "POST", headers, body); -} +}; -export async function putBackendAPI({ +export const putBackendAPI = async ({ path, urlParams, queryParams, authenticated, -}: API, body: RequestType): Promise { +}: API, body: RequestType): Promise => { const headers = getHeaders(authenticated); return callBackendAPI(path, urlParams, queryParams, "PUT", headers, body); -} +}; -export async function deleteBackendAPI({ +export const deleteBackendAPI = async ({ path, urlParams, queryParams, authenticated, -}: API): Promise { +}: API): Promise => { const headers = getHeaders(authenticated); return callBackendAPI(path, urlParams, queryParams, "DELETE", headers, undefined); -} +}; diff --git a/src/api/eventApi.ts b/src/api/eventApi.ts index c5038bf..13c978f 100644 --- a/src/api/eventApi.ts +++ b/src/api/eventApi.ts @@ -12,7 +12,7 @@ interface Options { } class EventApi { - static async getEvent(id: number, auth = false): Promise { + static getEvent = async (id: number, auth = false): Promise => { try { return await getBackendAPI({ path: APIPath.EVENTS, urlParams: { id }, authenticated: auth, @@ -21,11 +21,11 @@ class EventApi { console.error(err); throw err; } - } + }; - static async getEvents({ + static getEvents = async ({ since, limit, offset, auth, - }: Options = {}): Promise { + }: Options = {}): Promise => { try { return await getBackendAPI({ path: APIPath.EVENTS, @@ -40,9 +40,9 @@ class EventApi { console.error(err); throw err; } - } + }; - static async createEvent(data: Event): Promise { + static createEvent = async (data: Event): Promise => { try { return await postBackendAPI({ path: APIPath.EVENTS, authenticated: true, @@ -51,9 +51,9 @@ class EventApi { console.error(err); throw err; } - } + }; - static async updateEvent(data: Event): Promise { + static updateEvent = async (data: Event): Promise => { try { return await putBackendAPI({ path: APIPath.EVENTS, urlParams: { id: data.id }, authenticated: true, @@ -62,16 +62,16 @@ class EventApi { console.error(err); throw err; } - } + }; - static async deleteEvent(id: number): Promise { + static deleteEvent = async (id: number): Promise => { try { await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.EVENTS, urlParams: { id }, authenticated: true }); } catch (err) { console.error(err); throw err; } - } + }; } export default EventApi; diff --git a/src/api/feedApi.ts b/src/api/feedApi.ts index af4c323..e198b47 100644 --- a/src/api/feedApi.ts +++ b/src/api/feedApi.ts @@ -11,7 +11,7 @@ interface Options { } class FeedApi { - static async getPost(id: number, auth?: boolean): Promise { + static getPost = async (id: number, auth?: boolean): Promise => { try { return await getBackendAPI({ path: APIPath.FEED, urlParams: { id }, authenticated: auth, @@ -20,9 +20,9 @@ class FeedApi { console.error(err); throw err; } - } + }; - static async getFeed({ limit, offset, auth }: Options = {}): Promise { + static getFeed = async ({ limit, offset, auth }: Options = {}): Promise => { try { return await getBackendAPI({ path: APIPath.FEED, @@ -36,18 +36,18 @@ class FeedApi { console.error(err); throw err; } - } + }; - static async createPost(data: Post): Promise { + static createPost = async (data: Post): Promise => { try { return await postBackendAPI({ path: APIPath.FEED, authenticated: true }, data); } catch (err) { console.error(err); throw err; } - } + }; - static async updatePost(data: Post): Promise { + static updatePost = async (data: Post): Promise => { try { return await putBackendAPI({ path: APIPath.FEED, urlParams: { id: data.id }, authenticated: true, @@ -56,16 +56,16 @@ class FeedApi { console.error(err); throw err; } - } + }; - static async deletePost(id: number): Promise { + static deletePost = async (id: number): Promise => { try { await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.EVENTS, urlParams: { id }, authenticated: true }); } catch (err) { console.error(err); throw err; } - } + }; } export default FeedApi; diff --git a/src/api/jobAdApi.ts b/src/api/jobAdApi.ts index 9ee70ff..4b74db5 100644 --- a/src/api/jobAdApi.ts +++ b/src/api/jobAdApi.ts @@ -12,7 +12,7 @@ interface Options { } class JobAdApi { - static async getJobAd(id: number, auth = false): Promise { + static getJobAd = async (id: number, auth = false): Promise => { try { return await getBackendAPI({ path: APIPath.JOBADS, urlParams: { id }, authenticated: auth, @@ -21,11 +21,11 @@ class JobAdApi { console.error(err); throw err; } - } + }; - static async getJobAds({ + static getJobAds = async ({ since, limit, offset, auth, - }: Options = {}): Promise { + }: Options = {}): Promise => { try { return await getBackendAPI({ path: APIPath.JOBADS, @@ -40,9 +40,9 @@ class JobAdApi { console.error(err); throw err; } - } + }; - static async createJobAd(data: JobAd): Promise { + static createJobAd = async (data: JobAd): Promise => { try { return await postBackendAPI({ path: APIPath.JOBADS, authenticated: true, @@ -51,9 +51,9 @@ class JobAdApi { console.error(err); throw err; } - } + }; - static async updateJobAd(data: JobAd): Promise { + static updateJobAd = async (data: JobAd): Promise => { try { return await putBackendAPI({ path: APIPath.JOBADS, urlParams: { id: data.id }, authenticated: true, @@ -62,16 +62,16 @@ class JobAdApi { console.error(err); throw err; } - } + }; - static async deleteJobAd(id: number): Promise { + static deleteJobAd = async (id: number): Promise => { try { await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.JOBADS, urlParams: { id }, authenticated: true }); } catch (err) { console.error(err); throw err; } - } + }; } export default JobAdApi; diff --git a/src/api/signupApi.ts b/src/api/signupApi.ts index c726b45..908a3b9 100644 --- a/src/api/signupApi.ts +++ b/src/api/signupApi.ts @@ -11,7 +11,7 @@ export type EmailRequest = { }; class SignupApi { - static async getSignup(id: number): Promise { + static getSignup = async (id: number): Promise => { try { return await getBackendAPI({ path: APIPath.SIGNUPS, urlParams: { id }, authenticated: true, @@ -20,9 +20,9 @@ class SignupApi { console.error(err); throw err; } - } + }; - static async createSignup(data: Signup): Promise { + static createSignup = async (data: Signup): Promise => { try { return await postBackendAPI({ path: APIPath.SIGNUPS, @@ -31,9 +31,9 @@ class SignupApi { console.error(err); throw err; } - } + }; - static async updateSignup(data: Signup, uuid: string): Promise { + static updateSignup = async (data: Signup, uuid: string): Promise => { try { const { id } = data; if (!id) throw new Error("SignupId required!"); @@ -48,9 +48,9 @@ class SignupApi { console.error(err); throw err; } - } + }; - static async getSignupUUID(id: number, uuid: string): Promise { + static getSignupUUID = async (id: number, uuid: string): Promise => { try { return await getBackendAPI({ path: APIPath.SIGNUPS_EDIT, @@ -63,18 +63,18 @@ class SignupApi { console.error(err); throw err; } - } + }; - static async deleteSignup(id: number): Promise { + static deleteSignup = async (id: number): Promise => { try { await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.SIGNUPS, urlParams: { id }, authenticated: true }); } catch (err) { console.error(err); throw err; } - } + }; - static async getForm(id: number, auth = false): Promise { + static getForm = async (id: number, auth = false): Promise => { try { return await getBackendAPI({ path: APIPath.SIGNUP_FORMS, urlParams: { id }, authenticated: auth, @@ -83,9 +83,9 @@ class SignupApi { console.error(err); throw err; } - } + }; - static async getForms(auth = false): Promise { + static getForms = async (auth = false): Promise => { try { return await getBackendAPI({ path: APIPath.SIGNUP_FORMS, authenticated: auth, @@ -94,9 +94,9 @@ class SignupApi { console.error(err); throw err; } - } + }; - static async createForm(data: SignupForm): Promise { + static createForm = async (data: SignupForm): Promise => { try { return await postBackendAPI({ path: APIPath.SIGNUP_FORMS, authenticated: true, @@ -105,9 +105,9 @@ class SignupApi { console.error(err); throw err; } - } + }; - static async updateForm(data: SignupForm): Promise { + static updateForm = async (data: SignupForm): Promise => { try { return await putBackendAPI({ path: APIPath.SIGNUP_FORMS, urlParams: { id: data.id }, authenticated: true, @@ -116,34 +116,34 @@ class SignupApi { console.error(err); throw err; } - } + }; - static async deleteForm(id: number): Promise { + static deleteForm = async (id: number): Promise => { try { await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.SIGNUP_FORMS, urlParams: { id }, authenticated: true }); } catch (err) { console.error(err); throw err; } - } + }; - static async signupFormSendEmail(data: EmailRequest, id: number): Promise { + static signupFormSendEmail = async (data: EmailRequest, id: number): Promise => { try { await postBackendAPI({ path: APIPath.SIGNUP_FORMS_EMAIL, urlParams: { id }, authenticated: true }, data); } catch (err) { console.error(err); throw err; } - } + }; - static async getSignups(id: number): Promise { + static getSignups = async (id: number): Promise => { try { return await getBackendAPI({ path: APIPath.SIGNUP_FORMS_SIGNUPS, urlParams: { id }, authenticated: true }); } catch (err) { console.error(err); throw err; } - } + }; } export default SignupApi; diff --git a/src/api/tagApi.ts b/src/api/tagApi.ts index 48c2b76..1ac4c6b 100644 --- a/src/api/tagApi.ts +++ b/src/api/tagApi.ts @@ -3,14 +3,14 @@ import Tag from "@models/Tag"; import { APIPath, getBackendAPI } from "./backend"; class TagApi { - static async getTags(): Promise { + static getTags = async (): Promise => { try { return await getBackendAPI({ path: APIPath.TAGS }); } catch (err) { console.error(err); throw err; } - } + }; } export default TagApi; diff --git a/src/components/Widgets/Checkbox/Checkboxes.tsx b/src/components/Widgets/Checkbox/Checkboxes.tsx index f54e6bc..539a9f3 100644 --- a/src/components/Widgets/Checkbox/Checkboxes.tsx +++ b/src/components/Widgets/Checkbox/Checkboxes.tsx @@ -5,17 +5,15 @@ import Checkbox from "./Checkbox"; // See https://github.com/rjsf-team/react-jsonschema-form/blob/master/packages/core/src/components/widgets/CheckboxesWidget.js -function selectValue(value, selected, all) { +const selectValue = (value, selected, all) => { const at = all.indexOf(value); const updated = selected.slice(0, at).concat(value, selected.slice(at)); // As inserting values at predefined index positions doesn't work with empty // arrays, we need to reorder the updated selection to match the initial order return updated.sort((a, b) => all.indexOf(a) > all.indexOf(b)); -} +}; -function deselectValue(value, selected) { - return selected.filter((v) => v !== value); -} +const deselectValue = (value, selected) => selected.filter((v) => v !== value); type CheckboxesProps = Omit & { options: any; diff --git a/src/hooks/useFetchBackend.ts b/src/hooks/useFetchBackend.ts index e6c1308..86be5d7 100644 --- a/src/hooks/useFetchBackend.ts +++ b/src/hooks/useFetchBackend.ts @@ -1,7 +1,7 @@ import useSWR from "swr"; import { APIPath, getBackendAPI } from "@api/backend"; -function useFetchBackend({ +const useFetchBackend = ({ apiPath: path, fallbackData, options, @@ -14,14 +14,14 @@ function useFetchBackend({ } }): { data?: DataType, - error?: any - } { + error?: Error + } => { const fetcher = (limit: number, authenticated: boolean) => getBackendAPI({ path, queryParams: { limit }, authenticated }); const { data, error } = useSWR([options?.limit, options?.auth], fetcher, { fallbackData }); return { data, error, }; -} +}; export default useFetchBackend; diff --git a/src/pages/_document.tsx b/src/pages/_document.tsx index fac394e..42939ab 100644 --- a/src/pages/_document.tsx +++ b/src/pages/_document.tsx @@ -6,7 +6,7 @@ import { ServerStyleSheet } from "styled-components"; import Favicons from "@components/Favicons"; export default class MyDocument extends Document<{ styleTags: unknown }> { - static async getInitialProps(ctx: DocumentContext): Promise { + static getInitialProps = async (ctx: DocumentContext): Promise => { const sheet = new ServerStyleSheet(); const originalRenderPage = ctx.renderPage; try { @@ -26,7 +26,7 @@ export default class MyDocument extends Document<{ styleTags: unknown }> { } finally { sheet.seal(); } - } + }; render(): JSX.Element { const { styleTags } = this.props; diff --git a/src/pages/admin/login.tsx b/src/pages/admin/login.tsx index 31e6967..6c97845 100644 --- a/src/pages/admin/login.tsx +++ b/src/pages/admin/login.tsx @@ -3,7 +3,7 @@ import { NextPage } from "next"; import { useRouter } from "next/router"; import styled from "styled-components"; import { setTokenCookie } from "@utils/auth"; -import { generateToken, isAuthenticated } from "@api/auth"; +import { generateToken, authenticate } from "@api/auth"; import AdminPageWrapper from "@views/common/AdminPageWrapper"; const Main = styled.div` @@ -21,8 +21,8 @@ const AdminLoginPage: NextPage = () => { const next = router.query.next as string || DEFAULT_REDIRECT; useEffect(() => { - isAuthenticated().then((res) => { - if (res) { + authenticate().then((authResult) => { + if (authResult) { router.push(next); } }); diff --git a/src/utils/auth.ts b/src/utils/auth.ts index 2790a6d..7c22859 100644 --- a/src/utils/auth.ts +++ b/src/utils/auth.ts @@ -1,15 +1,13 @@ import Cookies from "js-cookie"; -export function setTokenCookie(token: string): void { +export const setTokenCookie = (token: string): void => { Cookies.set("jwt", token); Cookies.set("jwt", token, { domain: ".sahkoinsinoorikilta.fi" }); -} +}; -export function getTokenCookie(): string { - return Cookies.get("jwt"); -} +export const getTokenCookie = (): string => Cookies.get("jwt"); -export function deleteTokenCookie(): void { +export const deleteTokenCookie = (): void => { Cookies.remove("jwt", { domain: ".sahkoinsinoorikilta.fi" }); Cookies.remove("jwt"); -} +}; diff --git a/src/views/common/AdminPageWrapper.tsx b/src/views/common/AdminPageWrapper.tsx index 5fa156c..26c6eae 100644 --- a/src/views/common/AdminPageWrapper.tsx +++ b/src/views/common/AdminPageWrapper.tsx @@ -5,7 +5,7 @@ import colors from "@theme/colors"; import breakpoints from "@theme/breakpoints"; import AdminHeader from "@components/AdminHeader"; import AdminSidebar from "@components/AdminSidebar"; -import { isAuthenticated } from "@api/auth"; +import { authenticate } from "@api/auth"; import LoadingView from "./LoadingView"; const Main = styled.main` @@ -43,8 +43,8 @@ const useShouldRedirect = (enabled = true) => { useEffect(() => { if (enabled) { - isAuthenticated().then((result) => { - setRedirect(!result); + authenticate().then((authResult) => { + setRedirect(!authResult); setCompleted(true); }); }