Skip to content

Commit

Permalink
make loginand createBranch at once #58
Browse files Browse the repository at this point in the history
  • Loading branch information
mydu committed Jun 25, 2019
1 parent 68b3687 commit cddcac5
Show file tree
Hide file tree
Showing 3 changed files with 63 additions and 147 deletions.
59 changes: 11 additions & 48 deletions importApp/src/containers/DataPrep/DataPrep.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ import {
} from 'design-workshop';

import {
selectBranch,
createBranch,
fetchBranches,
fetchAllTables,
fetchDatapackage
} from '../../redux/modules/repoData';

import {loginGithub} from '../../redux/modules/auth';
import {loginCreateBranch} from '../../redux/modules/repoData';

import GithubAuthModal from '../../components/GithubAuthModal';


Expand All @@ -27,16 +25,10 @@ class DataPrep extends React.Component {
}
}
componentDidMount() {
// const {repoData} = this.props
// if (!repoData.tables && !repoData.datapackage) {
// this.props.fetchDatapackage()
// this.props.fetchAllTables({branch:'master'})
// }
this.props.fetchDatapackage()
}

handleShowLogin =()=> {
this.props.fetchBranches()
this.setState({
isModalShow: true
})
Expand All @@ -49,44 +41,23 @@ class DataPrep extends React.Component {
}

handleLogin = (payload) => {
this.props.loginGithub(payload);
this.props.loginCreateBranch(payload);
this.handleCloseModal()
}

handleCreateBranch = () => {
const {auth, repoData} = this.props;
const {branches, branchToCreated} = repoData;
// TODO: hardcoded
const refBranch = branches.find((branch) => branch.name === 'master');
if (refBranch) {
this.props.createBranch({
branch: branchToCreated,
auth,
reference: {
sha: refBranch.commit.sha
}
})
}
}
renderFetchTable() {
const {repoData, auth} = this.props;
const {selectedBranch, branchToCreated, tables, isBranchCreated} = repoData;
const {repoData} = this.props;
const {selectedBranch, tables, isBranchCreated} = repoData;

const handleGetTables = () => {
this.props.fetchAllTables({branch: selectedBranch.name});
}

return (
<div>
{branchToCreated &&
<div>
<Help isColor="danger">No previous working branch</Help>
<Button isColor="info" onClick={this.handleCreateBranch}>Create a new branch</Button>
</div>
}
{isBranchCreated ?
<Help isColor="success">branch "{selectedBranch.name}" is created</Help> :
<Help isColor="danger">could not create branch "{branchToCreated}"</Help>
<Help isColor="danger">could not get branch from github, try login again</Help>
}
{
selectedBranch && !tables &&
Expand All @@ -101,8 +72,8 @@ class DataPrep extends React.Component {
}

render () {
const {repoData, isLogined} = this.props;
const {branches, selectedBranch, tables} = repoData;
const {repoData} = this.props;
const {selectedBranch} = repoData;

return (
<div>
Expand All @@ -118,13 +89,9 @@ class DataPrep extends React.Component {
</Select>
<Button isColor="info" onClick={handleGetTables}>Fetch</Button> */}
<Button isColor="info" onClick={this.handleShowLogin}>
<span>{isLogined ? "Re-Login" : "Login"} to get branch</span>
<span>Login to get branch</span>
</Button>
{isLogined ?
<Help isColor="success">you are logined</Help> :
<Help isColor="danger">you are not logined</Help>
}
{isLogined && this.renderFetchTable()}
{selectedBranch && this.renderFetchTable()}
<GithubAuthModal
isActive={this.state.isModalShow}
isCommit={false}
Expand All @@ -137,15 +104,11 @@ class DataPrep extends React.Component {

const mapStateToProps = state => ({
repoData: state.repoData,
isLogined: state.auth.isLogined,
auth: state.auth.auth
})

export default connect(mapStateToProps, {
loginGithub,
fetchBranches,
createBranch,
selectBranch,
loginCreateBranch,
fetchAllTables,
fetchDatapackage
})(DataPrep);
71 changes: 0 additions & 71 deletions importApp/src/redux/modules/auth.js

This file was deleted.

80 changes: 52 additions & 28 deletions importApp/src/redux/modules/repoData.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,14 @@ export const CREATE_BRANCH_REQUEST = 'CREATE_BRANCH_REQUEST';
export const CREATE_BRANCH_SUCCESS = 'CREATE_BRANCH_SUCCESS';
export const CREATE_BRANCH_FAILURE = 'CREATE_BRANCH_FAILURE';

export const LOGIN_CREATE_BRANCH_REQUEST = 'LOGIN_CREATE_BRANCH_REQUEST';
export const LOGIN_CREATE_BRANCH_SUCCESS = 'LOGIN_CREATE_BRANCH_SUCCESS';
export const LOGIN_CREATE_BRANCH_FAILURE = 'LOGIN_CREATE_BRANCH_FAILURE';

export const UPDATE_REMOTE_FILES_REQUEST = 'UPDATE_REMOTE_FILES_REQUEST';
export const UPDATE_REMOTE_FILES_SUCCESS = 'UPDATE_REMOTE_FILES_SUCCESS';
export const UPDATE_REMOTE_FILES_FAILURE = 'UPDATE_REMOTE_FILES_FAILURE';

export const SELECT_BRANCH = 'SELECT_BRANCH';

export const tablesList = [
{
name: 'sources',
Expand Down Expand Up @@ -72,11 +74,6 @@ const DEFAULT_MESSAGE = 'update data'
* ACTIONS
*/

export const selectBranch = (payload) => ({
type: SELECT_BRANCH,
payload
})

export const fetchBranches = (payload) => (dispatch) => {
dispatch({
type: FETCH_BRANCHES_REQUEST,
Expand Down Expand Up @@ -235,6 +232,52 @@ export const createBranch = (payload) => (dispatch) => {
}))
}

export const loginCreateBranch = (payload) => (dispatch) => {
dispatch({
type: LOGIN_CREATE_BRANCH_REQUEST,
payload
})
const {username, token} = payload;

const github = new Octokat({
username: username,
password: token
});

dispatch(async () => {
try {
let repo = await github.repos(owner, repoName).fetch();
let branches = await repo.branches.fetch();
let selectedBranch = branches.items.find((branch) => branch.name === username);
if (!selectedBranch) {
const refBranch = branches.items.find((branch) => branch.name === 'master');
selectedBranch = await repo.git.refs.create({
ref: `refs/heads/${username}`,
sha: refBranch.commit.sha
})
}
dispatch({
type: LOGIN_CREATE_BRANCH_SUCCESS,
payload: {
name: username,
ref: selectedBranch
}
})

} catch(error) {
console.error(error)
dispatch({
type: LOGIN_CREATE_BRANCH_FAILURE,
payload: {
error
}
})
}
})


}

/**
* REDUCER
*/
Expand Down Expand Up @@ -267,37 +310,18 @@ export default function reducer(state = initialState, action){
...state,
branches: payload.branches
}
case CREATE_BRANCH_SUCCESS:
case LOGIN_CREATE_BRANCH_SUCCESS:
return {
...state,
selectedBranch: payload,
isBranchCreated: true,
branchToCreated: null
}
case CREATE_BRANCH_FAILURE:
case LOGIN_CREATE_BRANCH_FAILURE:
return {
...state,
selectedBranch: null,
isBranchCreated: false
}
case SELECT_BRANCH:
const selectedBranch = state.branches.find((branch) => branch.name === payload.branch);
if (selectedBranch) {
return {
...state,
selectedBranch,
isBranchCreated: true,
tables: null
}
} else {
return {
...state,
branchToCreated: payload.branch,
isBranchCreated: false,
selectedBranch: null,
tables: null
}
}
case UPDATE_REMOTE_FILES_REQUEST:
return {
...state,
Expand Down

0 comments on commit cddcac5

Please sign in to comment.