Skip to content

Commit

Permalink
Preserve commit message when switching to the History tab (#931) (#836)…
Browse files Browse the repository at this point in the history
… (#971)

* Move commit message state to git panel (#931)

* Preserve commit message when commit fails (#836)

* Fix unit tests from refactor (#931) (#836)

* Rename commit message state variables (#931)

* summary -> commitSummary
* description -> commitDescription

* Preserve commit message when refusing to set identity when committing (#836)

- Raise error instead of returning false on fail setting identity

* Refactor commit and commit message handler functions

* GitPanel.commitFiles now handles all commit actions
* Refactor GitPanel spec to test commitFiles function instead of commitStaged
* Change commit message handler functions to be more generic
  • Loading branch information
navn-r authored Jul 8, 2021
1 parent e5ac50c commit a915184
Show file tree
Hide file tree
Showing 4 changed files with 320 additions and 375 deletions.
84 changes: 30 additions & 54 deletions src/components/CommitBox.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -34,19 +34,6 @@ export interface ICommitBoxProps {
*/
trans: TranslationBundle;

/**
* Callback to invoke in order to commit changes.
*
* @param msg - commit message
* @returns a promise which commits changes
*/
onCommit: (msg: string) => Promise<void>;
}

/**
* Interface describing component state.
*/
export interface ICommitBoxState {
/**
* Commit message summary.
*/
Expand All @@ -56,15 +43,33 @@ export interface ICommitBoxState {
* Commit message description.
*/
description: string;

/**
* Updates the commit message summary.
*
* @param summary - commit message summary
*/
setSummary: (summary: string) => void;

/**
* Updates the commit message description.
*
* @param description - commit message description
*/
setDescription: (description: string) => void;

/**
* Callback to invoke in order to commit changes.
*
* @returns a promise which commits changes
*/
onCommit: () => Promise<void>;
}

/**
* React component for entering a commit message.
*/
export class CommitBox extends React.Component<
ICommitBoxProps,
ICommitBoxState
> {
export class CommitBox extends React.Component<ICommitBoxProps> {
/**
* Returns a React component for entering a commit message.
*
Expand All @@ -73,10 +78,6 @@ export class CommitBox extends React.Component<
*/
constructor(props: ICommitBoxProps) {
super(props);
this.state = {
summary: '',
description: ''
};
}

componentDidMount(): void {
Expand All @@ -96,7 +97,7 @@ export class CommitBox extends React.Component<
const disabled = !this._canCommit();
const title = !this.props.hasFiles
? this.props.trans.__('Disabled: No files are staged for commit')
: !this.state.summary
: !this.props.summary
? this.props.trans.__('Disabled: No commit message summary')
: this.props.label;

Expand All @@ -116,7 +117,7 @@ export class CommitBox extends React.Component<
title={this.props.trans.__(
'Enter a commit message summary (a single line, preferably less than 50 characters)'
)}
value={this.state.summary}
value={this.props.summary}
onChange={this._onSummaryChange}
onKeyPress={this._onSummaryKeyPress}
/>
Expand All @@ -125,7 +126,7 @@ export class CommitBox extends React.Component<
minRows={5}
placeholder={this.props.trans.__('Description (optional)')}
title={this.props.trans.__('Enter a commit message description')}
value={this.state.description}
value={this.props.description}
onChange={this._onDescriptionChange}
/>
<input
Expand All @@ -134,7 +135,7 @@ export class CommitBox extends React.Component<
title={title}
value={this.props.label}
disabled={disabled}
onClick={this._onCommitSubmit}
onClick={this.props.onCommit}
/>
</form>
);
Expand All @@ -144,7 +145,7 @@ export class CommitBox extends React.Component<
* Whether a commit can be performed (files are staged and summary is not empty).
*/
private _canCommit(): boolean {
return !!(this.props.hasFiles && this.state.summary);
return !!(this.props.hasFiles && this.props.summary);
}

/**
Expand All @@ -157,26 +158,13 @@ export class CommitBox extends React.Component<
return binding.keys.join(' ');
};

/**
* Callback invoked upon clicking a commit message submit button or otherwise submitting the form.
*/
private _onCommitSubmit = (): void => {
const msg = this.state.summary + '\n\n' + this.state.description + '\n';
this.props.onCommit(msg);

// NOTE: we assume here that committing changes always works and we can safely clear component state
this._reset();
};

/**
* Callback invoked upon updating a commit message description.
*
* @param event - event object
*/
private _onDescriptionChange = (event: any): void => {
this.setState({
description: event.target.value
});
this.props.setDescription(event.target.value);
};

/**
Expand All @@ -185,9 +173,7 @@ export class CommitBox extends React.Component<
* @param event - event object
*/
private _onSummaryChange = (event: any): void => {
this.setState({
summary: event.target.value
});
this.props.setSummary(event.target.value);
};

/**
Expand Down Expand Up @@ -218,17 +204,7 @@ export class CommitBox extends React.Component<
commandArgs: CommandRegistry.ICommandExecutedArgs
): void => {
if (commandArgs.id === CommandIDs.gitSubmitCommand && this._canCommit()) {
this._onCommitSubmit();
this.props.onCommit();
}
};

/**
* Resets component state (e.g., in order to re-initialize the commit message input box).
*/
private _reset(): void {
this.setState({
summary: '',
description: ''
});
}
}
Loading

0 comments on commit a915184

Please sign in to comment.