From fe6f78fdb0cdd44deee72fe53aeac6d66414cc10 Mon Sep 17 00:00:00 2001 From: Lemuel Date: Thu, 10 Mar 2022 15:57:06 +0800 Subject: [PATCH] feat: rename bot --- src/components/BotEntry.vue | 19 ++++++---- src/components/BotList.vue | 36 ++++++++++++++++-- src/components/BotRename.vue | 57 ++++++++++++++++++++++++++++ src/shared/userService.ts | 6 +++ src/store/modules/botStoreWrapper.ts | 22 ++++++++++- src/store/modules/ftbot/index.ts | 4 ++ src/types/auth.ts | 5 +++ 7 files changed, 136 insertions(+), 13 deletions(-) create mode 100644 src/components/BotRename.vue diff --git a/src/components/BotEntry.vue b/src/components/BotEntry.vue index e4bd004d..b62210c0 100644 --- a/src/components/BotEntry.vue +++ b/src/components/BotEntry.vue @@ -16,9 +16,10 @@ R
- + + + + @@ -31,15 +32,19 @@ import { Component, Prop, Vue } from 'vue-property-decorator'; import { namespace } from 'vuex-class'; import { MultiBotStoreGetters } from '@/store/modules/botStoreWrapper'; -import LoginModal from '@/views/LoginModal.vue'; -import EditIcon from 'vue-material-design-icons/Cog.vue'; +import EditIcon from 'vue-material-design-icons/Pencil.vue'; import DeleteIcon from 'vue-material-design-icons/Delete.vue'; import { BotDescriptor, BotDescriptors } from '@/types'; import StoreModules from '@/store/storeSubModules'; const ftbot = namespace(StoreModules.ftbot); -@Component({ components: { LoginModal, DeleteIcon, EditIcon } }) +@Component({ + components: { + DeleteIcon, + EditIcon, + }, +}) export default class BotList extends Vue { @Prop({ default: false, type: Object }) bot!: BotDescriptor; @@ -79,5 +84,3 @@ export default class BotList extends Vue { } } - - diff --git a/src/components/BotList.vue b/src/components/BotList.vue index 6e7c9617..c57acf87 100644 --- a/src/components/BotList.vue +++ b/src/components/BotList.vue @@ -10,7 +10,14 @@ :title="`${bot.botId} - ${bot.botName} - ${bot.botUrl}`" @click="selectBot(bot.botId)" > - + + + @@ -23,12 +30,19 @@ import { namespace } from 'vuex-class'; import { MultiBotStoreGetters } from '@/store/modules/botStoreWrapper'; import LoginModal from '@/views/LoginModal.vue'; import BotEntry from '@/components/BotEntry.vue'; +import BotRename from '@/components/BotRename.vue'; import { BotDescriptors } from '@/types'; import StoreModules from '@/store/storeSubModules'; const ftbot = namespace(StoreModules.ftbot); -@Component({ components: { LoginModal, BotEntry } }) +@Component({ + components: { + LoginModal, + BotEntry, + BotRename, + }, +}) export default class BotList extends Vue { @Prop({ default: false, type: Boolean }) small!: boolean; @@ -41,7 +55,21 @@ export default class BotList extends Vue { @ftbot.Getter [MultiBotStoreGetters.allAvailableBots]: BotDescriptors; @ftbot.Action selectBot; + + editingBots: string[] = []; + + editBot(botId: string) { + if (!this.editingBots.includes(botId)) { + this.editingBots.push(botId); + } + } + + stopEditBot(botId: string) { + if (!this.editingBots.includes(botId)) { + return; + } + + this.editingBots.splice(this.editingBots.indexOf(botId), 1); + } } - - diff --git a/src/components/BotRename.vue b/src/components/BotRename.vue new file mode 100644 index 00000000..a9b293ee --- /dev/null +++ b/src/components/BotRename.vue @@ -0,0 +1,57 @@ + + + diff --git a/src/shared/userService.ts b/src/shared/userService.ts index dfd3804c..11a86e4c 100644 --- a/src/shared/userService.ts +++ b/src/shared/userService.ts @@ -13,6 +13,12 @@ export class UserService { this.botId = botId; } + public renameBot(newName: string): void { + const newInfo = this.getLoginInfo(); + newInfo.botName = newName; + this.storeLoginInfo(newInfo); + } + /** * Stores info for current botId in the object of all bots. */ diff --git a/src/store/modules/botStoreWrapper.ts b/src/store/modules/botStoreWrapper.ts index 17895a63..0c3d0b24 100644 --- a/src/store/modules/botStoreWrapper.ts +++ b/src/store/modules/botStoreWrapper.ts @@ -6,6 +6,7 @@ import { DailyReturnValue, MultiDeletePayload, MultiForcesellPayload, + RenameBotPayload, Trade, } from '@/types'; import { AxiosInstance } from 'axios'; @@ -182,7 +183,15 @@ export default function createBotStore(store) { state.refreshing = refreshing; }, addBot(state: FTMultiBotState, bot: BotDescriptor) { - state.availableBots[bot.botId] = bot; + // When Vue gets initialized, only existing objects will be added with reactivity. + // To add reactivity to new property, we need to mutate the already reactive object. + state.availableBots = { + ...state.availableBots, + [bot.botId]: bot, + }; + }, + renameBot(state: FTMultiBotState, bot: RenameBotPayload) { + state.availableBots[bot.botId].botName = bot.botName; }, removeBot(state: FTMultiBotState, botId: string) { if (botId in state.availableBots) { @@ -214,6 +223,17 @@ export default function createBotStore(store) { dispatch(`${bot.botId}/botAdded`); commit('addBot', bot); }, + renameBot({ dispatch, getters, commit }, bot: RenameBotPayload) { + if (!Object.keys(getters.allAvailableBots).includes(bot.botId)) { + // TODO: handle error! + console.error('Bot not found'); + return; + } + + dispatch(`${bot.botId}/rename`, bot.botName).then(() => { + commit('renameBot', bot); + }); + }, removeBot({ commit, getters, dispatch }, botId: string) { if (Object.keys(getters.allAvailableBots).includes(botId)) { dispatch(`${botId}/logout`); diff --git a/src/store/modules/ftbot/index.ts b/src/store/modules/ftbot/index.ts index d15eac2d..3eff960d 100644 --- a/src/store/modules/ftbot/index.ts +++ b/src/store/modules/ftbot/index.ts @@ -155,6 +155,7 @@ export enum BotStoreActions { setBacktestResultKey = 'setBacktestResultKey', sysInfo = 'sysInfo', logout = 'logout', + rename = 'rename', } export function createBotSubStore(botId: string, botName: string) { @@ -488,6 +489,9 @@ export function createBotSubStore(botId: string, botName: string) { [BotStoreActions.logout]() { userService.logout(); }, + [BotStoreActions.rename](ctx, name) { + userService.renameBot(name); + }, [BotStoreActions.setRefreshRequired]({ commit }, refreshRequired: boolean) { commit('updateRefreshRequired', refreshRequired); }, diff --git a/src/types/auth.ts b/src/types/auth.ts index b4d32a03..c05fe9cd 100644 --- a/src/types/auth.ts +++ b/src/types/auth.ts @@ -31,3 +31,8 @@ export interface BotDescriptor { export interface BotDescriptors { [key: string]: BotDescriptor; } + +export interface RenameBotPayload { + botId: string; + botName: string; +}