add restentpoint for login/logout, restructuring of modules and components, add flow usage

This commit is contained in:
Maren Süwer
2018-07-04 16:43:46 +02:00
parent 85902010ce
commit 3cc87ede73
22 changed files with 646 additions and 99 deletions

View File

@@ -1,20 +0,0 @@
import React, { Component } from 'react';
import Navigation from './Navigation';
import Main from './Main';
import {withRouter} from 'react-router-dom';
import 'ces-theme/dist/css/ces.css';
class App extends Component {
render() {
return (
<div className="App">
<Navigation />
<Main />
</div>
);
}
}
export default withRouter(App);

64
scm-ui/src/apiclient.js Normal file
View File

@@ -0,0 +1,64 @@
// @flow
// get api base url from environment
const apiUrl = process.env.API_URL || process.env.PUBLIC_URL || "";
export const PAGE_NOT_FOUND_ERROR = Error("page not found");
// fetch does not send the X-Requested-With header (https://github.com/github/fetch/issues/17),
// but we need the header to detect ajax request (AjaxAwareAuthenticationRedirectStrategy).
const fetchOptions: RequestOptions = {
credentials: "same-origin",
headers: {
"X-Requested-With": "XMLHttpRequest"
}
};
function handleStatusCode(response: Response) {
if (!response.ok) {
if (response.status === 401) {
return response;
}
if (response.status === 404) {
throw PAGE_NOT_FOUND_ERROR;
}
throw new Error("server returned status code " + response.status);
}
return response;
}
function createUrl(url: string) {
return `${apiUrl}/api/rest/v2/${url}`;
}
class ApiClient {
get(url: string) {
return fetch(createUrl(url), fetchOptions).then(handleStatusCode);
}
post(url: string, payload: any) {
return this.httpRequestWithJSONBody(url, payload, "POST");
}
delete(url: string, payload: any) {
let options: RequestOptions = {
method: "DELETE"
};
options = Object.assign(options, fetchOptions);
return fetch(createUrl(url), options).then(handleStatusCode);
}
httpRequestWithJSONBody(url: string, payload: any, method: string) {
let options: RequestOptions = {
method: method,
body: JSON.stringify(payload)
};
options = Object.assign(options, fetchOptions);
// $FlowFixMe
options.headers["Content-Type"] = "application/json";
return fetch(createUrl(url), options).then(handleStatusCode);
}
}
export let apiClient = new ApiClient();

View File

@@ -0,0 +1,35 @@
import React, { Component } from "react";
import Navigation from "./Navigation";
import Main from "./Main";
import Login from "./Login";
import { withRouter } from "react-router-dom";
type Props = {
login: boolean
}
class App extends Component {
render() {
const { login} = this.props;
if(login) {
return (
<div>
<Login/>
</div>
);
}
else {
return (
<div className="App">
<Navigation />
<Main />
</div>
);
}
}
}
export default withRouter(App);

View File

