From c679f5524cc247e30a0b7d45631d8061bb222535 Mon Sep 17 00:00:00 2001 From: Philip Molares Date: Sat, 13 Jun 2020 00:29:39 +0200 Subject: [PATCH] added redirector component (#199) * added redirector component * it will redirect every request to /$something that is not handled otherwise (/intro, /login and such) to /n/$something * added getNote API Call * added NotFound component * added LandingLayout around the NotFound component, so users can easily navigate away from the component Signed-off-by: Philip Molares --- public/api/v2/notes/old | 14 ++++++++++++ src/api/note.ts | 23 +++++++++++++++++++ src/components/error/not-found.tsx | 12 ++++++++++ src/components/redirector/redirector.tsx | 28 ++++++++++++++++++++++++ src/index.tsx | 8 +++++++ 5 files changed, 85 insertions(+) create mode 100644 public/api/v2/notes/old create mode 100644 src/components/error/not-found.tsx create mode 100644 src/components/redirector/redirector.tsx diff --git a/public/api/v2/notes/old b/public/api/v2/notes/old new file mode 100644 index 000000000..edfd3512e --- /dev/null +++ b/public/api/v2/notes/old @@ -0,0 +1,14 @@ +{ + "id": "ABC123", + "alias": "old", + "lastChange": { + "userId": "snskxnksnxksnxksnx", + "username": "testy", + "timestamp": 123456789 + }, + "viewcount": 0, + "createtime": "2020-05-22T20:46:08.962Z", + "content": "test123", + "authorship": [], + "preVersionTwoNote": true +} diff --git a/src/api/note.ts b/src/api/note.ts index 5333ce719..a399e6b53 100644 --- a/src/api/note.ts +++ b/src/api/note.ts @@ -1,6 +1,29 @@ import { expectResponseCode, getBackendUrl } from '../utils/apiUtils' import { defaultFetchConfig } from './default' +interface LastChange { + userId: string + username: string + timestamp: number +} + +export interface Note { + id: string + alias: string + lastChange: LastChange + viewcount: number + createtime: string + content: string + authorship: number[] + preVersionTwoNote: boolean +} + +export const getNote = async (noteId: string): Promise => { + const response = await fetch(getBackendUrl() + `/notes/${noteId}`) + expectResponseCode(response) + return await response.json() as Promise +} + export const deleteNote = async (noteId: string): Promise => { const response = await fetch(getBackendUrl() + `/notes/${noteId}`, { ...defaultFetchConfig, diff --git a/src/components/error/not-found.tsx b/src/components/error/not-found.tsx new file mode 100644 index 000000000..cfe20d85c --- /dev/null +++ b/src/components/error/not-found.tsx @@ -0,0 +1,12 @@ +import React from 'react' +import { LandingLayout } from '../landing/landing-layout' + +export const NotFound: React.FC = () => { + return ( + +
+

404 Not Found oops.

+
+
+ ) +} diff --git a/src/components/redirector/redirector.tsx b/src/components/redirector/redirector.tsx new file mode 100644 index 000000000..4c485a0f5 --- /dev/null +++ b/src/components/redirector/redirector.tsx @@ -0,0 +1,28 @@ +import React, { useEffect, useState } from 'react' +import { Redirect } from 'react-router' +import { useParams } from 'react-router-dom' +import { getNote } from '../../api/note' +import { NotFound } from '../error/not-found' + +interface RouteParameters { + id: string +} + +export const Redirector: React.FC = () => { + const { id } = useParams() + const [error, setError] = useState(null) + + useEffect(() => { + getNote(id) + .then((noteFromAPI) => setError(!noteFromAPI.preVersionTwoNote)) + .catch(() => setError(true)) + }, [id]) + + if (error) { + return () + } else if (!error && error != null) { + return () + } else { + return (Loading) + } +} diff --git a/src/index.tsx b/src/index.tsx index 655da15f9..dc0901e9c 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -4,6 +4,7 @@ import { Provider } from 'react-redux' import { BrowserRouter as Router, Redirect, Route, Switch } from 'react-router-dom' import { ApplicationLoader } from './components/application-loader/application-loader' import { Editor } from './components/editor/editor' +import { NotFound } from './components/error/not-found' import { LandingLayout } from './components/landing/landing-layout' import { History } from './components/landing/pages/history/history' import { Intro } from './components/landing/pages/intro/intro' @@ -12,6 +13,7 @@ import { Profile } from './components/landing/pages/profile/profile' import './global-style/index.scss' import * as serviceWorker from './service-worker' import { store } from './utils/store' +import { Redirector } from './components/redirector/redirector' ReactDOM.render( @@ -41,9 +43,15 @@ ReactDOM.render( + + + + + +