Fix garage deletion (#1085)

This commit is contained in:
KoalaSat 2024-01-23 11:37:04 +01:00 committed by GitHub
parent c22606023e
commit d080b6a663
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
5 changed files with 113 additions and 96 deletions

View File

@ -65,7 +65,7 @@ const RobotPage = (): JSX.Element => {
setInputToken(token);
genKey(token)
.then((key) => {
garage.upsertRobot(token, sortedCoordinators[0], {
garage.createRobot(token, sortedCoordinators[0], {
token,
pubKey: key.publicKeyArmored,
encPrivKey: key.encryptedPrivateKeyArmored,

View File

@ -292,54 +292,60 @@ const BookTable = ({
};
}, []);
const typeObj = useCallback((width: number) => {
return {
field: 'type',
headerName: t('Is'),
width: width * fontSize,
renderCell: (params: any) => {
return (
<div
style={{ cursor: 'pointer' }}
onClick={() => {
onOrderClicked(params.row.id, params.row.coordinatorShortAlias);
}}
>
{params.row.type === 1
? t(fav.mode === 'fiat' ? 'Seller' : 'Swapping Out')
: t(fav.mode === 'fiat' ? 'Buyer' : 'Swapping In')}
</div>
);
},
};
}, [fav.mode]);
const typeObj = useCallback(
(width: number) => {
return {
field: 'type',
headerName: t('Is'),
width: width * fontSize,
renderCell: (params: any) => {
return (
<div
style={{ cursor: 'pointer' }}
onClick={() => {
onOrderClicked(params.row.id, params.row.coordinatorShortAlias);
}}
>
{params.row.type === 1
? t(fav.mode === 'fiat' ? 'Seller' : 'Swapping Out')
: t(fav.mode === 'fiat' ? 'Buyer' : 'Swapping In')}
</div>
);
},
};
},
[fav.mode],
);
const amountObj = useCallback((width: number) => {
return {
field: 'amount',
headerName: t('Amount'),
type: 'number',
width: width * fontSize,
renderCell: (params: any) => {
const amount = fav.mode === 'swap' ? params.row.amount * 100 : params.row.amount;
const minAmount =
fav.mode === 'swap' ? params.row.min_amount * 100 : params.row.min_amount;
const maxAmount =
fav.mode === 'swap' ? params.row.max_amount * 100 : params.row.max_amount;
return (
<div
style={{ cursor: 'pointer' }}
onClick={() => {
onOrderClicked(params.row.id, params.row.coordinatorShortAlias);
}}
>
{amountToString(amount, params.row.has_range, minAmount, maxAmount) +
(fav.mode === 'swap' ? 'M Sats' : '')}
</div>
);
},
};
}, [fav.mode]);
const amountObj = useCallback(
(width: number) => {
return {
field: 'amount',
headerName: t('Amount'),
type: 'number',
width: width * fontSize,
renderCell: (params: any) => {
const amount = fav.mode === 'swap' ? params.row.amount * 100 : params.row.amount;
const minAmount =
fav.mode === 'swap' ? params.row.min_amount * 100 : params.row.min_amount;
const maxAmount =
fav.mode === 'swap' ? params.row.max_amount * 100 : params.row.max_amount;
return (
<div
style={{ cursor: 'pointer' }}
onClick={() => {
onOrderClicked(params.row.id, params.row.coordinatorShortAlias);
}}
>
{amountToString(amount, params.row.has_range, minAmount, maxAmount) +
(fav.mode === 'swap' ? 'M Sats' : '')}
</div>
);
},
};
},
[fav.mode],
);
const currencyObj = useCallback((width: number) => {
return {
@ -369,30 +375,33 @@ const BookTable = ({
};
}, []);
const paymentObj = useCallback((width: number) => {
return {
field: 'payment_method',
headerName: fav.mode === 'fiat' ? t('Payment Method') : t('Destination'),
width: width * fontSize,
renderCell: (params: any) => {
return (
<div
style={{ cursor: 'pointer' }}
onClick={() => {
onOrderClicked(params.row.id, params.row.coordinatorShortAlias);
}}
>
<PaymentStringAsIcons
othersText={t('Others')}
verbose={true}
size={1.7 * fontSize}
text={params.row.payment_method}
/>
</div>
);
},
};
}, [fav.mode]);
const paymentObj = useCallback(
(width: number) => {
return {
field: 'payment_method',
headerName: fav.mode === 'fiat' ? t('Payment Method') : t('Destination'),
width: width * fontSize,
renderCell: (params: any) => {
return (
<div
style={{ cursor: 'pointer' }}
onClick={() => {
onOrderClicked(params.row.id, params.row.coordinatorShortAlias);
}}
>
<PaymentStringAsIcons
othersText={t('Others')}
verbose={true}
size={1.7 * fontSize}
text={params.row.payment_method}
/>
</div>
);
},
};
},
[fav.mode],
);
const paymentSmallObj = useCallback((width: number) => {
return {

View File

@ -319,7 +319,7 @@ export class Coordinator {
console.log(e);
});
garage.upsertRobot(token, this.shortAlias, {
garage.updateRobot(token, this.shortAlias, {
...newAttributes,
tokenSHA256: authHeaders.tokenSHA256,
loading: false,
@ -378,7 +378,7 @@ export class Coordinator {
},
{ tokenSHA256: robot.tokenSHA256 },
);
garage.upsertRobot(slot?.token, this.shortAlias, {
garage.updateRobot(slot?.token, this.shortAlias, {
earnedRewards: data?.successful_withdrawal === true ? 0 : robot.earnedRewards,
});
@ -400,7 +400,7 @@ export class Coordinator {
{ tokenSHA256: robot.tokenSHA256 },
);
garage.upsertRobot(slot?.token, this.shortAlias, {
garage.updateRobot(slot?.token, this.shortAlias, {
stealthInvoices: wantsStealth,
});

View File

@ -59,10 +59,9 @@ class Garage {
const rawSlots = JSON.parse(slotsDump);
Object.values(rawSlots).forEach((rawSlot: Record<any, any>) => {
if (rawSlot?.token) {
this.createSlot(rawSlot?.token);
Object.keys(rawSlot.robots).forEach((shortAlias) => {
const rawRobot = rawSlot.robots[shortAlias];
this.upsertRobot(rawRobot.token, shortAlias, rawRobot);
this.createRobot(rawRobot.token, shortAlias, rawRobot);
});
this.currentSlot = rawSlot?.token;
}
@ -79,22 +78,14 @@ class Garage {
return currentToken ? this.slots[currentToken] ?? null : null;
};
createSlot: (token: string) => Slot | null = (token) => {
if (token !== null) {
this.slots[token] = new Slot(token);
return this.slots[token];
}
return null;
};
deleteSlot: (token?: string) => void = (token) => {
const targetIndex = token ?? this.currentSlot;
if (targetIndex) {
Reflect.deleteProperty(this.slots, targetIndex);
this.currentSlot = null;
this.save();
this.triggerHook('onRobotUpdate');
this.triggerHook('onOrderUpdate');
this.save();
}
};
@ -111,7 +102,22 @@ class Garage {
};
// Robots
upsertRobot: (token: string, shortAlias: string, attributes: Record<any, any>) => void = (
createRobot: (token: string, shortAlias: string, attributes: Record<any, any>) => void = (
token,
shortAlias,
attributes,
) => {
if (!token || !shortAlias) return;
if (this.getSlot(token) === null) {
this.slots[token] = new Slot(token);
this.slots[token]?.createRobot(shortAlias, attributes);
this.save();
this.triggerHook('onRobotUpdate');
}
};
updateRobot: (token: string, shortAlias: string, attributes: Record<any, any>) => void = (
token,
shortAlias,
attributes,
@ -120,14 +126,10 @@ class Garage {
let slot = this.getSlot(token);
if (slot === null && token) {
slot = this.createSlot(token);
}
if (slot != null) {
slot.upsertRobot(shortAlias, { token, ...attributes });
this.triggerHook('onRobotUpdate');
slot.updateRobot(shortAlias, { token, ...attributes });
this.save();
this.triggerHook('onRobotUpdate');
}
};
@ -149,8 +151,8 @@ class Garage {
} else {
slot.order = null;
}
this.triggerHook('onOrderUpdate');
this.save();
this.triggerHook('onOrderUpdate');
}
};
}

View File

@ -47,10 +47,16 @@ class Slot {
return null;
};
upsertRobot = (shortAlias: string, attributes: Record<any, any>): Robot | null => {
if (this.robots[shortAlias] === undefined)
createRobot = (shortAlias: string, attributes: Record<any, any>): Robot | null => {
if (this.robots[shortAlias] === undefined) {
this.robots[shortAlias] = new Robot(attributes ?? {});
return this.robots[shortAlias];
}
return null;
};
updateRobot = (shortAlias: string, attributes: Record<any, any>): Robot | null => {
this.robots[shortAlias].update(attributes);
if (attributes.lastOrderId !== undefined && attributes.lastOrderId != null) {