Skip to content

Commit bf00da1

Browse files
committed
botconfig
1 parent ff0de8f commit bf00da1

File tree

1 file changed

+185
-125
lines changed

1 file changed

+185
-125
lines changed

src/views/modals/BotConfig.vue

Lines changed: 185 additions & 125 deletions
Original file line numberDiff line numberDiff line change
@@ -17,15 +17,25 @@
1717
<div class="form-item">
1818
<div class="form-item__buttons">
1919
<button class="button button--confirm" @click="onSave">
20-
<FontAwesomeIcon v-if="saving" icon="spinner" spin></FontAwesomeIcon>
21-
<span v-else>{{ $t('save') }}</span>
20+
<FontAwesomeIcon
21+
v-if="saving"
22+
icon="spinner"
23+
spin
24+
></FontAwesomeIcon>
25+
<span v-else>{{ $t("save") }}</span>
2226
</button>
23-
<router-link v-slot="{ navigate }" custom :to="{ name: 'bot-copy', params: { bot: bot.name } }">
24-
<button class="button button--confirm" @click="navigate">{{ $t('bot-copy') }}</button>
27+
<router-link
28+
v-slot="{ navigate }"
29+
custom
30+
:to="{ name: 'bot-copy', params: { bot: bot.name } }"
31+
>
32+
<button class="button button--confirm" @click="navigate">
33+
{{ $t("bot-copy") }}
34+
</button>
2535
</router-link>
2636

2737
<button class="button button--link pull-right" @click="onDownload">
28-
{{ $t('download-raw-config') }}
38+
{{ $t("download-raw-config") }}
2939
</button>
3040
</div>
3141
</div>
@@ -34,142 +44,192 @@
3444
</template>
3545

