Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix garage deletion #1085

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion frontend/src/basic/RobotPage/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
151 changes: 80 additions & 71 deletions frontend/src/components/BookTable/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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],
);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think all modifications on this file was caused by the lintern, I'm pretty sure I didn't change anything here


const paymentSmallObj = useCallback((width: number) => {
return {
Expand Down
6 changes: 3 additions & 3 deletions frontend/src/models/Coordinator.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
});

Expand All @@ -400,7 +400,7 @@ export class Coordinator {
{ tokenSHA256: robot.tokenSHA256 },
);

garage.upsertRobot(slot?.token, this.shortAlias, {
garage.updateRobot(slot?.token, this.shortAlias, {
stealthInvoices: wantsStealth,
});

Expand Down
40 changes: 21 additions & 19 deletions frontend/src/models/Garage.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand All @@ -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();
}
};

Expand All @@ -111,23 +102,34 @@ 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;

let slot = this.getSlot(token);

if (slot === null && token) {
slot = this.createSlot(token);
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,
) => {
if (!token || !shortAlias) return;

let slot = this.getSlot(token);

if (slot != null) {
slot.upsertRobot(shortAlias, { token, ...attributes });
this.triggerHook('onRobotUpdate');
slot.updateRobot(shortAlias, { token, ...attributes });
this.save();
this.triggerHook('onRobotUpdate');
}
};

Expand All @@ -149,8 +151,8 @@ class Garage {
} else {
slot.order = null;
}
this.triggerHook('onOrderUpdate');
this.save();
this.triggerHook('onOrderUpdate');
}
};
}
Expand Down
10 changes: 8 additions & 2 deletions frontend/src/models/Slot.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down