oak-pay-business/es/components/withdrawAccount/list/index.js

163 lines
4.8 KiB
JavaScript

import { generateNewIdAsync } from 'oak-domain/lib/utils/uuid';
import assert from 'assert';
export default OakComponent({
entity: 'withdrawAccount',
isList: true,
projection: {
id: 1,
name: 1,
org: 1,
code: 1,
ofSystemId: 1,
data: 1,
channel: {
id: 1,
enabled: 1,
entity: 1,
entityId: 1,
offlineAccount: {
id: 1,
type: 1,
},
},
channelId: 1,
entity: 1,
entityId: 1,
isDefault: 1,
},
properties: {
onPick: (id) => undefined,
onCancel: () => undefined,
entity: '',
entityId: '',
},
actions: ['create', 'update', 'remove'],
formData({ data, features, legalActions }) {
return {
withdrawAccounts: data.filter(ele => ele.$$createAt$$ > 1).map((ele) => {
const { channel } = ele;
const { entity, offlineAccount } = channel;
const label = entity === 'offlineAccount' ? this.t(`withdraw::channel.offlineAccount.${offlineAccount.type}`) : this.t(`withdraw::channel.${entity}`);
const symbol = label[0];
return {
...ele,
label,
symbol,
allowUpdate: ele['#oakLegalActions'].includes('update'),
allowRemove: ele['#oakLegalActions'].includes('remove'),
};
}),
asPicker: !!this.props.onPick,
allowCreate: legalActions?.includes('create'),
};
},
data: {
upsertId: '',
isCreation: false,
selectedId: '',
},
filters: [
{
filter() {
const { entity, entityId } = this.props;
return {
entity,
entityId,
enabled: true,
};
}
}
],
methods: {
setUpsertId(id) {
this.setState({
upsertId: id,
});
},
newAccount() {
const id = this.addItem({});
this.setState({
upsertId: id,
isCreation: true,
});
},
updateAccount(id) {
this.setState({
upsertId: id,
isCreation: false,
});
},
async removeAccount(id) {
const row = this.state.withdrawAccounts.find(ele => ele.id === id);
if (row['#oakLegalActions']?.includes('remove')) {
return this.execute(undefined, undefined, undefined, [
{
entity: 'withdrawAccount',
operation: {
id: await generateNewIdAsync(),
action: 'remove',
data: {},
filter: {
id,
},
}
}
]);
}
else {
return this.execute(undefined, undefined, undefined, [
{
entity: 'withdrawAccount',
operation: {
id: await generateNewIdAsync(),
action: 'disable',
data: {},
filter: {
id,
},
}
}
]);
}
},
resetAll() {
this.clean();
this.setState({
upsertId: '',
});
},
async doUpdate() {
await this.execute();
this.resetAll();
},
switchDefault(id) {
const row = this.state.withdrawAccounts.find(ele => ele.id === id);
this.clean();
this.updateItem({ isDefault: !row.isDefault }, row.id);
},
pickOne(id) {
this.setState({
selectedId: id,
});
},
confirmPick() {
const { selectedId } = this.state;
assert(selectedId);
this.props.onPick(selectedId);
}
},
listeners: {
withdrawAccounts(prev, next) {
if (!prev.withdrawAccounts?.length && next.withdrawAccounts?.length > 1) {
const { withdrawAccounts } = this.state;
const defaultOne = withdrawAccounts.find(ele => ele.isDefault);
if (defaultOne) {
this.setState({
selectedId: defaultOne.id,
});
}
}
}
}
});