Web: ensure switching active jobs doesn't retain previous job data

Pinia's `$patch()` function will merge the given state with the current
state, instead of doing a replacement. As a result, going from an active
job with metadata fields `A` and `B`, to a job with metadata fields `B`
and `C` would actually have fields `A`, `B`, and `C` in the Pinia store.

This is resolved by replacing the `$patch(object)` with `$patch(function)`
and having that function replace the entire job.
This commit is contained in:
Sybren A. Stüvel 2022-05-12 10:55:42 +02:00
parent 47361cdc69
commit 07576f3225

View File

@ -32,14 +32,20 @@ export const useJobs = defineStore('jobs', {
actions: {
setActiveJobID(jobID) {
this.$patch({
activeJob: {id: jobID},
activeJob: {id: jobID, settings: {}, metadata: {}},
activeJobID: jobID,
});
},
setActiveJob(job) {
this.$patch({
activeJob: job,
activeJobID: job.id,
// The "function" form of $patch is necessary here, as otherwise it'll
// merge `job` into `state.activeJob`. As a result, it won't touch missing
// keys, which means that metadata fields that existed on the previous job
// but not on the new one will still linger around. By passing a function
// to `$patch` this is resolved.
this.$patch((state) => {
state.activeJob = job;
state.activeJobID = job.id;
state.hasChanged = true;
});
},
deselectAllJobs() {