add parmas

This commit is contained in:
Simon Larsen 2022-07-12 20:31:19 +01:00
parent 1804f0e019
commit c2417743a1
No known key found for this signature in database
GPG Key ID: AB45983AA9C81CDE
8 changed files with 81 additions and 12 deletions

View File

@ -1,5 +1,5 @@
import React, { ReactElement } from 'react';
import { Routes, Route, useNavigate, useLocation } from 'react-router-dom';
import { Routes, Route, useNavigate, useLocation, useParams } from 'react-router-dom';
import LoginPage from './Pages/Login';
import SsoLoginPage from './Pages/SsoLogin';
import ForgotPasswordPage from './Pages/ForgotPassword';
@ -13,6 +13,8 @@ import User from 'CommonUI/src/Utils/User';
function App(): ReactElement {
Navigation.setNavigateHook(useNavigate());
Navigation.setLocation(useLocation());
Navigation.setParams(useParams());
if (User.isLoggedIn()) {
Navigation.navigate(DASHBOARD_URL);

View File

@ -73,7 +73,7 @@ const ProjectPicker: FunctionComponent<ComponentProps> = (
<ProjectPickerMenuItem
key={i}
title={project.name!}
route={new Route('/')}
route={new Route('/dashboard/'+project.id?.toString())}
icon={IconProp.Folder}
/>
);

View File

@ -4,11 +4,24 @@ import { JSONValue } from 'Common/Types/JSON';
export default class LocalStorage {
public static setItem(key: string, value: JSONValue | Email | URL): void {
if (typeof value === "object") {
// if of type jsonobject.
value = JSON.stringify(value);
}
localStorage.setItem(key, value as string);
}
public static getItem(key: string): JSONValue {
return localStorage.getItem(key) as JSONValue;
const value = localStorage.getItem(key) as JSONValue;
try {
if (value) {
return JSON.parse(value?.toString());
}
return value;
} catch (err) {
return value;
}
}
public static removeItem(key: string): void {

View File

@ -1,10 +1,11 @@
import Route from 'Common/Types/API/Route';
import { NavigateFunction, Location } from 'react-router-dom';
import { NavigateFunction, Location, Params } from 'react-router-dom';
import URL from 'Common/Types/API/URL';
abstract class Navigation {
private static navigateHook: NavigateFunction;
private static location: Location;
private static params: Params;
public static setNavigateHook(navigateHook: NavigateFunction): void {
this.navigateHook = navigateHook;
@ -14,6 +15,21 @@ abstract class Navigation {
this.location = location;
}
public static setParams(params: Params): void {
this.params = params;
}
public static getParams(): Params {
return this.params;
}
public static getParamByName(param: string): string | null {
if (!this.params || !this.params[param]) {
return null;
}
return this.params[param] as string;
}
public static getCurrentRoute(): Route {
return new Route(this.location.pathname);
}

View File

@ -0,0 +1,21 @@
import LocalStorage from './LocalStorage';
import { JSONObject } from 'Common/Types/JSON';
import Project from "Common/Models/Project";
export default class ProjectUtil {
public static getCurrentProject(): Project | null {
if (!LocalStorage.getItem('current_project')) {
return null;
}
const projectJson = LocalStorage.getItem('current_project') as JSONObject;
return Project.fromJSON(projectJson, Project) as Project;
}
public static setCurrentProject(project: JSONObject | Project): void {
if (project instanceof Project) {
project = project.toJSON();
}
LocalStorage.setItem('current_project', project);
}
}

View File

@ -5,6 +5,7 @@ import {
Route as PageRoute,
useNavigate,
useLocation,
useParams,
} from 'react-router-dom';
import MasterPage from './Components/MasterPage/MasterPage';
// Pages
@ -29,8 +30,10 @@ import User from 'CommonUI/src/Utils/User';
import Logout from './Pages/Logout/Logout';
const App: FunctionComponent = () => {
Navigation.setNavigateHook(useNavigate());
Navigation.setLocation(useLocation());
Navigation.setParams(useParams());
if (!User.isLoggedIn()) {
Navigation.navigate(ACCOUNTS_URL);

View File

@ -5,20 +5,34 @@ import Project from 'Common/Models/Project';
import ModelFromModal from 'CommonUI/src/Components/ModelFormModal/ModelFormModal';
import FormFieldSchemaType from 'CommonUI/src/Components/Forms/Types/FormFieldSchemaType';
import { FormType } from 'CommonUI/src/Components/Forms/ModelForm';
import { JSONObject } from 'Common/Types/JSON';
import ProjectUtil from "CommonUI/src/Utils/Project";
import { useEffect } from 'react';
const DashboardProjectPicker: FunctionComponent = (): ReactElement => {
export interface ComponentProps {
projects: Array<Project>;
}
const DashboardProjectPicker: FunctionComponent<ComponentProps> = (props: ComponentProps): ReactElement => {
const project: Project = new Project();
project.name = 'Sample 1';
const [showModel, setShowModel] = useState<boolean>(false);
const [selectedProject, setSelectedProject] = useState<Project | null>(null);
useEffect(() => {
setSelectedProject(ProjectUtil.getCurrentProject())
}, []);
useEffect(() => {
ProjectUtil.setCurrentProject(project);
}, [selectedProject]);
return (
<>
<ProjectPicker
selectedProjectName="Project One"
selectedProjectName={selectedProject?.name || ''}
selectedProjectIcon={IconProp.Folder}
projects={[project]}
projects={props.projects}
onCreateProjectButtonClicked={() => {
setShowModel(true);
}}
@ -30,7 +44,10 @@ const DashboardProjectPicker: FunctionComponent = (): ReactElement => {
setShowModel(false);
}}
submitButtonText="Create Project"
onSubmit={() => {}}
onSuccess={(project: Project) => {
setSelectedProject(project);
setShowModel(false);
}}
formProps={{
model: new Project(),
id: 'create-project-from',
@ -49,9 +66,6 @@ const DashboardProjectPicker: FunctionComponent = (): ReactElement => {
},
],
formType: FormType.Create,
onSuccess: (_value: JSONObject) => {
setShowModel(false);
},
}}
/>
) : (