From d080b6a663b8c8507c3ff0d69f61f9969ab6254a Mon Sep 17 00:00:00 2001 From: KoalaSat Date: Tue, 23 Jan 2024 11:37:04 +0100 Subject: [PATCH] Fix garage deletion (#1085) --- frontend/src/basic/RobotPage/index.tsx | 2 +- frontend/src/components/BookTable/index.tsx | 151 +++++++++++--------- frontend/src/models/Coordinator.model.ts | 6 +- frontend/src/models/Garage.model.ts | 40 +++--- frontend/src/models/Slot.model.ts | 10 +- 5 files changed, 113 insertions(+), 96 deletions(-) diff --git a/frontend/src/basic/RobotPage/index.tsx b/frontend/src/basic/RobotPage/index.tsx index bed8672a..a58e7c2b 100644 --- a/frontend/src/basic/RobotPage/index.tsx +++ b/frontend/src/basic/RobotPage/index.tsx @@ -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, diff --git a/frontend/src/components/BookTable/index.tsx b/frontend/src/components/BookTable/index.tsx index 42f104ae..e10da1f3 100644 --- a/frontend/src/components/BookTable/index.tsx +++ b/frontend/src/components/BookTable/index.tsx @@ -292,54 +292,60 @@ const BookTable = ({ }; }, []); - const typeObj = useCallback((width: number) => { - return { - field: 'type', - headerName: t('Is'), - width: width * fontSize, - renderCell: (params: any) => { - return ( -
{ - 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')} -
- ); - }, - }; - }, [fav.mode]); + const typeObj = useCallback( + (width: number) => { + return { + field: 'type', + headerName: t('Is'), + width: width * fontSize, + renderCell: (params: any) => { + return ( +
{ + 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')} +
+ ); + }, + }; + }, + [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 ( -
{ - onOrderClicked(params.row.id, params.row.coordinatorShortAlias); - }} - > - {amountToString(amount, params.row.has_range, minAmount, maxAmount) + - (fav.mode === 'swap' ? 'M Sats' : '')} -
- ); - }, - }; - }, [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 ( +
{ + onOrderClicked(params.row.id, params.row.coordinatorShortAlias); + }} + > + {amountToString(amount, params.row.has_range, minAmount, maxAmount) + + (fav.mode === 'swap' ? 'M Sats' : '')} +
+ ); + }, + }; + }, + [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 ( -
{ - onOrderClicked(params.row.id, params.row.coordinatorShortAlias); - }} - > - -
- ); - }, - }; - }, [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 ( +
{ + onOrderClicked(params.row.id, params.row.coordinatorShortAlias); + }} + > + +
+ ); + }, + }; + }, + [fav.mode], + ); const paymentSmallObj = useCallback((width: number) => { return { diff --git a/frontend/src/models/Coordinator.model.ts b/frontend/src/models/Coordinator.model.ts index 3950d69d..376b6e3a 100644 --- a/frontend/src/models/Coordinator.model.ts +++ b/frontend/src/models/Coordinator.model.ts @@ -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, }); diff --git a/frontend/src/models/Garage.model.ts b/frontend/src/models/Garage.model.ts index 14f5b339..3c97d266 100644 --- a/frontend/src/models/Garage.model.ts +++ b/frontend/src/models/Garage.model.ts @@ -59,10 +59,9 @@ class Garage { const rawSlots = JSON.parse(slotsDump); Object.values(rawSlots).forEach((rawSlot: Record) => { 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) => void = ( + createRobot: (token: string, shortAlias: string, attributes: Record) => 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) => 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'); } }; } diff --git a/frontend/src/models/Slot.model.ts b/frontend/src/models/Slot.model.ts index 76eff4e9..c0585fd5 100644 --- a/frontend/src/models/Slot.model.ts +++ b/frontend/src/models/Slot.model.ts @@ -47,10 +47,16 @@ class Slot { return null; }; - upsertRobot = (shortAlias: string, attributes: Record): Robot | null => { - if (this.robots[shortAlias] === undefined) + createRobot = (shortAlias: string, attributes: Record): Robot | null => { + if (this.robots[shortAlias] === undefined) { this.robots[shortAlias] = new Robot(attributes ?? {}); + return this.robots[shortAlias]; + } + return null; + }; + + updateRobot = (shortAlias: string, attributes: Record): Robot | null => { this.robots[shortAlias].update(attributes); if (attributes.lastOrderId !== undefined && attributes.lastOrderId != null) {