refactor all api files
This commit is contained in:
@@ -0,0 +1,19 @@
|
|||||||
|
import { deleteTokenCookie, getTokenCookie } from "@utils/auth";
|
||||||
|
import { APIPath, postBackendAPI } from "./backend";
|
||||||
|
|
||||||
|
export async function generateToken(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> {
|
||||||
|
try {
|
||||||
|
const token = getTokenCookie();
|
||||||
|
await postBackendAPI({ path: APIPath.AUTH_TOKEN_VERIFY }, { token });
|
||||||
|
return true;
|
||||||
|
} catch (err) {
|
||||||
|
// remove the cookie since it's invalid
|
||||||
|
deleteTokenCookie();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -13,7 +13,10 @@ export enum APIPath {
|
|||||||
FEED = "/feed/:id",
|
FEED = "/feed/:id",
|
||||||
JOBADS = "/jobads/:id",
|
JOBADS = "/jobads/:id",
|
||||||
SIGNUPS = "/signup/:id",
|
SIGNUPS = "/signup/:id",
|
||||||
|
SIGNUPS_EDIT = "/signup/:id/edit/:uuid",
|
||||||
SIGNUP_FORMS = "/signupForm/:id",
|
SIGNUP_FORMS = "/signupForm/:id",
|
||||||
|
SIGNUP_FORMS_EMAIL = "/signupForm/:id/sendemail",
|
||||||
|
SIGNUP_FORMS_SIGNUPS = "/signupForm/:id/signups",
|
||||||
AUTH_TOKEN = "/api-token-auth",
|
AUTH_TOKEN = "/api-token-auth",
|
||||||
AUTH_TOKEN_VERIFY = "/api-token-verify",
|
AUTH_TOKEN_VERIFY = "/api-token-verify",
|
||||||
}
|
}
|
||||||
@@ -22,6 +25,7 @@ export type API = {
|
|||||||
path: APIPath;
|
path: APIPath;
|
||||||
urlParams?: {
|
urlParams?: {
|
||||||
id?: string | number;
|
id?: string | number;
|
||||||
|
uuid?: string;
|
||||||
};
|
};
|
||||||
queryParams?: {
|
queryParams?: {
|
||||||
limit?: number;
|
limit?: number;
|
||||||
|
|||||||
+2
-2
@@ -14,7 +14,7 @@ interface Options {
|
|||||||
class EventApi {
|
class EventApi {
|
||||||
static async getEvent(id: number, auth = false): Promise<Event> {
|
static async getEvent(id: number, auth = false): Promise<Event> {
|
||||||
try {
|
try {
|
||||||
return await getBackendAPI({
|
return await getBackendAPI<Event>({
|
||||||
path: APIPath.EVENTS, urlParams: { id }, authenticated: auth,
|
path: APIPath.EVENTS, urlParams: { id }, authenticated: auth,
|
||||||
});
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
@@ -66,7 +66,7 @@ class EventApi {
|
|||||||
|
|
||||||
static async deleteEvent(id: number): Promise<void> {
|
static async deleteEvent(id: number): Promise<void> {
|
||||||
try {
|
try {
|
||||||
await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.EVENTS, urlParams: { id } });
|
await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.EVENTS, urlParams: { id }, authenticated: true });
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
|
|||||||
+23
-41
@@ -1,41 +1,37 @@
|
|||||||
/* eslint-disable no-console */
|
/* eslint-disable no-console */
|
||||||
import axios from "axios";
|
|
||||||
import Post from "@models/Feed";
|
import Post from "@models/Feed";
|
||||||
import { getAuthHeader } from "@utils/auth";
|
import {
|
||||||
|
APIPath, deleteBackendAPI, getBackendAPI, postBackendAPI, putBackendAPI,
|
||||||
|
} from "./backend";
|
||||||
|
|
||||||
export const URL = `${process.env.NEXT_PUBLIC_API_URL}/feed/`;
|
interface Options {
|
||||||
|
|
||||||
export interface Options {
|
|
||||||
limit?: number;
|
limit?: number;
|
||||||
offset?: number;
|
offset?: number;
|
||||||
auth?: boolean;
|
auth?: boolean;
|
||||||
}
|
}
|
||||||
|
|
||||||
class FeedApi {
|
class FeedApi {
|
||||||
static async getFeed(options: Options = {}): Promise<Post[]> {
|
static async getPost(id: number, auth?: boolean): Promise<Post> {
|
||||||
const {
|
|
||||||
limit, offset, auth,
|
|
||||||
} = options;
|
|
||||||
const params = {
|
|
||||||
limit,
|
|
||||||
offset,
|
|
||||||
};
|
|
||||||
const headers = auth ? { Authorization: getAuthHeader() } : null;
|
|
||||||
try {
|
try {
|
||||||
const resp = await axios.get(URL, { params, headers });
|
return await getBackendAPI<Post>({
|
||||||
return resp.data.results;
|
path: APIPath.FEED, urlParams: { id }, authenticated: auth,
|
||||||
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getPost(id: number, options: Options = {}): Promise<Post> {
|
static async getFeed({ limit, offset, auth }: Options = {}): Promise<Post[]> {
|
||||||
const { auth } = options;
|
|
||||||
const headers = auth ? { Authorization: getAuthHeader() } : null;
|
|
||||||
try {
|
try {
|
||||||
const resp = await axios.get(`${URL}${id}/`, { headers });
|
return await getBackendAPI<Post[]>({
|
||||||
return resp.data;
|
path: APIPath.FEED,
|
||||||
|
queryParams: {
|
||||||
|
limit,
|
||||||
|
offset,
|
||||||
|
},
|
||||||
|
authenticated: auth,
|
||||||
|
});
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
@@ -44,12 +40,7 @@ class FeedApi {
|
|||||||
|
|
||||||
static async createPost(data: Post): Promise<Post> {
|
static async createPost(data: Post): Promise<Post> {
|
||||||
try {
|
try {
|
||||||
const resp = await axios.post(URL, data, {
|
return await postBackendAPI<Post, Post>({ path: APIPath.FEED, authenticated: true }, data);
|
||||||
headers: {
|
|
||||||
Authorization: getAuthHeader(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return resp.data;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
@@ -58,27 +49,18 @@ class FeedApi {
|
|||||||
|
|
||||||
static async updatePost(data: Post): Promise<Post> {
|
static async updatePost(data: Post): Promise<Post> {
|
||||||
try {
|
try {
|
||||||
const putUrl = `${URL}${data.id}/`;
|
return await putBackendAPI<Post, Post>({
|
||||||
const resp = await axios.put(putUrl, data, {
|
path: APIPath.FEED, urlParams: { id: data.id }, authenticated: true,
|
||||||
headers: {
|
}, data);
|
||||||
Authorization: getAuthHeader(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return resp.data;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async deletePost(id: number) {
|
static async deletePost(id: number): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const resp = await axios.delete(`${URL}${id}`, {
|
await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.EVENTS, urlParams: { id }, authenticated: true });
|
||||||
headers: {
|
|
||||||
Authorization: getAuthHeader(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return resp.data;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
|
|||||||
+26
-44
@@ -1,11 +1,10 @@
|
|||||||
/* eslint-disable no-console */
|
/* eslint-disable no-console */
|
||||||
import axios from "axios";
|
|
||||||
import JobAd from "@models/JobAd";
|
import JobAd from "@models/JobAd";
|
||||||
import { getAuthHeader } from "@utils/auth";
|
import {
|
||||||
|
APIPath, deleteBackendAPI, getBackendAPI, postBackendAPI, putBackendAPI,
|
||||||
|
} from "./backend";
|
||||||
|
|
||||||
export const URL = `${process.env.NEXT_PUBLIC_API_URL}/jobads/`;
|
interface Options {
|
||||||
|
|
||||||
export interface Options {
|
|
||||||
since?: Date;
|
since?: Date;
|
||||||
limit?: number;
|
limit?: number;
|
||||||
offset?: number;
|
offset?: number;
|
||||||
@@ -13,35 +12,30 @@ export interface Options {
|
|||||||
}
|
}
|
||||||
|
|
||||||
class JobAdApi {
|
class JobAdApi {
|
||||||
static async getJobAds(options: Options = {}): Promise<JobAd[]> {
|
static async getJobAd(id: number, auth = false): Promise<JobAd> {
|
||||||
const {
|
|
||||||
since, limit, offset, auth,
|
|
||||||
} = options;
|
|
||||||
try {
|
try {
|
||||||
const params = {
|
return await getBackendAPI({
|
||||||
since,
|
path: APIPath.JOBADS, urlParams: { id }, authenticated: auth,
|
||||||
limit,
|
|
||||||
offset,
|
|
||||||
};
|
|
||||||
const headers = auth ? { Authorization: getAuthHeader() } : null;
|
|
||||||
const resp = await axios.get(`${URL}`, {
|
|
||||||
headers,
|
|
||||||
params,
|
|
||||||
});
|
});
|
||||||
return resp.data.results;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async getJobAd(id: number, auth = false): Promise<JobAd> {
|
static async getJobAds({
|
||||||
|
since, limit, offset, auth,
|
||||||
|
}: Options = {}): Promise<JobAd[]> {
|
||||||
try {
|
try {
|
||||||
const headers = auth ? { Authorization: getAuthHeader() } : null;
|
return await getBackendAPI<JobAd[]>({
|
||||||
const resp = await axios.get(`${URL}${id}/`, {
|
path: APIPath.JOBADS,
|
||||||
headers,
|
queryParams: {
|
||||||
|
since,
|
||||||
|
limit,
|
||||||
|
offset,
|
||||||
|
},
|
||||||
|
authenticated: auth,
|
||||||
});
|
});
|
||||||
return resp.data;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
@@ -50,12 +44,9 @@ class JobAdApi {
|
|||||||
|
|
||||||
static async createJobAd(data: JobAd): Promise<JobAd> {
|
static async createJobAd(data: JobAd): Promise<JobAd> {
|
||||||
try {
|
try {
|
||||||
const resp = await axios.post(URL, data, {
|
return await postBackendAPI<JobAd, JobAd>({
|
||||||
headers: {
|
path: APIPath.JOBADS, authenticated: true,
|
||||||
Authorization: getAuthHeader(),
|
}, data);
|
||||||
},
|
|
||||||
});
|
|
||||||
return resp.data;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
@@ -64,27 +55,18 @@ class JobAdApi {
|
|||||||
|
|
||||||
static async updateJobAd(data: JobAd): Promise<JobAd> {
|
static async updateJobAd(data: JobAd): Promise<JobAd> {
|
||||||
try {
|
try {
|
||||||
const putUrl = `${URL}${data.id}/`;
|
return await putBackendAPI<JobAd, JobAd>({
|
||||||
const resp = await axios.put(putUrl, data, {
|
path: APIPath.JOBADS, urlParams: { id: data.id }, authenticated: true,
|
||||||
headers: {
|
}, data);
|
||||||
Authorization: getAuthHeader(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return resp.data;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async deleteJobAd(id: number) {
|
static async deleteJobAd(id: number): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const resp = await axios.delete(`${URL}${id}`, {
|
await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.JOBADS, urlParams: { id }, authenticated: true });
|
||||||
headers: {
|
|
||||||
Authorization: getAuthHeader(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return resp.data;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
|
|||||||
+50
-83
@@ -1,27 +1,21 @@
|
|||||||
/* eslint-disable no-console */
|
/* eslint-disable no-console */
|
||||||
import axios from "axios";
|
|
||||||
import { Signup, SignupForm } from "@models/Signup";
|
import { Signup, SignupForm } from "@models/Signup";
|
||||||
import { getAuthHeader } from "@utils/auth";
|
import {
|
||||||
|
APIPath, deleteBackendAPI, getBackendAPI, postBackendAPI, putBackendAPI,
|
||||||
|
} from "./backend";
|
||||||
|
|
||||||
export const URL = `${process.env.NEXT_PUBLIC_API_URL}/signup/`;
|
export type EmailRequest = {
|
||||||
export const FORM_URL = `${process.env.NEXT_PUBLIC_API_URL}/signupForm/`;
|
mode: "all" | "actual" | "reserve";
|
||||||
|
subject: string;
|
||||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
content: string;
|
||||||
export interface Options {
|
};
|
||||||
// limit?: number;
|
|
||||||
// offset?: number;
|
|
||||||
// auth?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
class SignupApi {
|
class SignupApi {
|
||||||
static async getSignup(id: number): Promise<Signup> {
|
static async getSignup(id: number): Promise<Signup> {
|
||||||
try {
|
try {
|
||||||
const resp = await axios.get(`${URL}${id}`, {
|
return await getBackendAPI<Signup>({
|
||||||
headers: {
|
path: APIPath.SIGNUPS, urlParams: { id }, authenticated: true,
|
||||||
Authorization: getAuthHeader(),
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
return resp.data;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
@@ -30,8 +24,9 @@ class SignupApi {
|
|||||||
|
|
||||||
static async createSignup(data: Signup): Promise<Signup> {
|
static async createSignup(data: Signup): Promise<Signup> {
|
||||||
try {
|
try {
|
||||||
const resp = await axios.post(URL, data);
|
return await postBackendAPI<Signup, Signup>({
|
||||||
return resp.data;
|
path: APIPath.SIGNUPS,
|
||||||
|
}, data);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
@@ -42,10 +37,13 @@ class SignupApi {
|
|||||||
try {
|
try {
|
||||||
const { id } = data;
|
const { id } = data;
|
||||||
if (!id) throw new Error("SignupId required!");
|
if (!id) throw new Error("SignupId required!");
|
||||||
const resp = await axios.put(`${URL}${id}/edit/`, data, {
|
return await putBackendAPI<Signup, Signup>({
|
||||||
params: { uuid },
|
path: APIPath.SIGNUPS_EDIT,
|
||||||
});
|
urlParams: {
|
||||||
return resp.data;
|
id,
|
||||||
|
uuid,
|
||||||
|
},
|
||||||
|
}, data);
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
@@ -54,40 +52,22 @@ class SignupApi {
|
|||||||
|
|
||||||
static async getSignupUUID(id: number, uuid: string): Promise<Signup> {
|
static async getSignupUUID(id: number, uuid: string): Promise<Signup> {
|
||||||
try {
|
try {
|
||||||
const resp = await axios.get(`${URL}${id}/edit/`, {
|
return await getBackendAPI<Signup>({
|
||||||
params: {
|
path: APIPath.SIGNUPS_EDIT,
|
||||||
|
urlParams: {
|
||||||
|
id,
|
||||||
uuid,
|
uuid,
|
||||||
},
|
},
|
||||||
});
|
});
|
||||||
return resp.data;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async deleteSignup(id: number) {
|
static async deleteSignup(id: number): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const resp = await axios.delete(`${URL}${id}`, {
|
await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.SIGNUPS, urlParams: { id }, authenticated: true });
|
||||||
headers: {
|
|
||||||
Authorization: getAuthHeader(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return resp.data;
|
|
||||||
} catch (err) {
|
|
||||||
console.error(err);
|
|
||||||
throw err;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
static async getForms(auth = false): Promise<SignupForm[]> {
|
|
||||||
try {
|
|
||||||
const headers = auth ? { Authorization: getAuthHeader() } : null;
|
|
||||||
const resp = await axios.get(FORM_URL, {
|
|
||||||
headers,
|
|
||||||
});
|
|
||||||
const { results } = resp.data;
|
|
||||||
return results;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
@@ -96,11 +76,20 @@ class SignupApi {
|
|||||||
|
|
||||||
static async getForm(id: number, auth = false): Promise<SignupForm> {
|
static async getForm(id: number, auth = false): Promise<SignupForm> {
|
||||||
try {
|
try {
|
||||||
const headers = auth ? { Authorization: getAuthHeader() } : null;
|
return await getBackendAPI<SignupForm>({
|
||||||
const resp = await axios.get(`${FORM_URL}${id}/`, {
|
path: APIPath.SIGNUP_FORMS, urlParams: { id }, authenticated: auth,
|
||||||
headers,
|
});
|
||||||
|
} catch (err) {
|
||||||
|
console.error(err);
|
||||||
|
throw err;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
static async getForms(auth = false): Promise<SignupForm[]> {
|
||||||
|
try {
|
||||||
|
return await getBackendAPI<SignupForm[]>({
|
||||||
|
path: APIPath.SIGNUP_FORMS, authenticated: auth,
|
||||||
});
|
});
|
||||||
return resp.data;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
@@ -109,12 +98,9 @@ class SignupApi {
|
|||||||
|
|
||||||
static async createForm(data: SignupForm): Promise<SignupForm> {
|
static async createForm(data: SignupForm): Promise<SignupForm> {
|
||||||
try {
|
try {
|
||||||
const resp = await axios.post(FORM_URL, data, {
|
return await postBackendAPI<SignupForm, SignupForm>({
|
||||||
headers: {
|
path: APIPath.SIGNUP_FORMS, authenticated: true,
|
||||||
Authorization: getAuthHeader(),
|
}, data);
|
||||||
},
|
|
||||||
});
|
|
||||||
return resp.data;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
@@ -123,41 +109,27 @@ class SignupApi {
|
|||||||
|
|
||||||
static async updateForm(data: SignupForm): Promise<SignupForm> {
|
static async updateForm(data: SignupForm): Promise<SignupForm> {
|
||||||
try {
|
try {
|
||||||
const putUrl = `${FORM_URL}${data.id}/`;
|
return await putBackendAPI<SignupForm, SignupForm>({
|
||||||
const resp = await axios.put(putUrl, data, {
|
path: APIPath.SIGNUP_FORMS, urlParams: { id: data.id }, authenticated: true,
|
||||||
headers: {
|
}, data);
|
||||||
Authorization: getAuthHeader(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return resp.data;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async deleteForm(id: number) {
|
static async deleteForm(id: number): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const resp = await axios.delete(`${FORM_URL}${id}`, {
|
await deleteBackendAPI<{ message: "OK" }>({ path: APIPath.SIGNUP_FORMS, urlParams: { id }, authenticated: true });
|
||||||
headers: {
|
|
||||||
Authorization: getAuthHeader(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return resp.data;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
static async signupFormSendEmail(data: any, id: number): Promise<any> {
|
static async signupFormSendEmail(data: EmailRequest, id: number): Promise<void> {
|
||||||
try {
|
try {
|
||||||
const resp = await axios.post(`${FORM_URL}${id}/sendemail/`, data, {
|
await postBackendAPI<EmailRequest, { message: "Email sent" }>({ path: APIPath.SIGNUP_FORMS_EMAIL, urlParams: { id }, authenticated: true }, data);
|
||||||
headers: {
|
|
||||||
Authorization: getAuthHeader(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return resp.data;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
@@ -166,12 +138,7 @@ class SignupApi {
|
|||||||
|
|
||||||
static async getSignups(id: number): Promise<Signup[]> {
|
static async getSignups(id: number): Promise<Signup[]> {
|
||||||
try {
|
try {
|
||||||
const resp = await axios.get(`${FORM_URL}${id}/signups/`, {
|
return await getBackendAPI<Signup[]>({ path: APIPath.SIGNUP_FORMS_SIGNUPS, urlParams: { id }, authenticated: true });
|
||||||
headers: {
|
|
||||||
Authorization: getAuthHeader(),
|
|
||||||
},
|
|
||||||
});
|
|
||||||
return resp.data;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
|
|||||||
+2
-12
@@ -1,21 +1,11 @@
|
|||||||
/* eslint-disable no-console */
|
/* eslint-disable no-console */
|
||||||
import axios from "axios";
|
|
||||||
import Tag from "@models/Tag";
|
import Tag from "@models/Tag";
|
||||||
|
import { APIPath, getBackendAPI } from "./backend";
|
||||||
export const URL = `${process.env.NEXT_PUBLIC_API_URL}/tags/`;
|
|
||||||
|
|
||||||
// eslint-disable-next-line @typescript-eslint/no-empty-interface
|
|
||||||
export interface Options {
|
|
||||||
// limit?: number;
|
|
||||||
// offset?: number;
|
|
||||||
// auth?: boolean;
|
|
||||||
}
|
|
||||||
|
|
||||||
class TagApi {
|
class TagApi {
|
||||||
static async getTags(): Promise<Tag[]> {
|
static async getTags(): Promise<Tag[]> {
|
||||||
try {
|
try {
|
||||||
const resp = await axios.get(URL);
|
return await getBackendAPI<Tag[]>({ path: APIPath.TAGS });
|
||||||
return resp.data.results;
|
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
console.error(err);
|
console.error(err);
|
||||||
throw err;
|
throw err;
|
||||||
|
|||||||
@@ -150,7 +150,7 @@ const FeedCreatePage: NextPage = () => {
|
|||||||
|
|
||||||
const feedId = id && Number(id);
|
const feedId = id && Number(id);
|
||||||
if (feedId !== undefined) {
|
if (feedId !== undefined) {
|
||||||
FeedApi.getPost(feedId, { auth: true })
|
FeedApi.getPost(feedId, true)
|
||||||
.then((res) => setFormData({
|
.then((res) => setFormData({
|
||||||
...res,
|
...res,
|
||||||
tags: (res.tags).map((inst) => inst.id) as any,
|
tags: (res.tags).map((inst) => inst.id) as any,
|
||||||
|
|||||||
@@ -2,7 +2,8 @@ import React, { useState, useEffect } from "react";
|
|||||||
import { NextPage } from "next";
|
import { NextPage } from "next";
|
||||||
import { useRouter } from "next/router";
|
import { useRouter } from "next/router";
|
||||||
import styled from "styled-components";
|
import styled from "styled-components";
|
||||||
import { generateToken, setTokenCookie, isAuthenticated } from "@utils/auth";
|
import { setTokenCookie } from "@utils/auth";
|
||||||
|
import { generateToken, isAuthenticated } from "@api/auth";
|
||||||
import AdminPageWrapper from "@views/common/AdminPageWrapper";
|
import AdminPageWrapper from "@views/common/AdminPageWrapper";
|
||||||
|
|
||||||
const Main = styled.div`
|
const Main = styled.div`
|
||||||
|
|||||||
@@ -5,7 +5,7 @@ import { toast } from "react-toastify";
|
|||||||
import AdminCreateCommon from "@views/admin/AdminCreateCommon";
|
import AdminCreateCommon from "@views/admin/AdminCreateCommon";
|
||||||
import MarkdownEditorWidget from "@components/Widgets/MarkdownEditorWidget";
|
import MarkdownEditorWidget from "@components/Widgets/MarkdownEditorWidget";
|
||||||
import { SignupForm } from "@models/Signup";
|
import { SignupForm } from "@models/Signup";
|
||||||
import SignupApi from "@api/signupApi";
|
import SignupApi, { EmailRequest } from "@api/signupApi";
|
||||||
|
|
||||||
const widgets = {
|
const widgets = {
|
||||||
markdownEditor: MarkdownEditorWidget,
|
markdownEditor: MarkdownEditorWidget,
|
||||||
@@ -67,7 +67,7 @@ const SignupEmailPage: NextPage = () => {
|
|||||||
|
|
||||||
const onSubmit = async (data) => {
|
const onSubmit = async (data) => {
|
||||||
try {
|
try {
|
||||||
const payload = data.formData;
|
const payload: EmailRequest = data.formData;
|
||||||
await SignupApi.signupFormSendEmail(payload, Number(id));
|
await SignupApi.signupFormSendEmail(payload, Number(id));
|
||||||
toast.success("Email sent successfully 😎");
|
toast.success("Email sent successfully 😎");
|
||||||
} catch (err) {
|
} catch (err) {
|
||||||
|
|||||||
@@ -1,17 +1,5 @@
|
|||||||
import axios from "axios";
|
|
||||||
import Cookies from "js-cookie";
|
import Cookies from "js-cookie";
|
||||||
|
|
||||||
const tokenUrl = `${process.env.NEXT_PUBLIC_API_URL}/api-token-auth/`;
|
|
||||||
const checkUrl = `${process.env.NEXT_PUBLIC_API_URL}/api-token-verify/`;
|
|
||||||
|
|
||||||
export async function generateToken(username: string, password: string): Promise<string> {
|
|
||||||
const resp = await axios.post(tokenUrl, {
|
|
||||||
username,
|
|
||||||
password,
|
|
||||||
});
|
|
||||||
return resp.data.token;
|
|
||||||
}
|
|
||||||
|
|
||||||
export function setTokenCookie(token: string): void {
|
export function setTokenCookie(token: string): void {
|
||||||
Cookies.set("jwt", token);
|
Cookies.set("jwt", token);
|
||||||
Cookies.set("jwt", token, { domain: ".sahkoinsinoorikilta.fi" });
|
Cookies.set("jwt", token, { domain: ".sahkoinsinoorikilta.fi" });
|
||||||
@@ -25,22 +13,3 @@ export function deleteTokenCookie(): void {
|
|||||||
Cookies.remove("jwt", { domain: ".sahkoinsinoorikilta.fi" });
|
Cookies.remove("jwt", { domain: ".sahkoinsinoorikilta.fi" });
|
||||||
Cookies.remove("jwt");
|
Cookies.remove("jwt");
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function isAuthenticated(): Promise<boolean> {
|
|
||||||
try {
|
|
||||||
const token = getTokenCookie();
|
|
||||||
await axios.post(checkUrl, {
|
|
||||||
token,
|
|
||||||
});
|
|
||||||
return true;
|
|
||||||
} catch (err) {
|
|
||||||
// remove the cookie since it's invalid
|
|
||||||
deleteTokenCookie();
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
export function getAuthHeader(): string {
|
|
||||||
const jwt = getTokenCookie();
|
|
||||||
return `JWT ${jwt}`;
|
|
||||||
}
|
|
||||||
|
|||||||
@@ -1,11 +1,11 @@
|
|||||||
import React, { useEffect, useState } from "react";
|
import React, { useEffect, useState } from "react";
|
||||||
|
import { useRouter } from "next/router";
|
||||||
import styled from "styled-components";
|
import styled from "styled-components";
|
||||||
import colors from "@theme/colors";
|
import colors from "@theme/colors";
|
||||||
import breakpoints from "@theme/breakpoints";
|
import breakpoints from "@theme/breakpoints";
|
||||||
import AdminHeader from "@components/AdminHeader";
|
import AdminHeader from "@components/AdminHeader";
|
||||||
import AdminSidebar from "@components/AdminSidebar";
|
import AdminSidebar from "@components/AdminSidebar";
|
||||||
import { isAuthenticated } from "@utils/auth";
|
import { isAuthenticated } from "@api/auth";
|
||||||
import { useRouter } from "next/router";
|
|
||||||
import LoadingView from "./LoadingView";
|
import LoadingView from "./LoadingView";
|
||||||
|
|
||||||
const Main = styled.main`
|
const Main = styled.main`
|
||||||
|
|||||||
Reference in New Issue
Block a user