-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #4 from IS3106-T07/hansel
Update to CRA-v2, and new project structure
- Loading branch information
Showing
34 changed files
with
1,205 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,43 +1,94 @@ | ||
# Frontend Setup | ||
> Instructions to install dependencies and run frontend development environment. | ||
## Project Files | ||
This is our basic file structure, including `build/`, `public/` and `src/`. | ||
``` | ||
/ | ||
├── build | ||
│ └── ...generated static resources: css, js, media | ||
│ | ||
├── public | ||
│ └── ...favicon.ico, index.html, manifest.json | ||
│ | ||
├── src | ||
│ └── ...development files | ||
│ | ||
├── .gitignore | ||
├── package.json (dependencies, scripts, configuration) | ||
└── README.md | ||
``` | ||
## Development | ||
For development, `src/` contains the main assets. There are `components/`, `resources/`, redux-based `actions/`, `constants/`, `reducers/` and `services/`. | ||
|
||
### Directory | ||
. | ||
├── create-react-app # For installing dependencies, you can delete after install | ||
└── frontend # Actual development environment | ||
├── other files... # Stores App.js, components/, etc.. | ||
└── (to be updated) | ||
**Note**: each folder contains a file called `index.js`. This helps to shorten import statements, and the decision to include these is for convenience. | ||
|
||
### Install Create-React-App (only for dependencies) | ||
Link: /~https://github.com/IS3106-T07/create-react-app | ||
``` | ||
/ ... | ||
└── src | ||
├── actions | ||
│ ├── ...actions.js | ||
│ └── index.js | ||
├── components | ||
│ ├── css | ||
│ │ └── ...example.module.css | ||
│ ├── App.js | ||
│ ├── PrivateRoute.js | ||
│ └── ...PageComponents.js | ||
├── constants | ||
│ ├── ...constants.js | ||
│ └── index.js | ||
├── reducers | ||
│ ├── ...reducer.js | ||
│ └── index.js | ||
├── resources | ||
│ └── (images) | ||
├── services | ||
│ ├── ...service.js | ||
│ └── index.js | ||
└ ... | ||
``` | ||
|
||
git clone /~https://github.com/IS3106-T07/create-react-app | ||
cd create-react-app | ||
npm install | ||
And `helpers/`, `index.html` and `index.js`. | ||
``` | ||
... | ||
├── helpers | ||
│ ├── auth-header.js | ||
│ ├── fake-backend.js | ||
│ ├── history.js | ||
│ ├── index.js | ||
│ ├── serviceWorker.js | ||
│ └── store.js | ||
├── index.html | ||
└── index.js | ||
``` | ||
|
||
### Install Frontend | ||
Link: Current Page | ||
## Using this project | ||
The development server runs on `PORT 8000`. See *scripts* in `package.json` to change. | ||
Configure *apiUrl* at `src/index.js`. Default value: http://localhost:3000. | ||
|
||
cd .. | ||
git clone /~https://github.com/IS3106-T07/frontend | ||
cd frontend | ||
npm install | ||
**Note**: `HTTPS` is required for service workers. | ||
|
||
Run in **development** | ||
``` | ||
npm install | ||
npm start | ||
HTTPS=true npm start // Linux, macOS (bash) | ||
set HTTPS=true&&npm start // Windows (cmd.exe) | ||
($env:HTTPS = $true) -and (npm start) // Windows (powershell) | ||
``` | ||
Run in **production** | ||
``` | ||
npm install | ||
npm run build | ||
serve -s build | ||
``` | ||
Run **tests** | ||
``` | ||
npm build | ||
npm run test | ||
``` | ||
|
||
### Testing | ||
npm test | ||
## Creating a component | ||
``` | ||
// todo Guide | ||
``` | ||
|
||
### Run | ||
npm start | ||
|
||
Running at http://localhost:3003 (and at specified IP network address, e.g. 192.168.1.40:3003) | ||
|
||
# FAQ | ||
> Frequently asked questions. | ||
### Q. I'm stuck at registry config, on install. How to proceed? | ||
Set config manually. | ||
|
||
npm config set registry="http://registry.npmjs.org" | ||
|
||
### Q. Where is the PWA Setup Guide? | ||
See MANUAL.md. | ||
This brings us to the end of project-specific documentation. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
import { alertConstants } from '../constants'; | ||
|
||
export const alertActions = { | ||
success, | ||
error, | ||
clear | ||
}; | ||
|
||
function success(message) { | ||
return { type: alertConstants.SUCCESS, message }; | ||
} | ||
|
||
function error(message) { | ||
return { type: alertConstants.ERROR, message }; | ||
} | ||
|
||
function clear() { | ||
return { type: alertConstants.CLEAR }; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
export * from './alert.actions'; | ||
export * from './user.actions'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
import { userConstants } from '../constants'; | ||
import { userService, history } from '../helpers'; | ||
import { alertActions } from './'; | ||
|
||
export const userActions = { | ||
login, | ||
logout, | ||
register, | ||
getAll, | ||
delete: _delete | ||
}; | ||
|
||
function login(email, password) { | ||
return dispatch => { | ||
dispatch(request({ email })); | ||
userService.login(email, password) | ||
.then( | ||
user => { | ||
dispatch(success(user)); | ||
history.push('/'); | ||
}, | ||
error => { | ||
dispatch(failure(error.toString())); | ||
console.log(error); | ||
dispatch(alertActions.error(error.toString())); | ||
} | ||
); | ||
}; | ||
|
||
function request(user) { return { type: userConstants.LOGIN_REQUEST, user } } | ||
function success(user) { return { type: userConstants.LOGIN_SUCCESS, user } } | ||
function failure(error) { return { type: userConstants.LOGIN_FAILURE, error } } | ||
} | ||
|
||
function logout() { | ||
userService.logout(); | ||
return { type: userConstants.LOGOUT }; | ||
} | ||
|
||
function register(user) { | ||
return dispatch => { | ||
dispatch(request(user)); | ||
|
||
userService.register(user) | ||
.then( | ||
user => { | ||
dispatch(success()); | ||
history.push('/login'); | ||
dispatch(alertActions.success('Registration successful')); | ||
}, | ||
error => { | ||
dispatch(failure(error.toString())); | ||
dispatch(alertActions.error(error.toString())); | ||
} | ||
); | ||
}; | ||
|
||
function request(user) { return { type: userConstants.REGISTER_REQUEST, user } } | ||
function success(user) { return { type: userConstants.REGISTER_SUCCESS, user } } | ||
function failure(error) { return { type: userConstants.REGISTER_FAILURE, error } } | ||
} | ||
|
||
function getAll() { | ||
return dispatch => { | ||
dispatch(request()); | ||
|
||
userService.getAll() | ||
.then( | ||
users => dispatch(success(users)), | ||
error => dispatch(failure(error.toString())) | ||
); | ||
}; | ||
|
||
function request() { return { type: userConstants.GETALL_REQUEST } } | ||
function success(users) { return { type: userConstants.GETALL_SUCCESS, users } } | ||
function failure(error) { return { type: userConstants.GETALL_FAILURE, error } } | ||
} | ||
|
||
// prefixed function name with underscore because delete is a reserved word in javascript | ||
function _delete(id) { | ||
return dispatch => { | ||
dispatch(request(id)); | ||
|
||
userService.delete(id) | ||
.then( | ||
user => dispatch(success(id)), | ||
error => dispatch(failure(id, error.toString())) | ||
); | ||
}; | ||
|
||
function request(id) { return { type: userConstants.DELETE_REQUEST, id } } | ||
function success(id) { return { type: userConstants.DELETE_SUCCESS, id } } | ||
function failure(id, error) { return { type: userConstants.DELETE_FAILURE, id, error } } | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
import React from 'react'; | ||
import { Router, Route } from 'react-router-dom'; | ||
import { connect } from 'react-redux'; | ||
|
||
import { history } from '../helpers'; | ||
import { alertActions } from '../actions'; | ||
import { PrivateRoute } from './PrivateRoute'; | ||
import { HomePage } from './HomePage'; | ||
import { LoginPage } from './LoginPage'; | ||
import { RegisterPage } from './RegisterPage'; | ||
|
||
class App extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
|
||
const { dispatch } = this.props; | ||
history.listen((location, action) => { | ||
// clear alert on location change | ||
dispatch(alertActions.clear()); | ||
}); | ||
} | ||
|
||
render() { | ||
const { alert } = this.props; | ||
return ( | ||
<div className="jumbotron"> | ||
<div className="container"> | ||
<div className="col-sm-8 col-sm-offset-2"> | ||
{alert.message && | ||
<div className={`alert ${alert.type}`}>{alert.message}</div> | ||
} | ||
<Router history={history}> | ||
<div> | ||
<PrivateRoute exact path="/" component={HomePage} /> | ||
<Route path="/login" component={LoginPage} /> | ||
<Route path="/register" component={RegisterPage} /> | ||
</div> | ||
</Router> | ||
</div> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
|
||
function mapStateToProps(state) { | ||
const { alert } = state; | ||
return { | ||
alert | ||
}; | ||
} | ||
|
||
const connectedApp = connect(mapStateToProps)(App); | ||
export { connectedApp as App }; |
Oops, something went wrong.