3646
<script>
37-
import { mapGetters } from 'vuex';
38-
import ConfigEditor from '../../components/Config/Editor.vue';
39-
import fetchConfigSchema from '../../utils/fetchConfigSchema';
40-
import loadParameterDescriptions from '../../utils/loadParameterDescriptions';
41-
import { downloadConfig } from '../../utils/download';
42-
import { botCategories } from '../../utils/configCategories';
43-
import isSameConfig from '../../utils/isSameConfig';
44-
45-
export default {
46-
name: 'BotConfig',
47-
components: { ConfigEditor },
48-
data() {
49-
return {
50-
loading: false,
51-
saving: false,
52-
fields: [],
53-
model: {},
54-
categories: botCategories,
55-
};
56-
},
57-
computed: {
58-
...mapGetters({
59-
version: 'asf/version',
60-
displayCategories: 'settings/displayCategories',
61-
bots: 'bots/bots',
62-
}),
63-
bot() {
64-
return this.$store.getters['bots/bot'](this.$route.params.bot);
65-
},
66-
},
67-
watch: {
68-
$route: {
69-
immediate: true,
70-
async handler() {
71-
if (!this.bot) return;
72-
await this.loadConfig();
73-
},
47+
import { mapGetters } from "vuex";
48+
import ConfigEditor from "../../components/Config/Editor.vue";
49+
import fetchConfigSchema from "../../utils/fetchConfigSchema";
50+
import loadParameterDescriptions from "../../utils/loadParameterDescriptions";
51+
import { downloadConfig } from "../../utils/download";
52+
import { botCategories } from "../../utils/configCategories";
53+
import isSameConfig from "../../utils/isSameConfig";
54+
55+
export default {
56+
name: "BotConfig",
57+
components: { ConfigEditor },
58+
data() {
59+
return {
60+
loading: false,
61+
saving: false,
62+
fields: [],
63+
model: {},
64+
categories: botCategories
65+
};
66+
},
67+
computed: {
68+
...mapGetters({
69+
version: "asf/version",
70+
displayCategories: "settings/displayCategories",
71+
bots: "bots/bots"
72+
}),
73+
bot() {
74+
return this.$store.getters["bots/bot"](this.$route.params.bot);
75+
}
76+
},
77+
watch: {
78+
"bot.viewableName": {
79+
handler() {
80+
this.updateDocumentTitle();
7481
},
82+
immediate: true
7583
},
76-
created() {
77-
if (!this.bot) this.$router.replace({ name: 'bots' });
84+
$route: {
85+
immediate: true,
86+
async handler() {
87+
if (!this.bot) return;
88+
await this.loadConfig();
89+
}
90+
}
91+
},
92+
created() {
93+
if (!this.bot) {
94+
this.$router.replace({ name: "bots" });
95+
} else {
96+
this.updateDocumentTitle();
97+
}
98+
},
99+
mounted() {
100+
this.updateDocumentTitle();
101+
},
102+
methods: {
103+
updateDocumentTitle() {
104+
if (this.bot) {
105+
document.title = `${this.bot.viewableName} - ${this.$t(
106+
"bot-config"
107+
)} - ASF-UI`;
108+
}
78109
},
79-
methods: {
80-
async loadConfig() {
81-
if (this.loading) return;
82-
83-
this.loading = true;
84-
85-
try {
86-
const [
87-
{ body: fields },
88-
{ [this.bot.name]: { BotConfig: model } },
89-
descriptions,
90-
] = await Promise.all([
91-
fetchConfigSchema('ArchiSteamFarm.Steam.Storage.BotConfig'),
92-
this.$http.get(`bot/${this.bot.name}`),
93-
loadParameterDescriptions(this.version, this.$i18n.locale),
94-
]);
95-
96-
Object.keys(model).forEach(key => {
97-
if (key.startsWith('s_')) delete model[key.substr(2)];
98-
});
99-
100-
this.model = model;
101-
102-
// if we got routed to bot-config with params, we propably
103-
// came from PasswordEncrypt.vue and want to set password data from params
104-
if (Object.keys(this.$route.params).length !== 0) {
105-
// only set the values if they exist in the params
106-
if (typeof this.$route.params.steamPassword !== 'undefined') {
107-
this.model.SteamPassword = this.$route.params.steamPassword;
108-
}
109-
if (typeof this.$route.params.passwordFormat !== 'undefined') {
110-
this.model.PasswordFormat = this.$route.params.passwordFormat;
111-
}
110+
async loadConfig() {
111+
if (this.loading) return;
112+
113+
this.loading = true;
114+
115+
try {
116+
const [
117+
{ body: fields },
118+
{
119+
[this.bot.name]: { BotConfig: model }
120+
},
121+
descriptions
122+
] = await Promise.all([
123+
fetchConfigSchema("ArchiSteamFarm.Steam.Storage.BotConfig"),
124+
this.$http.get(`bot/${this.bot.name}`),
125+
loadParameterDescriptions(this.version, this.$i18n.locale)
126+
]);
127+
128+
Object.keys(model).forEach(key => {
129+
if (key.startsWith("s_")) delete model[key.substr(2)];
130+
});
131+
132+
this.model = model;
133+
134+
// if we got routed to bot-config with params, we propably
135+
// came from PasswordEncrypt.vue and want to set password data from params
136+
if (Object.keys(this.$route.params).length !== 0) {
137+
// only set the values if they exist in the params
138+
if (typeof this.$route.params.steamPassword !== "undefined") {
139+
this.model.SteamPassword = this.$route.params.steamPassword;
140+
}
141+
if (typeof this.$route.params.passwordFormat !== "undefined") {
142+
this.model.PasswordFormat = this.$route.params.passwordFormat;
112143
}
144+
}
113145
114-
const extendedFields = {
115-
SteamLogin: { placeholder: this.$t('keep-unchanged') },
116-
SteamPassword: { placeholder: this.$t('keep-unchanged') },
117-
SteamParentalCode: { placeholder: this.$t('keep-unchanged') },
146+
const extendedFields = {
147+
SteamLogin: { placeholder: this.$t("keep-unchanged") },
148+
SteamPassword: { placeholder: this.$t("keep-unchanged") },
149+
SteamParentalCode: { placeholder: this.$t("keep-unchanged") }
150+
};
151+
152+
this.fields = Object.keys(fields).map(key => {
153+
const description = !descriptions[key]
154+
? this.$t("description-not-found")
155+
: descriptions[key].replace(
156+
/<a href="/g,
157+
'<a target="_blank" rel="noreferrer noopener" href="'
158+
);
159+
160+
return {
161+
description,
162+
...fields[key],
163+
...(extendedFields[key] || [])
118164
};
165+
});
166+
167+
// Sort the config properties alphabetically to make them easier to find when not using config categories
168+
if (!this.displayCategories)
169+
this.fields = this.fields.sort((a, b) =>
170+
a.paramName.localeCompare(b.paramName)
171+
);
172+
} catch (err) {
173+
this.$error(err.message);
174+
} finally {
175+
this.loading = false;
176+
}
177+
},
178+
async onSave() {
179+
if (this.saving) return;
119180
120-
this.fields = Object.keys(fields).map(key => {
121-
const description = (!descriptions[key])
122-
? this.$t('description-not-found')
123-
: descriptions[key].replace(/<a href="/g, '<a target="_blank" rel="noreferrer noopener" href="');
181+
if (this.model.Name === "ASF") {
182+
this.$error(this.$t("bot-create-name-asf"));
183+
return;
184+
}
124185
125-
return { description, ...fields[key], ...(extendedFields[key] || []) };
126-
});
186+
this.saving = true;
127187
128-
// Sort the config properties alphabetically to make them easier to find when not using config categories
129-
if (!this.displayCategories) this.fields = this.fields.sort((a, b) => a.paramName.localeCompare(b.paramName));
130-
} catch (err) {
131-
this.$error(err.message);
132-
} finally {
133-
this.loading = false;
134-
}
135-
},
136-
async onSave() {
137-
if (this.saving) return;
188+
try {
189+
// fetch current bot config
190+
const {
191+
[this.bot.name]: { BotConfig: oldConfig }
192+
} = await this.$http.get(`bot/${this.bot.name}`);
138193
139-
if (this.model.Name === 'ASF') {
140-
this.$error(this.$t('bot-create-name-asf'));
194+
// we do not want to save identical config
195+
if (isSameConfig(this.model, oldConfig)) {
196+
this.$info(this.$t("config-no-changes"));
141197
return;
142198
}
143199
144-
this.saving = true;
145-
146-
try {
147-
// fetch current bot config
148-
const { [this.bot.name]: { BotConfig: oldConfig } } = await this.$http.get(`bot/${this.bot.name}`);
149-
150-
// we do not want to save identical config
151-
if (isSameConfig(this.model, oldConfig)) {
152-
this.$info(this.$t('config-no-changes'));
153-
return;
154-
}
155-
156-
await this.$http.post(`bot/${this.bot.name}`, { botConfig: this.model });
157-
this.$parent.back();
158-
} catch (err) {
159-
this.$error(err.message);
160-
} finally {
161-
this.saving = false;
200+
await this.$http.post(`bot/${this.bot.name}`, {
201+
botConfig: this.model
202+
});
203+
// Update the bot name in the store
204+
if (this.model.Name && this.model.Name !== this.bot.name) {
205+
await this.$store.dispatch("bots/updateBot", {
206+
name: this.bot.name,
207+
name: this.model.Name
208+
});
209+
// Update the route to reflect the new bot name
210+
this.$router.replace({
211+
name: "bot-config",
212+
params: { bot: this.model.Name }
213+
});
214+
} else {
215+
await this.$store.dispatch("bots/updateBot", { name: this.bot.name });
162216
}
163-
},
164-
async onDownload() {
165-
downloadConfig(this.model, this.bot.name);
166-
},
217+
this.$parent.back();
218+
} catch (err) {
219+
this.$error(err.message);
220+
} finally {
221+
this.saving = false;
222+
}
167223
},
168-
};
224+
async onDownload() {
225+
downloadConfig(this.model, this.bot.name);
226+
}
227+
}
228+
};
169229
</script>
170230

171231
<style lang="scss">
172-
.main-container--bot-config {
173-
max-width: 1000px;
174-
}
232+
.main-container--bot-config {
233+
max-width: 1000px;
234+
}
175235
</style>

0 commit comments

Comments
 (0)