Skip to content

Commit 92b571b

Browse files
committed
condition the save settings button on state
1 parent d41001c commit 92b571b

File tree

1 file changed

+39
-16
lines changed

1 file changed

+39
-16
lines changed

packages/vue/src/courses/piano/utility/MidiConfig.vue

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,18 @@
1212
hint="Play some notes on your input device to test the connection"
1313
></v-select>
1414
<v-select v-model="selectedOutput" :items="outputs" label="Select Output"></v-select>
15-
<v-btn class="mx-2" :loading="updatePending" @click="saveSettings"> Save these settings </v-btn>
16-
<v-btn class="mx-2" color="primary" @click="playSound">Test midi output</v-btn>
15+
<div class="d-flex justify-space-between mt-3">
16+
<v-btn color="primary" @click="playSound">Test midi output</v-btn>
17+
<v-btn
18+
v-if="configChanged"
19+
:loading="updatePending"
20+
:disabled="!configChanged"
21+
color="info"
22+
@click="saveSettings"
23+
>
24+
<span v-if="configChanged">Save these settings</span><span v-else>Settings saved</span>
25+
</v-btn>
26+
</div>
1727
</v-form>
1828
<div v-else>
1929
<p>This quilt requires a midi input device, which is not supported by this browser.</p>
@@ -71,6 +81,15 @@ export default defineComponent({
7181
const updatePending = ref(false);
7282
const user = ref<User>();
7383
84+
// managing config state updates
85+
const savedInputId = ref<string>('');
86+
const savedOutputId = ref<string>('');
87+
const configChanged = ref(false);
88+
89+
const checkConfigChanged = () => {
90+
configChanged.value = selectedInput.value !== savedInputId.value || selectedOutput.value !== savedOutputId.value;
91+
};
92+
7493
const playSound = () => {
7594
midi.value?.play([
7695
{
@@ -245,44 +264,37 @@ export default defineComponent({
245264
246265
watch(selectedInput, () => {
247266
midi.value?.selectInput(selectedInput.value);
267+
checkConfigChanged();
248268
});
249269
250270
watch(selectedOutput, () => {
251271
midi.value?.selectOutput(selectedOutput.value);
272+
checkConfigChanged();
252273
});
253274
254275
const retrieveSettings = async () => {
255276
const s = await user.value?.getCourseSettings(props._id);
256277
257278
if (s?.midiinput) {
258279
const savedInput = s.midiinput.toString();
259-
// Check if the saved input device is still available
260280
const inputExists = inputs.value.some((input) => input.value === savedInput);
261281
if (inputExists) {
262282
selectedInput.value = savedInput;
263-
} else {
264-
alertUser({
265-
text: `Configured MIDI input device is no longer available`,
266-
status: Status.error,
267-
});
268-
console.warn('Previously saved MIDI input device is no longer available');
283+
savedInputId.value = savedInput;
269284
}
270285
}
271286
272287
if (s?.midioutput) {
273288
const savedOutput = s.midioutput.toString();
274-
// Check if the saved output device is still available
275289
const outputExists = outputs.value.some((output) => output.value === savedOutput);
276290
if (outputExists) {
277291
selectedOutput.value = savedOutput;
278-
} else {
279-
alertUser({
280-
text: `Configured MIDI output device is no longer available`,
281-
status: Status.error,
282-
});
283-
console.warn('Previously saved MIDI output device is no longer available');
292+
savedOutputId.value = savedOutput;
284293
}
285294
}
295+
296+
// Initialize with no pending changes after loading
297+
configChanged.value = false;
286298
};
287299
288300
const saveSettings = async () => {
@@ -297,7 +309,17 @@ export default defineComponent({
297309
value: selectedOutput.value,
298310
},
299311
]);
312+
313+
// Update saved state references
314+
savedInputId.value = selectedInput.value;
315+
savedOutputId.value = selectedOutput.value;
316+
configChanged.value = false;
300317
updatePending.value = false;
318+
319+
alertUser({
320+
text: 'Settings updated.',
321+
status: Status.ok,
322+
});
301323
};
302324
303325
onMounted(async () => {
@@ -352,6 +374,7 @@ export default defineComponent({
352374
updatePending,
353375
playSound,
354376
saveSettings,
377+
configChanged,
355378
};
356379
},
357380
});

0 commit comments

Comments
 (0)