2021-11-30 15:22:16 +00:00
|
|
|
class JobStatusNotifier {
|
|
|
|
constructor(userId) {
|
|
|
|
this.userId = userId;
|
2022-07-08 09:46:47 +00:00
|
|
|
this.isInitialized = false;
|
|
|
|
app.subscribeUser(this.userId).then((response) => {
|
|
|
|
app.socket.on('PATCH', (patch) => {this.onPATCH(patch);});
|
|
|
|
});
|
|
|
|
app.getUser(this.userId).then((user) => {
|
|
|
|
this.isInitialized = true;
|
|
|
|
});
|
2021-11-30 15:22:16 +00:00
|
|
|
}
|
|
|
|
|
2022-07-04 12:09:17 +00:00
|
|
|
onPATCH(patch) {
|
2022-07-08 09:46:47 +00:00
|
|
|
if (!this.isInitialized) {return;}
|
|
|
|
|
2021-12-01 13:15:20 +00:00
|
|
|
let filteredPatch;
|
|
|
|
let jobId;
|
|
|
|
let match;
|
2021-12-06 13:42:49 +00:00
|
|
|
let operation;
|
2021-12-01 13:15:20 +00:00
|
|
|
let re;
|
|
|
|
|
2022-07-04 12:09:17 +00:00
|
|
|
re = new RegExp(`^/users/${this.userId}/jobs/([A-Za-z0-9]*)/status$`);
|
2021-11-30 15:22:16 +00:00
|
|
|
filteredPatch = patch
|
2022-07-08 09:46:47 +00:00
|
|
|
.filter((operation) => {return operation.op === 'replace';})
|
|
|
|
.filter((operation) => {return re.test(operation.path);});
|
2021-12-06 13:42:49 +00:00
|
|
|
for (operation of filteredPatch) {
|
2021-11-30 15:22:16 +00:00
|
|
|
[match, jobId] = operation.path.match(re);
|
2022-07-08 09:46:47 +00:00
|
|
|
app.flash(`[<a href="/jobs/${jobId}">${app.data.users[this.userId].jobs[jobId].title}</a>] New status: <span class="job-status-text" data-job-status="${operation.value}"></span>`, 'job');
|
2021-11-30 15:22:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|