文件修改接口

dev_forge
caicai8 5 years ago
parent ba8244006a
commit fb1fe1e2a7

@ -18,7 +18,6 @@ import axios from 'axios';
* filePath:点击目录时当前目录的路径 * filePath:点击目录时当前目录的路径
* subfileType:保存当前点击目录的文件类型显示目录列表时才显示新建文件如果点击的是文件就不显示新建文件按钮 * subfileType:保存当前点击目录的文件类型显示目录列表时才显示新建文件如果点击的是文件就不显示新建文件按钮
* readMeContent:根目录下面的readme文件内容 * readMeContent:根目录下面的readme文件内容
* current_path:当前文件路径
*/ */
class CoderRootDirectory extends Component{ class CoderRootDirectory extends Component{
constructor(props){ constructor(props){
@ -30,7 +29,6 @@ class CoderRootDirectory extends Component{
http_url:undefined, http_url:undefined,
subFileType:"", subFileType:"",
readMeContent:undefined, readMeContent:undefined,
current_path:undefined,
branchList:undefined, branchList:undefined,
fileDetail:undefined, fileDetail:undefined,
@ -72,8 +70,7 @@ class CoderRootDirectory extends Component{
this.renderUrl(arr.name,arr.path,arr.type); this.renderUrl(arr.name,arr.path,arr.type);
this.getFileDetail(arr); this.getFileDetail(arr);
this.setState({ this.setState({
subFileType:arr.type, subFileType:arr.type
current_path:arr.path
}) })
} }
@ -189,7 +186,7 @@ class CoderRootDirectory extends Component{
}) })
} }
render(){ render(){
const { rootList , branch ,filePath , fileDetail , subFileType , readMeContent , current_path } = this.state; const { rootList , branch ,filePath , fileDetail , subFileType , readMeContent } = this.state;
const { branchLastCommit , http_url , isManager , isDeveloper } = this.props; const { branchLastCommit , http_url , isManager , isDeveloper } = this.props;
const { projectsId } = this.props.match.params; const { projectsId } = this.props.match.params;
@ -287,7 +284,7 @@ class CoderRootDirectory extends Component{
{ {
fileDetail && fileDetail &&
<CoderRootFileDetail detail = {fileDetail} current_path={current_path} {...this.props} {...this.state}></CoderRootFileDetail> <CoderRootFileDetail detail = {fileDetail} {...this.props} {...this.state}></CoderRootFileDetail>
} }
{/* readme.txt */} {/* readme.txt */}

@ -17,10 +17,19 @@ class CoderRootFileDetail extends Component{
constructor(props){ constructor(props){
super(props); super(props);
this.state={ this.state={
readOnly:true readOnly:true,
value:undefined
} }
} }
componentDidMount=()=>{
const { detail } = this.props;
this.setState({
value:detail.content
})
}
EditFile=()=>{ EditFile=()=>{
this.setState({ this.setState({
readOnly:false readOnly:false
@ -32,19 +41,52 @@ class CoderRootFileDetail extends Component{
}) })
} }
// 编辑文件
changeContent=(e)=>{
this.setState({
value:e
})
}
deleteFile=()=>{ deleteFile=()=>{
const { current_path , current_user , branch}= this.props; const { current_user , branch , detail }= this.props;
const { projectsId } = this.props.match.params; const { projectsId } = this.props.match.params;
const url = `/api/${current_user.login}/${projectsId}/contents/files/delete.json`; const url = `/${current_user.login}/${projectsId}/contents/files/delete.json`;
axios.delete(url,{ axios.delete(url,{
params:{ params:{
filepath:current_path, filepath:detail.path,
branch branch
} }
}).then(result=>{ }).then(result=>{
if(result){ if(result){
this.props.showNotification("删除成功!");
this.props.history.push(`/projects/${projectsId}`);
}
}).catch(error=>{
console.log(error);
})
}
// 确认修改文件
UpdateFile=()=>{
const { current_user , branch , detail}= this.props;
const { projectsId } = this.props.match.params;
const { value } = this.state;
const url =`/${current_user.login}/${projectsId}/contents/files/update.json`;
axios.put(url,{
filepath:detail.path,
branch,
content:value,
sha:detail.sha,
}).then(result=>{
if(result){
this.props.showNotification("修改成功!");
this.setState({
readOnly:true
})
} }
}).catch(error=>{ }).catch(error=>{
console.log(error); console.log(error);
@ -53,8 +95,7 @@ class CoderRootFileDetail extends Component{
render(){ render(){
const { detail } = this.props; const { detail } = this.props;
const { readOnly ,value } = this.state;
const { readOnly } = this.state;
return( return(
<div className="branchTable"> <div className="branchTable">
<p className="branchTitle f-wrap-alignCenter f-wrap-between"> <p className="branchTitle f-wrap-alignCenter f-wrap-between">
@ -66,7 +107,7 @@ class CoderRootFileDetail extends Component{
: :
<React.Fragment> <React.Fragment>
<button type="button" class="ant-btn ant-btn-sm mr10" onClick={this.CancelEdit}><span> </span></button> <button type="button" class="ant-btn ant-btn-sm mr10" onClick={this.CancelEdit}><span> </span></button>
<button type="button" class="ant-btn ant-btn-primary ant-btn-sm"><span> </span></button> <button type="button" class="ant-btn ant-btn-primary ant-btn-sm" onClick={this.UpdateFile}><span> </span></button>
</React.Fragment> </React.Fragment>
} }
@ -78,7 +119,8 @@ class CoderRootFileDetail extends Component{
<Editor <Editor
height="300px" height="300px"
theme={"vs-dark"} theme={"vs-dark"}
value={detail && detail.content} value={value}
onChange={this.changeContent}
readOnly={readOnly} readOnly={readOnly}
/> />
</div> </div>

Loading…
Cancel
Save