From 7a73aa57c343c8167913e7da96c507d51a3d2a10 Mon Sep 17 00:00:00 2001 From: Bauke Date: Fri, 15 Sep 2023 18:22:13 +0200 Subject: [PATCH] Add save status changes to the dedicated user label editor. --- source/options/user-label-editor.tsx | 35 ++++++++++++++++++---------- source/scss/user-label-editor.scss | 12 +++++++++- 2 files changed, 34 insertions(+), 13 deletions(-) diff --git a/source/options/user-label-editor.tsx b/source/options/user-label-editor.tsx index 890a39b..787261e 100644 --- a/source/options/user-label-editor.tsx +++ b/source/options/user-label-editor.tsx @@ -27,8 +27,8 @@ type Props = { }; type State = { - hasUnsavedChanges: boolean; newLabelUsername: string; + unsavedUserLabelIds: Set; userLabels: UserLabelsData; userLabelsToRemove: UserLabelsData; }; @@ -38,15 +38,15 @@ class App extends Component { super(props); this.state = { - hasUnsavedChanges: false, newLabelUsername: "", + unsavedUserLabelIds: new Set(), userLabels: props.userLabels, userLabelsToRemove: [], }; } addNewLabel = async () => { - const {newLabelUsername, userLabels} = this.state; + const {newLabelUsername, unsavedUserLabelIds, userLabels} = this.state; if (!isValidTildesUsername(newLabelUsername)) { return; } @@ -70,7 +70,8 @@ class App extends Component { username: existingUserLabel?.value.username ?? newLabelUsername, }), ); - this.setState({userLabels}); + unsavedUserLabelIds.add(id); + this.setState({unsavedUserLabelIds, userLabels}); }; onNewUsernameInput = (event: Event) => { @@ -79,6 +80,7 @@ class App extends Component { }; editUserLabel = (event: Event, targetId: number, key: keyof UserLabel) => { + const {unsavedUserLabelIds} = this.state; const index = this.state.userLabels.findIndex( ({value: {id}}) => id === targetId, ); @@ -95,19 +97,21 @@ class App extends Component { this.state.userLabels[index].value[key] = newValue; } + unsavedUserLabelIds.add(targetId); this.setState({ - hasUnsavedChanges: true, + unsavedUserLabelIds, userLabels: this.state.userLabels, }); }; removeUserLabel = async (targetId: number) => { - const {userLabels, userLabelsToRemove} = this.state; + const {unsavedUserLabelIds, userLabels, userLabelsToRemove} = this.state; const index = userLabels.findIndex(({value}) => value.id === targetId); userLabelsToRemove.push(...userLabels.splice(index, 1)); + unsavedUserLabelIds.add(targetId); this.setState({ - hasUnsavedChanges: true, + unsavedUserLabelIds, userLabels, userLabelsToRemove, }); @@ -120,11 +124,11 @@ class App extends Component { this.props.userLabels = this.state.userLabels; void saveUserLabels(this.props.userLabels); - this.setState({hasUnsavedChanges: false, userLabelsToRemove: []}); + this.setState({unsavedUserLabelIds: new Set(), userLabelsToRemove: []}); }; render() { - const {hasUnsavedChanges, newLabelUsername, userLabels} = this.state; + const {newLabelUsername, unsavedUserLabelIds, userLabels} = this.state; userLabels.sort((a, b) => a.value.username.localeCompare(b.value.username)); const labelGroups = new Map(); @@ -167,8 +171,9 @@ class App extends Component { await this.removeUserLabel(label.id); }; + const hasUnsavedChanges = unsavedUserLabelIds.has(label.id); userLabels.push( -
  • +
  • {index === 0 ? : undefined} { ); } + const anyUnsavedChanges = unsavedUserLabelIds.size > 0; return ( <>
    {labels}
    diff --git a/source/scss/user-label-editor.scss b/source/scss/user-label-editor.scss index f8fd245..de623d2 100644 --- a/source/scss/user-label-editor.scss +++ b/source/scss/user-label-editor.scss @@ -1,15 +1,25 @@ @import "button"; .user-label-editor { + --save-status-color: var(--blue); + + .unsaved-changes { + --save-status-color: var(--yellow); + } + input { background-color: var(--background-primary); - border: 1px solid var(--blue); + border: 1px solid var(--save-status-color); color: var(--foreground); padding: 8px; } .button { @include button; + + &.save-button { + --button-color: var(--save-status-color); + } } .info {