From 4148a78627077ac888c3fced329189adeb5604e7 Mon Sep 17 00:00:00 2001
From: Kacper Donat <kadet1090@gmail.com>
Date: Wed, 18 Nov 2020 19:29:58 +0100
Subject: [PATCH] Redirect to previous path after login

---
 deploy-stg.sh                  | 7 +++++++
 src/api/type.ts                | 2 +-
 src/app.tsx                    | 9 ++++++---
 src/middleware.tsx             | 6 ++++--
 src/pages/edition/register.tsx | 9 +++++----
 src/pages/user/login.tsx       | 9 ++++++---
 src/routing.tsx                | 1 +
 translations/pl.yaml           | 2 ++
 webpack.config.js              | 2 +-
 9 files changed, 33 insertions(+), 14 deletions(-)
 create mode 100644 deploy-stg.sh

diff --git a/deploy-stg.sh b/deploy-stg.sh
new file mode 100644
index 0000000..b62a27d
--- /dev/null
+++ b/deploy-stg.sh
@@ -0,0 +1,7 @@
+BASEDIR=$(dirname "$0")
+
+npx webpack --mode production --progress || exit $?
+
+rsync -azv $BASEDIR/public/* system-praktyk@kadet.net:~/stg/front
+rsync -azv $BASEDIR/build/* system-praktyk@kadet.net:~/stg/front
+
diff --git a/src/api/type.ts b/src/api/type.ts
index aa6c577..08ad36f 100644
--- a/src/api/type.ts
+++ b/src/api/type.ts
@@ -2,7 +2,7 @@ import { InternshipType } from "@/data";
 import { axios } from "@/api/index";
 import { InternshipTypeDTO, internshipTypeDtoTransformer } from "@/api/dto/type";
 
-const AVAILABLE_INTERNSHIP_TYPES = '/internshipTypes';
+const AVAILABLE_INTERNSHIP_TYPES = '/internshipTypes/current';
 
 export async function available(): Promise<InternshipType[]> {
     const response = await axios.get<InternshipTypeDTO[]>(AVAILABLE_INTERNSHIP_TYPES);
diff --git a/src/app.tsx b/src/app.tsx
index dc69b6d..47d9ad6 100644
--- a/src/app.tsx
+++ b/src/app.tsx
@@ -98,9 +98,12 @@ function App() {
         </header>
         <main id="content">
             { <Switch>
-                { routes.map(({ name, content, middlewares = [], ...route }) => <Route { ...route } key={ name }>
-                    { processMiddlewares([ ...middlewares, content ]) }
-                </Route>) }
+                { routes.map(({ name, content, middlewares = [], ...route }) =>
+                    <Route { ...route } key={ name } render={ () => {
+                        const Next = () => processMiddlewares([ ...middlewares, content ])
+                        return <Next />
+                    } } />
+                ) }
             </Switch> }
         </main>
         <footer className="footer">
diff --git a/src/middleware.tsx b/src/middleware.tsx
index 621d234..1ffdfb0 100644
--- a/src/middleware.tsx
+++ b/src/middleware.tsx
@@ -1,8 +1,8 @@
 import { Middleware, route } from "@/routing";
 import { useSelector } from "react-redux";
 import { AppState, isReady } from "@/state/reducer";
-import { Redirect } from "react-router-dom";
-import React from "react";
+import { Redirect, useRouteMatch } from "react-router-dom";
+import React, { useEffect } from "react";
 import { UserState } from "@/state/reducer/user";
 
 export const isReadyMiddleware: Middleware<any, any> = Next => isLoggedInMiddleware(() => {
@@ -22,5 +22,7 @@ export const isLoggedInMiddleware: Middleware<any, any> = Next => {
         return <Next />;
     }
 
+    window.sessionStorage.setItem('back-path', window.location.pathname);
+
     return <Redirect to={ route("user_login") } />;
 }
diff --git a/src/pages/edition/register.tsx b/src/pages/edition/register.tsx
index 634636d..0fb3393 100644
--- a/src/pages/edition/register.tsx
+++ b/src/pages/edition/register.tsx
@@ -12,18 +12,19 @@ import { Alert } from "@material-ui/lab";
 import { Subset } from "@/helpers";
 import { useDispatch } from "@/state/actions";
 import { loginToEdition } from "@/pages/edition/pick";
-import { useHistory } from "react-router-dom";
+import { useHistory, useRouteMatch } from "react-router-dom";
 import { useDebouncedEffect } from "@/hooks/useDebouncedEffect";
 
 export const RegisterEditionPage = () => {
     const { t } = useTranslation();
 
-    const [key, setKey] = useState<string>("");
-    const [{ value: edition, isLoading }, setEdition] = useAsyncState<Subset<Edition> | null>(undefined);
-
     const classes = useVerticalSpacing(3);
     const dispatch = useDispatch();
     const history = useHistory();
+    const match = useRouteMatch<any>();
+
+    const [key, setKey] = useState<string>(match.params['edition'] || "");
+    const [{ value: edition, isLoading }, setEdition] = useAsyncState<Subset<Edition> | null>(undefined);
 
     useDebouncedEffect(() => {
         setEdition(api.edition.get(key));
diff --git a/src/pages/user/login.tsx b/src/pages/user/login.tsx
index 79c994f..2515e92 100644
--- a/src/pages/user/login.tsx
+++ b/src/pages/user/login.tsx
@@ -45,6 +45,10 @@ export const UserLoginPage = () => {
     const query    = new URLSearchParams(useLocation().search);
     const { t }    = useTranslation();
 
+    const redirectAfterLogin = () => {
+        history.push(window.sessionStorage.getItem('back-path') || "/");
+    }
+
     const handleSampleAdminLogin = async () => {
         await dispatch(authorizeUser(undefined, { isManager: true }));
 
@@ -53,8 +57,7 @@ export const UserLoginPage = () => {
 
     const handleSampleStudentLogin = async () => {
         await dispatch(authorizeUser());
-
-        history.push(route("home"));
+        redirectAfterLogin();
     }
 
     const handlePgLogin = async () => {
@@ -67,7 +70,7 @@ export const UserLoginPage = () => {
         (async function() {
             if (location.pathname === `${match.path}/check/pg`) {
                 await dispatch(authorizeUser(query.get("code") as string));
-                history.push("/");
+                redirectAfterLogin();
             }
         })();
     }, [ match.path ]);
diff --git a/src/routing.tsx b/src/routing.tsx
index 1df7a20..a88566b 100644
--- a/src/routing.tsx
+++ b/src/routing.tsx
@@ -37,6 +37,7 @@ export const routes: Route[] = [
 
     // edition
     { name: "edition_register", path: "/edition/register", exact: true, content: () => <RegisterEditionPage/>, middlewares: [ isLoggedInMiddleware ] },
+    { name: "edition_register_exact", path: "/edition/register/:edition", exact: true, content: () => <RegisterEditionPage/>, middlewares: [ isLoggedInMiddleware ] },
     { name: "edition_pick", path: "/edition/pick", exact: true, content: () => <PickEditionPage/>, middlewares: [ isLoggedInMiddleware ] },
 
     // internship
diff --git a/translations/pl.yaml b/translations/pl.yaml
index 5cb83fd..294a9ff 100644
--- a/translations/pl.yaml
+++ b/translations/pl.yaml
@@ -224,6 +224,8 @@ validation:
   api:
     GreaterThanOrEqualValidator: Wartość pola "{{ PropertyName }}" musi być większa bądź równa {{ ComparisonValue }}.
     NotEmptyValidator: Wartosć pola "{{ PropertyName }}" nie może być pusta.
+    NotNullValidator: Wartosć pola "{{ PropertyName }}" nie może być pusta.
+    PredicateValidator: Wartosć pola "{{ PropertyName }}" nie spełnia warunków walidacji.
   required: "To pole jest wymagane"
   email: "Wprowadź poprawny adres e-mail"
   phone: "Wprowadź poprawny numer telefonu"
diff --git a/webpack.config.js b/webpack.config.js
index 201baa4..71d5061 100644
--- a/webpack.config.js
+++ b/webpack.config.js
@@ -59,7 +59,7 @@ const config = {
         port: parseInt(process.env.APP_PORT || "3000"),
         proxy: {
             "/api": {
-                target: "https://system-praktyk.stg.kadet.net/api/",
+                target: "https://system-praktyk.dev.kadet.net/api/",
                 changeOrigin: true,
                 pathRewrite: {
                     "^/api": ''