@@ -5,8 +5,8 @@ import classNames from 'classnames';
import { Route, withRouter } from 'react-router';
import Repositories from './containers/Repositories';
import Users from './containers/Users';
import Repositories from '../repositories/containers/Repositories';
import Users from '../users/containers/Users';
import {Switch} from 'react-router-dom';
const styles = {

View File

@@ -1,12 +1,15 @@
import thunk from 'redux-thunk';
import logger from 'redux-logger';
import { createStore, compose, applyMiddleware, combineReducers } from 'redux';
import { routerReducer, routerMiddleware } from 'react-router-redux';
// @flow
import thunk from "redux-thunk";
import logger from "redux-logger";
import { createStore, compose, applyMiddleware, combineReducers } from "redux";
import { routerReducer, routerMiddleware } from "react-router-redux";
import repositories from './modules/repositories';
import users from './modules/users';
import repositories from "./repositories/modules/repositories";
import users from "./users/modules/users";
function createReduxStore(history) {
import type {BrowserHistory} from "history/createBrowserHistory";
function createReduxStore(history: BrowserHistory) {
const composeEnhancers = window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ || compose;
const reducer = combineReducers({

View File

@@ -1,32 +1,41 @@
import React from 'react';
import ReactDOM from 'react-dom';
import App from './App';
import registerServiceWorker from './registerServiceWorker';
// @flow
import React from "react";
import ReactDOM from "react-dom";
import App from "./containers/App";
import registerServiceWorker from "./registerServiceWorker";
import { Provider } from 'react-redux';
import createHistory from 'history/createBrowserHistory';
import createReduxStore from './createReduxStore';
import { ConnectedRouter } from 'react-router-redux';
import { Provider } from "react-redux";
import createHistory from "history/createBrowserHistory";
import type { BrowserHistory } from "history/createBrowserHistory";
import createReduxStore from "./createReduxStore";
import { ConnectedRouter } from "react-router-redux";
const publicUrl: string = process.env.PUBLIC_URL || "";
// Create a history of your choosing (we're using a browser history in this case)
const history = createHistory({
basename: process.env.PUBLIC_URL
const history: BrowserHistory = createHistory({
basename: publicUrl
});
window.appHistory = history;
// Add the reducer to your store on the `router` key
// Also apply our middleware for navigating
const store = createReduxStore(history);
const root = document.getElementById("root");
if (!root) {
throw new Error("could not find root element");
}
ReactDOM.render(
<Provider store={store}>
{ /* ConnectedRouter will use the store from Provider automatically */}
<ConnectedRouter history={history}>
<App />
</ConnectedRouter>
{/* ConnectedRouter will use the store from Provider automatically */}
<ConnectedRouter history={history}>
<App />
</ConnectedRouter>
</Provider>,
document.getElementById('root')
root
);
registerServiceWorker();

View File

@@ -3,12 +3,14 @@ import React from 'react';
import { connect } from 'react-redux';
import { fetchRepositoriesIfNeeded } from '../modules/repositories';
import Login from '../Login';
import Login from '../../containers/Login';
type Props = {
login: boolean,
error: any,
error: Error,
repositories: any,
fetchRepositoriesIfNeeded: () => void
}
class Repositories extends React.Component<Props> {
@@ -21,16 +23,7 @@ class Repositories extends React.Component<Props> {
const { login, error, repositories } = this.props;
if(login) {
return (
<div>
<h1>SCM</h1>
<Login/>
</div>
);
}
else if(!login){
return (
return (
<div>
<h1>SCM</h1>
<h2>Startpage</h2>
@@ -38,8 +31,7 @@ class Repositories extends React.Component<Props> {
Users hier!
</a>
</div>
);
}
)
}

View File

@@ -1,4 +1,3 @@
//@flow
const FETCH_REPOSITORIES = 'scm/repositories/FETCH';
const FETCH_REPOSITORIES_SUCCESS = 'scm/repositories/FETCH_SUCCESS';
const FETCH_REPOSITORIES_FAILURE = 'scm/repositories/FETCH_FAILURE';

View File

@@ -3,7 +3,7 @@ import React from 'react';
import { connect } from 'react-redux';
import { fetchUsersIfNeeded } from '../modules/users';
import Login from '../Login';
import Login from '../../containers/Login';
type Props = {
login: boolean,
@@ -22,23 +22,13 @@ class Users extends React.Component<Props> {
const { login, error, users } = this.props;
if(login) {
return (
<div>
<h1>SCM</h1>
<Login/>
</div>
);
}
else if(!login){
return (
<div>
<h1>SCM</h1>
<h2>Users</h2>
</div>
);
}
}
}

View File

@@ -1,10 +1,7 @@
//@flow
const FETCH_USERS = 'scm/users/FETCH';
const FETCH_USERS_SUCCESS= 'scm/users/FETCH_SUCCESS';
const FETCH_USERS_FAILURE = 'scm/users/FETCH_FAILURE';
const THRESHOLD_TIMESTAMP = 10000;
function requestUsers() {
return {
type: FETCH_USERS