mirror of
https://github.com/rjNemo/ticket_manager
synced 2026-06-06 08:46:39 +00:00
redirect to error page if project get unsuccessful
This commit is contained in:
parent
19116dc5f3
commit
202eaad2df
3 changed files with 29 additions and 11 deletions
|
|
@ -1,31 +1,42 @@
|
||||||
import React, { FC, useState, useEffect } from "react";
|
import React, { FC, useState, useEffect } from "react";
|
||||||
import { useParams } from "react-router-dom";
|
import { useParams, Redirect } from "react-router-dom";
|
||||||
import { ProjectPage } from "../pages/ProjectPage";
|
import { ProjectPage } from "../pages/ProjectPage";
|
||||||
import ProjectVM from "../VM/ProjectVM";
|
import ProjectVM from "../VM/ProjectVM";
|
||||||
import { Constants } from "../utils/Constants";
|
|
||||||
import { Project } from "../types/Project";
|
import { Project } from "../types/Project";
|
||||||
import { Preloader } from "../components/Preloader";
|
import { Preloader } from "../components/Preloader";
|
||||||
|
import { Constants } from "../utils/Constants";
|
||||||
|
import { HttpResponse, get } from "../utils/http";
|
||||||
|
|
||||||
export const ProjectController: FC = () => {
|
export const ProjectController: FC = () => {
|
||||||
const [project, setProject] = useState({} as Project);
|
const [project, setProject] = useState({} as Project);
|
||||||
const [isLoading, setIsLoading] = useState(true);
|
const [isLoading, setIsLoading] = useState(true);
|
||||||
|
const [hasError, setHasError] = useState(false);
|
||||||
const { id } = useParams();
|
const { id } = useParams();
|
||||||
|
|
||||||
const getProject: (id: string) => void = async (id: string) => {
|
async function httpGet(id: string): Promise<void> {
|
||||||
await fetch(`${Constants.getProjectURI}/${id}`)
|
try {
|
||||||
.then((res: Response) => res.json())
|
const response: HttpResponse<Project> = await get<Project>(
|
||||||
.catch(err => console.log(err))
|
`${Constants.getProjectURI}/${id}`
|
||||||
.then(data => setProject(data))
|
);
|
||||||
.finally(() => setIsLoading(false));
|
if (response.parsedBody !== undefined) {
|
||||||
};
|
setProject(response.parsedBody);
|
||||||
|
setIsLoading(false);
|
||||||
|
}
|
||||||
|
} catch (ex) {
|
||||||
|
setHasError(true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
useEffect(() => {
|
useEffect(() => {
|
||||||
if (id !== undefined) {
|
if (id !== undefined) {
|
||||||
getProject(id);
|
httpGet(id);
|
||||||
}
|
}
|
||||||
}, [id]);
|
}, [id]);
|
||||||
|
|
||||||
const viewModel = new ProjectVM(project);
|
const viewModel = new ProjectVM(project);
|
||||||
|
|
||||||
|
if (hasError) {
|
||||||
|
return <Redirect to="/error" />;
|
||||||
|
}
|
||||||
return isLoading ? <Preloader /> : <ProjectPage viewModel={viewModel} />;
|
return isLoading ? <Preloader /> : <ProjectPage viewModel={viewModel} />;
|
||||||
};
|
};
|
||||||
|
|
|
||||||
|
|
@ -1,3 +1,5 @@
|
||||||
|
import { Redirect } from "react-router-dom";
|
||||||
|
|
||||||
export interface HttpResponse<T> extends Response {
|
export interface HttpResponse<T> extends Response {
|
||||||
parsedBody?: T;
|
parsedBody?: T;
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -10,6 +10,7 @@ import * as creacteHistory from "history";
|
||||||
// import { TicketPage } from "../pages/TicketPage";
|
// import { TicketPage } from "../pages/TicketPage";
|
||||||
// import { HomeController } from "../controllers/HomeController";
|
// import { HomeController } from "../controllers/HomeController";
|
||||||
import { ProjectController } from "../controllers/ProjectController";
|
import { ProjectController } from "../controllers/ProjectController";
|
||||||
|
import { ErrorPage } from "../pages/ErrorPage";
|
||||||
// import { UserController } from "../controllers/UserController";
|
// import { UserController } from "../controllers/UserController";
|
||||||
// import { TicketController } from "../controllers/TicketController";
|
// import { TicketController } from "../controllers/TicketController";
|
||||||
|
|
||||||
|
|
@ -33,8 +34,12 @@ export const AppRouter = () => {
|
||||||
<TicketController />
|
<TicketController />
|
||||||
</Route> */}
|
</Route> */}
|
||||||
|
|
||||||
|
<Route path="/error">
|
||||||
|
<ErrorPage error="" />
|
||||||
|
</Route>
|
||||||
|
|
||||||
<Route path="*">
|
<Route path="*">
|
||||||
<Redirect to="/404" />
|
<Redirect to="/error" />
|
||||||
</Route>
|
</Route>
|
||||||
</Switch>
|
</Switch>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue