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'; import process from 'process';
/**
* State machine for request
*
* @example
*
* let former = this.state.rs;
* this.setState({
* rs: former.trans(true),
* })
*/
export class RequestState { export class RequestState {
loading: boolean; loading: boolean;
success: boolean; success: boolean;
@ -7,6 +17,16 @@ export class RequestState {
this.loading = true; this.loading = true;
this.success = false; 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 => { .then(res => {
let former = that.state.rs;
if (!res.data.success) { if (!res.data.success) {
that.setState({ that.setState({
rs: { rs: former.trans(false),
loading: false,
success: false,
},
}); });
} else { } else {
const data = res.data.data; const data = res.data.data;
@ -44,20 +42,15 @@ export function getTicketInfo(that: TicketDetail, id: number) {
that.setState({ that.setState({
ticketInfo: ticketDetail, ticketInfo: ticketDetail,
notes: notes, notes: notes,
rs: { rs: former.trans(true),
loading: false,
success: true,
},
}); });
} }
}) })
.catch(reason => { .catch(reason => {
let former = that.state.rs;
that.setState({ that.setState({
rs: { rs: former.trans(false),
loading: false,
success: false,
},
}); });
console.log(reason); console.error(reason);
}); });
} }