From eb33e19992593006bc9d89424ed66339e505fe0a Mon Sep 17 00:00:00 2001 From: Kacper Donat Date: Sun, 6 Sep 2020 22:51:16 +0200 Subject: [PATCH] Add possibilty to register to editions --- src/api/edition.ts | 20 ++++++++++++++++++++ src/api/index.ts | 26 +++++++++++++++++++++++++- src/pages/edition/register.tsx | 29 +++++++++++++++++++++++++++++ src/pages/main.tsx | 5 ++++- src/routing.tsx | 5 +++++ webpack.config.js | 9 +++++++++ 6 files changed, 92 insertions(+), 2 deletions(-) create mode 100644 src/api/edition.ts create mode 100644 src/pages/edition/register.tsx diff --git a/src/api/edition.ts b/src/api/edition.ts new file mode 100644 index 0000000..0fac55c --- /dev/null +++ b/src/api/edition.ts @@ -0,0 +1,20 @@ +import { axios } from "@/api/index"; + +const EDITIONS_ENDPOINT = "/editions"; +const REGISTER_ENDPOINT = "/register"; + +export async function editions() { + const response = await axios.get(EDITIONS_ENDPOINT); + + return response.data; +} + +export async function join(key: string): Promise { + try { + await axios.post(REGISTER_ENDPOINT, JSON.stringify(key), { headers: { "Content-Type": "application/json" } }); + return true; + } catch (error) { + console.error(error); + return false; + } +} diff --git a/src/api/index.ts b/src/api/index.ts index 2d1e182..b118f65 100644 --- a/src/api/index.ts +++ b/src/api/index.ts @@ -1,12 +1,36 @@ import Axios from "axios"; +import store from "@/state/store" +import { AppState } from "@/state/reducer"; +import { UserState } from "@/state/reducer/user"; + + import * as user from "./user"; +import * as edition from "./edition"; export const axios = Axios.create({ - baseURL: "https://system-praktyk.stg.kadet.net/api", + baseURL: "http://system-praktyk-front.localhost:3000/api/", +}) + +axios.interceptors.request.use(config => { + const state = store.getState() as AppState; + const user = state.user as UserState; + + if (!user.loggedIn) { + return config; + } + + return { + ...config, + headers: { + ...config.headers, + Authorization: `Bearer ${ user.token }` + } + } }) const api = { user, + edition } export default api; diff --git a/src/pages/edition/register.tsx b/src/pages/edition/register.tsx new file mode 100644 index 0000000..bb53874 --- /dev/null +++ b/src/pages/edition/register.tsx @@ -0,0 +1,29 @@ +import React, { useState } from "react"; +import { Page } from "@/pages/base"; +import { useTranslation } from "react-i18next"; +import { Button, Container, TextField } from "@material-ui/core"; + +import api from "@/api"; + +export const RegisterEditionPage = () => { + const { t } = useTranslation(); + + const [key, setKey] = useState(""); + + const handleRegister = () => { + api.edition.join(key); + } + + return + + { t("edition.register") } + + + + ) => setKey(ev.currentTarget.value) } + value={ key } /> + + + +} diff --git a/src/pages/main.tsx b/src/pages/main.tsx index 70ee9b4..73d7cab 100644 --- a/src/pages/main.tsx +++ b/src/pages/main.tsx @@ -1,4 +1,4 @@ -import React, { useMemo } from "react"; +import React, { useEffect, useMemo } from "react"; import { Page } from "@/pages/base"; import { Button, Container, Stepper, Typography } from "@material-ui/core"; import { Link as RouterLink, Redirect } from "react-router-dom"; @@ -13,6 +13,7 @@ import { ProposalStep } from "@/pages/steps/proposal"; import { PlanStep } from "@/pages/steps/plan"; import { InsuranceState } from "@/state/reducer/insurance"; import { InsuranceStep } from "@/pages/steps/insurance"; +import api from "@/api"; export const MainPage = () => { const { t } = useTranslation(); @@ -24,6 +25,8 @@ export const MainPage = () => { const missingStudentData = useMemo(() => student ? getMissingStudentData(student) : [], [student]); + useEffect(() => void api.edition.editions()) + if (!student) { return ; } diff --git a/src/routing.tsx b/src/routing.tsx index 6520bb8..3726afb 100644 --- a/src/routing.tsx +++ b/src/routing.tsx @@ -5,6 +5,7 @@ import { InternshipProposalFormPage, InternshipProposalPreviewPage } from "@/pag import { NotFoundPage } from "@/pages/errors/not-found"; import SubmitPlanPage from "@/pages/internship/plan"; import { UserLoginPage } from "@/pages/user/login"; +import { RegisterEditionPage } from "@/pages/edition/register"; type Route = { name?: string; @@ -15,6 +16,10 @@ type Route = { export const routes: Route[] = [ { name: "home", path: "/", exact: true, content: () => }, + // edition + { name: "edition_register", path: "/edition/register", exact: true, content: () => }, + + // internship { name: "internship_proposal", path: "/internship/proposal", exact: true, content: () => }, { name: "internship_proposal_preview", path: "/internship/preview/proposal", exact: true, content: () => }, { name: "internship_plan", path: "/internship/plan", exact: true, content: () => }, diff --git a/webpack.config.js b/webpack.config.js index dae0296..9e0e0c6 100644 --- a/webpack.config.js +++ b/webpack.config.js @@ -54,6 +54,15 @@ const config = { disableHostCheck: true, historyApiFallback: true, overlay: true, + proxy: { + "/api": { + target: "http://system-praktyk-front.localhost:8080/", + changeOrigin: true, + pathRewrite: { + "^/api": '' + } + } + } }, optimization: { usedExports: true