import ReactDOM from 'react-dom';
import React from 'react';
import {build as fetchBuild, buildLog as fetchBuildLog,cancelBuild as cancelBuildApi} from './../api/Api.jsx';
import Build from './../components/job/build/Build.jsx';
import Drawer from './../Drawer.jsx';
import page from 'page';

function dataChange(build){
  ReactDOM.render(<Build build={build} subBuild={build.subBuild}/>, document.getElementById('content'));
  ReactDOM.render(<Drawer menu="build" build={build}/>, document.getElementById('nav'));
}
async function buildChange(build){
  const actions = build.actions;
  let query = build.query;
  const data =await fetchBuild(build.number);
  actions.BuildInfoChange(data);
  const logText = await fetchBuildLog(build.number,build.subBuild)
  actions.LogChange(logText);
}
async function cancelBuild(build){
  await  cancelBuildApi(build.cancelUrl);
}
function lineSelect(build){
  window.location.hash =build.selectedLine;
}

export default function(build){
  const actions = build.actions;
  actions.BuildInfoChange.onAction = dataChange;
  actions.LogChange.onAction = dataChange;
  actions.CancelBuild.onAction = cancelBuild;
  actions.BuildChange.onAction = buildChange;
  actions.LineSelect.onAction = lineSelect;
}

@@ -177,16 +177,20 @@
 rt Build
+Page
  from '.
@@ -197,34 +197,23 @@
 /../
-components/job/build
+pages
 /Build
+Page
 .jsx
@@ -277,16 +277,96 @@
  'page';
+%0Aimport autoRefreshComponent from './../components/lib/AutoRefreshComponent.js';
 %0A%0Afuncti
@@ -393,30 +393,35 @@
 %7B%0A  
-ReactDOM.render(
+const buildPage =
 %3CBuild
+Page
  bui
@@ -462,46 +462,358 @@
 d%7D/%3E
-, document.getElementById('content'));
+%0A  if(build.inProgress)%7B%0A    const AutoRefreshBuildPage = autoRefreshComponent(buildPage,()=%3E%7B%0A      build.actions.BuildChange(%7BbuildNumber:build.number,subBuild:build.subBuild%7D);%0A    %7D);%0A    ReactDOM.render(%3CAutoRefreshBuildPage/%3E, document.getElementById('content'));%0A  %7Delse%7B%0A    ReactDOM.render(buildPage, document.getElementById('content'));%0A  %7D
 %0A  R

Build page refresh if in progress