refactor request state

yhy
FrozenArcher 2024-03-12 15:44:35 +08:00
parent 0120da0760
commit 7810cbaca1
2 changed files with 26 additions and 13 deletions

View File

@ -1,5 +1,15 @@
import process from 'process';
/**
* State machine for request
*
* @example
*
* let former = this.state.rs;
* this.setState({
* rs: former.trans(true),
* })
*/
export class RequestState {
loading: boolean;
success: boolean;
@ -7,6 +17,16 @@ export class RequestState {
this.loading = true;
this.success = false;
}
trans(success: boolean): RequestState {
if (this.loading) {
this.loading = false;
this.success = success;
} else {
console.error('calling trans on not loading state');
}
return this;
}
}
/**

View File

@ -13,12 +13,10 @@ export function getTicketInfo(that: TicketDetail, id: number) {
},
})
.then(res => {
let former = that.state.rs;
if (!res.data.success) {
that.setState({
rs: {
loading: false,
success: false,
},
rs: former.trans(false),
});
} else {
const data = res.data.data;
@ -44,20 +42,15 @@ export function getTicketInfo(that: TicketDetail, id: number) {
that.setState({
ticketInfo: ticketDetail,
notes: notes,
rs: {
loading: false,
success: true,
},
rs: former.trans(true),
});
}
})
.catch(reason => {
let former = that.state.rs;
that.setState({
rs: {
loading: false,
success: false,
},
rs: former.trans(false),
});
console.log(reason);
console.error(reason);
});
}