From cddcac5fc6b4cd021145e7d8d015090fca46097c Mon Sep 17 00:00:00 2001 From: mydu Date: Tue, 25 Jun 2019 16:47:05 +0200 Subject: [PATCH] make loginand createBranch at once #58 --- importApp/src/containers/DataPrep/DataPrep.js | 59 +++----------- importApp/src/redux/modules/auth.js | 71 ---------------- importApp/src/redux/modules/repoData.js | 80 ++++++++++++------- 3 files changed, 63 insertions(+), 147 deletions(-) delete mode 100644 importApp/src/redux/modules/auth.js diff --git a/importApp/src/containers/DataPrep/DataPrep.js b/importApp/src/containers/DataPrep/DataPrep.js index 47748de6..6915e577 100644 --- a/importApp/src/containers/DataPrep/DataPrep.js +++ b/importApp/src/containers/DataPrep/DataPrep.js @@ -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'; @@ -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 }) @@ -49,28 +41,13 @@ 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}); @@ -78,15 +55,9 @@ class DataPrep extends React.Component { return (
- {branchToCreated && -
- No previous working branch - -
- } {isBranchCreated ? branch "{selectedBranch.name}" is created : - could not create branch "{branchToCreated}" + could not get branch from github, try login again } { selectedBranch && !tables && @@ -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 (
@@ -118,13 +89,9 @@ class DataPrep extends React.Component { */} - {isLogined ? - you are logined : - you are not logined - } - {isLogined && this.renderFetchTable()} + {selectedBranch && this.renderFetchTable()} ({ repoData: state.repoData, - isLogined: state.auth.isLogined, auth: state.auth.auth }) export default connect(mapStateToProps, { - loginGithub, - fetchBranches, - createBranch, - selectBranch, + loginCreateBranch, fetchAllTables, fetchDatapackage })(DataPrep); \ No newline at end of file diff --git a/importApp/src/redux/modules/auth.js b/importApp/src/redux/modules/auth.js deleted file mode 100644 index 77c94f9e..00000000 --- a/importApp/src/redux/modules/auth.js +++ /dev/null @@ -1,71 +0,0 @@ -import {get} from 'axios'; -import {userUri} from '../../config/default'; - -import {selectBranch} from './repoData'; - -import {SET_STEP} from './ui'; -export const LOGIN_GITHUB_REQUEST = 'LOGIN_GITHUB_REQUEST'; -export const LOGIN_GITHUB_SUCCESS = 'LOGIN_GITHUB_SUCCESS'; -export const LOGIN_GITHUB_FAILURE = 'LOGIN_GITHUB_FAILURE'; - -export const loginGithub = (payload) => (dispatch, getState) => { - dispatch({ - type: LOGIN_GITHUB_REQUEST, - payload - }) - const {username, token} = payload; - const branch = username; - return get(userUri, { - auth: { - username, - password: token - } - }) - .then((res) => { - dispatch({ - type: LOGIN_GITHUB_SUCCESS, - payload - }); - // const {selectedBranch} = getState().repoData; - dispatch(selectBranch({branch})); - }) - .catch(err => { - console.error(err); - dispatch({ - type: LOGIN_GITHUB_FAILURE - }) - }); -} - -/** - * REDUCER - */ - -const initialState = { - isLogined: false -} - -export default function reducer(state = initialState, action){ - const {payload} = action; - switch (action.type){ - // case SET_STEP: - // if (payload.id === '0') { - // return initialState - // } - // return state; - case LOGIN_GITHUB_SUCCESS: - return { - ...state, - isLogined: true, - auth: payload - } - case LOGIN_GITHUB_FAILURE: - return { - ...state, - isLogined: false, - auth: null - } - default: - return state; - } -} \ No newline at end of file diff --git a/importApp/src/redux/modules/repoData.js b/importApp/src/redux/modules/repoData.js index 315cbd01..08999f09 100644 --- a/importApp/src/redux/modules/repoData.js +++ b/importApp/src/redux/modules/repoData.js @@ -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', @@ -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, @@ -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 */ @@ -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,