Skip to content

Commit

Permalink
[BOT] [Server Side Bot]maryia/bot 1913/Test link bug fixing and impro…
Browse files Browse the repository at this point in the history
…vements (#15719)

* fix: integrate a toggle button which will switch to stop if the bot is running

* feat: make available bot subscription after reloading the page
  • Loading branch information
maryia-matskevich-deriv authored Jun 24, 2024
1 parent cd3c5ef commit d2ed98d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 27 deletions.
36 changes: 28 additions & 8 deletions packages/bot-web-ui/src/pages/server-bot/server-bot-list.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,26 @@ const ServerBotList = () => {
const { is_mobile } = ui;
const DBotStores = useDBotStore();
const {
server_bot: { bot_list, removeBot, startBot, stopBot },
server_bot: { bot_list, removeBot, startBot, stopBot, notifyBot },
} = DBotStores;
const is_bot_filled_list = !!bot_list[0];

React.useEffect(() => {
if (is_bot_filled_list) {
subscribeToBot();
}
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [is_bot_filled_list]);

const subscribeToBot = () => {
if (bot_list && bot_list.length > 0) {
bot_list.forEach((bot: TBotListItem) => {
if (bot.status === 'running') {
notifyBot(bot.bot_id, false);
}
});
}
};

return (
<div className='bot-list__wrapper'>
Expand Down Expand Up @@ -75,13 +93,15 @@ const ServerBotList = () => {
<p>{localize('[ Strategy parameters ]')}</p>
</div>
<div className='bot-list-contract__actions'>
<Button green onClick={() => startBot(bot_id)}>
{localize('Start')}
</Button>
<Button primary onClick={() => stopBot(bot_id)}>
{localize('Stop')}
</Button>
<Button className='bot-list-btn--disabled' disabled>{localize('Pause')}</Button>
{status === 'started' || status === 'running' ? (
<Button primary onClick={() => stopBot(bot_id)}>
{localize('Stop')}
</Button>
) : (
<Button green onClick={() => startBot(bot_id)}>
{localize('Start')}
</Button>
)}
<Button primary onClick={() => removeBot(bot_id)}>
{localize('Remove')}
</Button>
Expand Down
5 changes: 0 additions & 5 deletions packages/bot-web-ui/src/pages/server-bot/server-bot.scss
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,4 @@
display: flex;
gap: 2rem;
}

}
.bot-list-btn--disabled {
pointer-events: none !important;
cursor: none !important;
}
14 changes: 5 additions & 9 deletions packages/bot-web-ui/src/pages/server-bot/server-bot.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,20 @@ import { api_base } from '@deriv/bot-skeleton';
const ServerBot = observer(() => {
const DBotStores = useDBotStore();
const {
server_bot: { getBotList, bot_list, createBot, notifications, setNotifications },
server_bot: { getBotList, bot_list, createBot, notifications, setNotifications, setStatusBot },
} = DBotStores;

const [add_btn_active, setAddBtnActive] = useState(false);
const { client } = useStore();
const { is_virtual } = client;

React.useEffect(() => {
setTimeout(() => getBotList(), 2000);

// eslint-disable-next-line react-hooks/exhaustive-deps
}, [is_virtual]);

React.useEffect(() => {
if (!bot_list[0]) {
getBotList();
setTimeout(() => getBotList(), 2000);
}

// eslint-disable-next-line react-hooks/exhaustive-deps
}, []);
}, [is_virtual]);

const handleMessage = ({ data }) => {
if (data?.msg_type === 'bot_notification' && !data?.error) {
Expand Down Expand Up @@ -64,6 +59,7 @@ const ServerBot = observer(() => {
);
}
if (data.bot_notification.msg_type === 'stop') {
setStatusBot('stopped', data.echo_req.bot_id);
setNotifications(`msg_type: ${data.bot_notification.msg_type} reason: ${bot_notification_msg.reason}`);
}
}
Expand Down
21 changes: 16 additions & 5 deletions packages/bot-web-ui/src/stores/server-bot-store.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ interface IServerBotStore {
notifications: Array<string>;
setNotifications: (notifications: string) => void;
getBotList: () => void;
notifyBot: (bot_id: string, should_change_status: boolean) => void;
createBot: () => void;
removeBot: (bot_id: string) => void;
startBot: (bot_id: string) => void;
Expand All @@ -16,6 +17,7 @@ interface IServerBotStore {
setValueServerBot: (form_data: any) => void;
makeRequest: (req_schema: TRequestSchema) => Promise<TBotResponse>;
setFormValues: (currency: string) => void;
setStatusBot: (status: string, bot_id: string) => void;
}

type TBotListResponse = {
Expand Down Expand Up @@ -64,11 +66,13 @@ export default class ServerBotStore implements IServerBotStore {
setNotifications: action.bound,
setValue: action.bound,
getBotList: action.bound,
notifyBot: action.bound,
createBot: action.bound,
removeBot: action.bound,
startBot: action.bound,
stopBot: action.bound,
setFormValues: action.bound,
setStatusBot: action.bound,
});
}

Expand Down Expand Up @@ -131,7 +135,7 @@ export default class ServerBotStore implements IServerBotStore {
})
.catch((error: Error) => {
/* eslint-disable no-console */
this.setNotifications(error?.error?.message)
this.setNotifications(error?.error?.message);
});
}, 2000);
};
Expand All @@ -150,7 +154,7 @@ export default class ServerBotStore implements IServerBotStore {
.catch((error: Error) => {
/* eslint-disable no-console */
console.error(error);
this.setNotifications(error?.error?.message)
this.setNotifications(error?.error?.message);
})
.then(() => this.getBotList());
};
Expand Down Expand Up @@ -203,14 +207,17 @@ export default class ServerBotStore implements IServerBotStore {
.then(() => this.getBotList());
};

notifyBot = (bot_id: string) => {
notifyBot = (bot_id: string, should_change_status = true) => {
if (should_change_status) {
this.setStatusBot('started', bot_id);
}
this.makeRequest({
bot_notification: 1,
subscribe: 1,
bot_id,
})
.then(data => {
this.setNotifications(data?.bot_notification?.message)
this.setNotifications(data?.bot_notification?.message);
return data;
})
.catch((error: Error) => {
Expand All @@ -222,5 +229,9 @@ export default class ServerBotStore implements IServerBotStore {

setNotifications = (notifications: string) => {
this.notifications.push(notifications);
}
};

setStatusBot = (status: string, bot_id: string) => {
this.bot_list = this.bot_list.map(bot => (bot.bot_id === bot_id ? { ...bot, status } : bot));
};
}

0 comments on commit d2ed98d

Please sign in to comment.