arrow functions

This commit is contained in:
Aarni Halinen
2022-01-14 00:46:41 +02:00
parent 8fb4dd9000
commit 44ccdd87de
13 changed files with 97 additions and 101 deletions
+4 -4
View File
@@ -1,12 +1,12 @@
import { deleteTokenCookie, getTokenCookie } from "@utils/auth";
import { APIPath, postBackendAPI } from "./backend";
export async function generateToken(username: string, password: string): Promise<string> {
export const generateToken = async (username: string, password: string): Promise<string> => {
const { token } = await postBackendAPI<{ username: string, password: string }, { token: string }>({ path: APIPath.AUTH_TOKEN }, { username, password });
return token;
}
};
export async function isAuthenticated(): Promise<boolean> {
export const authenticate = async (): Promise<boolean> => {
try {
const token = getTokenCookie();
await postBackendAPI({ path: APIPath.AUTH_TOKEN_VERIFY }, { token });
@@ -16,4 +16,4 @@ export async function isAuthenticated(): Promise<boolean> {
deleteTokenCookie();
return false;
}
}
};
+15 -15
View File
@@ -71,14 +71,14 @@ const fillUrlParams = (apiPath: APIPath, params: API["urlParams"] = {}): string
return `/${path}`;
};
async function callBackendAPI<RequestType, ResponseType>(
const callBackendAPI = async <RequestType, ResponseType>(
path: APIPath,
urlParams: API["urlParams"] = {},
queryParams: API["queryParams"] = {},
method: AxiosRequestConfig["method"],
headers: Headers,
requestBody: RequestType,
): Promise<ResponseType> {
): Promise<ResponseType> => {
const url = fillUrlParams(path, urlParams);
const request: AxiosRequestConfig = {
url,
@@ -95,32 +95,32 @@ async function callBackendAPI<RequestType, ResponseType>(
return arrayResp.results;
}
return response.data;
}
};
export async function getBackendAPI<ResponseType>({
export const getBackendAPI = async <ResponseType>({
path, urlParams, queryParams, authenticated,
}: API): Promise<ResponseType> {
}: API): Promise<ResponseType> => {
const headers = getHeaders(authenticated);
return callBackendAPI<undefined, ResponseType>(path, urlParams, queryParams, "GET", headers, undefined);
}
};
export async function postBackendAPI<RequestType, ResponseType>({
export const postBackendAPI = async <RequestType, ResponseType>({
path, urlParams, queryParams, authenticated,
}: API, body: RequestType): Promise<ResponseType> {
}: API, body: RequestType): Promise<ResponseType> => {
const headers = getHeaders(authenticated);
return callBackendAPI<RequestType, ResponseType>(path, urlParams, queryParams, "POST", headers, body);
}
};
export async function putBackendAPI<RequestType, ResponseType>({
export const putBackendAPI = async <RequestType, ResponseType>({
path, urlParams, queryParams, authenticated,
}: API, body: RequestType): Promise<ResponseType> {
}: API, body: RequestType): Promise<ResponseType> => {
const headers = getHeaders(authenticated);
return callBackendAPI<RequestType, ResponseType>(path, urlParams, queryParams, "PUT", headers, body);
}
};
export async function deleteBackendAPI<ResponseType>({
export const deleteBackendAPI = async <ResponseType>({
path, urlParams, queryParams, authenticated,
}: API): Promise<ResponseType> {
}: API): Promise<ResponseType> => {
const headers = getHeaders(authenticated);
return callBackendAPI<undefined, ResponseType>(path, urlParams, queryParams, "DELETE", headers, undefined);
}
};
+11 -11
View File
@@ -12,7 +12,7 @@ interface Options {
}
class EventApi {
static async getEvent(id: number, auth = false): Promise<Event> {
static getEvent = async (id: number, auth = false): Promise<Event> => {
try {
return await getBackendAPI<Event>({
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<Event[]> {
}: Options = {}): Promise<Event[]> => {
try {
return await getBackendAPI<Event[]>({
path: APIPath.EVENTS,
@@ -40,9 +40,9 @@ class EventApi {
console.error(err);
throw err;
}
}
};
static async createEvent(data: Event): Promise<Event> {
static createEvent = async (data: Event): Promise<Event> => {
try {
return await postBackendAPI<Event, Event>({
path: APIPath.EVENTS, authenticated: true,
@@ -51,9 +51,9 @@ class EventApi {
console.error(err);
throw err;
}
}
};
static async updateEvent(data: Event): Promise<Event> {
static updateEvent = async (data: Event): Promise<Event> => {
try {
return await putBackendAPI<Event, Event>({
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<void> {
static deleteEvent = async (id: number): Promise<void> => {
try {
await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.EVENTS, urlParams: { id }, authenticated: true });
} catch (err) {
console.error(err);
throw err;
}
}
};
}
export default EventApi;
+10 -10
View File
@@ -11,7 +11,7 @@ interface Options {
}
class FeedApi {
static async getPost(id: number, auth?: boolean): Promise<Post> {
static getPost = async (id: number, auth?: boolean): Promise<Post> => {
try {
return await getBackendAPI<Post>({
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<Post[]> {
static getFeed = async ({ limit, offset, auth }: Options = {}): Promise<Post[]> => {
try {
return await getBackendAPI<Post[]>({
path: APIPath.FEED,
@@ -36,18 +36,18 @@ class FeedApi {
console.error(err);
throw err;
}
}
};
static async createPost(data: Post): Promise<Post> {
static createPost = async (data: Post): Promise<Post> => {
try {
return await postBackendAPI<Post, Post>({ path: APIPath.FEED, authenticated: true }, data);
} catch (err) {
console.error(err);
throw err;
}
}
};
static async updatePost(data: Post): Promise<Post> {
static updatePost = async (data: Post): Promise<Post> => {
try {
return await putBackendAPI<Post, Post>({
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<void> {
static deletePost = async (id: number): Promise<void> => {
try {
await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.EVENTS, urlParams: { id }, authenticated: true });
} catch (err) {
console.error(err);
throw err;
}
}
};
}
export default FeedApi;
+11 -11
View File
@@ -12,7 +12,7 @@ interface Options {
}
class JobAdApi {
static async getJobAd(id: number, auth = false): Promise<JobAd> {
static getJobAd = async (id: number, auth = false): Promise<JobAd> => {
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<JobAd[]> {
}: Options = {}): Promise<JobAd[]> => {
try {
return await getBackendAPI<JobAd[]>({
path: APIPath.JOBADS,
@@ -40,9 +40,9 @@ class JobAdApi {
console.error(err);
throw err;
}
}
};
static async createJobAd(data: JobAd): Promise<JobAd> {
static createJobAd = async (data: JobAd): Promise<JobAd> => {
try {
return await postBackendAPI<JobAd, JobAd>({
path: APIPath.JOBADS, authenticated: true,
@@ -51,9 +51,9 @@ class JobAdApi {
console.error(err);
throw err;
}
}
};
static async updateJobAd(data: JobAd): Promise<JobAd> {
static updateJobAd = async (data: JobAd): Promise<JobAd> => {
try {
return await putBackendAPI<JobAd, JobAd>({
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<void> {
static deleteJobAd = async (id: number): Promise<void> => {
try {
await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.JOBADS, urlParams: { id }, authenticated: true });
} catch (err) {
console.error(err);
throw err;
}
}
};
}
export default JobAdApi;
+24 -24
View File
@@ -11,7 +11,7 @@ export type EmailRequest = {
};
class SignupApi {
static async getSignup(id: number): Promise<Signup> {
static getSignup = async (id: number): Promise<Signup> => {
try {
return await getBackendAPI<Signup>({
path: APIPath.SIGNUPS, urlParams: { id }, authenticated: true,
@@ -20,9 +20,9 @@ class SignupApi {
console.error(err);
throw err;
}
}
};
static async createSignup(data: Signup): Promise<Signup> {
static createSignup = async (data: Signup): Promise<Signup> => {
try {
return await postBackendAPI<Signup, Signup>({
path: APIPath.SIGNUPS,
@@ -31,9 +31,9 @@ class SignupApi {
console.error(err);
throw err;
}
}
};
static async updateSignup(data: Signup, uuid: string): Promise<Signup> {
static updateSignup = async (data: Signup, uuid: string): Promise<Signup> => {
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<Signup> {
static getSignupUUID = async (id: number, uuid: string): Promise<Signup> => {
try {
return await getBackendAPI<Signup>({
path: APIPath.SIGNUPS_EDIT,
@@ -63,18 +63,18 @@ class SignupApi {
console.error(err);
throw err;
}
}
};
static async deleteSignup(id: number): Promise<void> {
static deleteSignup = async (id: number): Promise<void> => {
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<SignupForm> {
static getForm = async (id: number, auth = false): Promise<SignupForm> => {
try {
return await getBackendAPI<SignupForm>({
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<SignupForm[]> {
static getForms = async (auth = false): Promise<SignupForm[]> => {
try {
return await getBackendAPI<SignupForm[]>({
path: APIPath.SIGNUP_FORMS, authenticated: auth,
@@ -94,9 +94,9 @@ class SignupApi {
console.error(err);
throw err;
}
}
};
static async createForm(data: SignupForm): Promise<SignupForm> {
static createForm = async (data: SignupForm): Promise<SignupForm> => {
try {
return await postBackendAPI<SignupForm, SignupForm>({
path: APIPath.SIGNUP_FORMS, authenticated: true,
@@ -105,9 +105,9 @@ class SignupApi {
console.error(err);
throw err;
}
}
};
static async updateForm(data: SignupForm): Promise<SignupForm> {
static updateForm = async (data: SignupForm): Promise<SignupForm> => {
try {
return await putBackendAPI<SignupForm, SignupForm>({
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<void> {
static deleteForm = async (id: number): Promise<void> => {
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<void> {
static signupFormSendEmail = async (data: EmailRequest, id: number): Promise<void> => {
try {
await postBackendAPI<EmailRequest, { message: "Email sent" }>({ path: APIPath.SIGNUP_FORMS_EMAIL, urlParams: { id }, authenticated: true }, data);
} catch (err) {
console.error(err);
throw err;
}
}
};
static async getSignups(id: number): Promise<Signup[]> {
static getSignups = async (id: number): Promise<Signup[]> => {
try {
return await getBackendAPI<Signup[]>({ path: APIPath.SIGNUP_FORMS_SIGNUPS, urlParams: { id }, authenticated: true });
} catch (err) {
console.error(err);
throw err;
}
}
};
}
export default SignupApi;
+2 -2
View File
@@ -3,14 +3,14 @@ import Tag from "@models/Tag";
import { APIPath, getBackendAPI } from "./backend";
class TagApi {
static async getTags(): Promise<Tag[]> {
static getTags = async (): Promise<Tag[]> => {
try {
return await getBackendAPI<Tag[]>({ path: APIPath.TAGS });
} catch (err) {
console.error(err);
throw err;
}
}
};
}
export default TagApi;
@@ -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<WidgetProps, "options"> & {
options: any;
+4 -4
View File
@@ -1,7 +1,7 @@
import useSWR from "swr";
import { APIPath, getBackendAPI } from "@api/backend";
function useFetchBackend<DataType>({
const useFetchBackend = <DataType>({
apiPath: path,
fallbackData,
options,
@@ -14,14 +14,14 @@ function useFetchBackend<DataType>({
}
}): {
data?: DataType,
error?: any
} {
error?: Error
} => {
const fetcher = (limit: number, authenticated: boolean) => getBackendAPI<DataType>({ path, queryParams: { limit }, authenticated });
const { data, error } = useSWR([options?.limit, options?.auth], fetcher, { fallbackData });
return {
data,
error,
};
}
};
export default useFetchBackend;
+2 -2
View File
@@ -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<DocumentInitialProps> {
static getInitialProps = async (ctx: DocumentContext): Promise<DocumentInitialProps> => {
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;
+3 -3
View File
@@ -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);
}
});
+5 -7
View File
@@ -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");
}
};
+3 -3
View File
@@ -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);
});
}