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
5 changed files with 113 additions and 96 deletions

View File

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

View File

@ -292,7 +292,8 @@ const BookTable = ({
}; };
}, []); }, []);
const typeObj = useCallback((width: number) => { const typeObj = useCallback(
(width: number) => {
return { return {
field: 'type', field: 'type',
headerName: t('Is'), headerName: t('Is'),
@ -312,9 +313,12 @@ const BookTable = ({
); );
}, },
}; };
}, [fav.mode]); },
[fav.mode],
);
const amountObj = useCallback((width: number) => { const amountObj = useCallback(
(width: number) => {
return { return {
field: 'amount', field: 'amount',
headerName: t('Amount'), headerName: t('Amount'),
@ -339,7 +343,9 @@ const BookTable = ({
); );
}, },
}; };
}, [fav.mode]); },
[fav.mode],
);
const currencyObj = useCallback((width: number) => { const currencyObj = useCallback((width: number) => {
return { return {
@ -369,7 +375,8 @@ const BookTable = ({
}; };
}, []); }, []);
const paymentObj = useCallback((width: number) => { const paymentObj = useCallback(
(width: number) => {
return { return {
field: 'payment_method', field: 'payment_method',
headerName: fav.mode === 'fiat' ? t('Payment Method') : t('Destination'), headerName: fav.mode === 'fiat' ? t('Payment Method') : t('Destination'),
@ -392,7 +399,9 @@ const BookTable = ({
); );
}, },
}; };
}, [fav.mode]); },
[fav.mode],
);
const paymentSmallObj = useCallback((width: number) => { const paymentSmallObj = useCallback((width: number) => {
return { return {

View File

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

View File

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

View File

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