Skip to content

Commit 9979bf0

Browse files
committed
Add support for Send tab selection & deletion in the model
1 parent 65e0e9f commit 9979bf0

File tree

1 file changed

+43
-4
lines changed

1 file changed

+43
-4
lines changed

src/model/send/send-store.ts

Lines changed: 43 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -55,19 +55,29 @@ export class SendStore {
5555
});
5656
}
5757

58-
autorun(() => {
59-
if (this.sendRequests.length === 0) this.addRequestInput();
60-
})
58+
if (this.sendRequests.length === 0) this.addRequestInput();
59+
this.selectedRequest = this.sendRequests[this.sendRequests.length - 1];
6160

6261
console.log('Send store initialized');
6362
});
6463

6564
@persist('list', sendRequestSchema) @observable
6665
sendRequests: Array<SendRequest> = [];
6766

67+
@observable
68+
selectedRequest!: SendRequest;
69+
6870
@action.bound
6971
addRequestInput(requestInput = new RequestInput()): RequestInput {
70-
this.sendRequests[0] = { request: requestInput, sentExchange: undefined };
72+
const newSendRequest = observable({
73+
id: uuid(),
74+
request: requestInput,
75+
sentExchange: undefined
76+
});
77+
78+
this.sendRequests.push(newSendRequest);
79+
this.selectedRequest = newSendRequest;
80+
7181
return requestInput;
7282
}
7383

@@ -79,6 +89,35 @@ export class SendStore {
7989
);
8090
}
8191

92+
@action.bound
93+
selectRequest(sendRequest: SendRequest) {
94+
this.selectedRequest = sendRequest;
95+
}
96+
97+
@action.bound
98+
deleteRequest(sendRequest: SendRequest) {
99+
const index = this.sendRequests.indexOf(sendRequest);
100+
if (index === -1) throw new Error('Attempt to delete non-existent Send request');
101+
102+
if (this.sendRequests.length === 1) {
103+
// Special case: if you close the only tab, you get a new empty tab
104+
this.addRequestInput(); // Add new fresh tab
105+
this.sendRequests.shift(); // Drop existing tab
106+
return;
107+
}
108+
109+
// Otherwise >1 tab: drop the closed tab and select an appropriate replacement
110+
if (this.selectedRequest == sendRequest) {
111+
const indexToSelect = (this.sendRequests.length > index + 1)
112+
? index + 1
113+
: index - 1;
114+
115+
this.selectRequest(this.sendRequests[indexToSelect]);
116+
}
117+
118+
this.sendRequests.splice(index, 1);
119+
}
120+
82121
readonly sendRequest = async (sendRequest: SendRequest) => {
83122
trackEvent({ category: 'Send', action: 'Sent request' });
84123

0 commit comments

Comments
 (0)