Skip to content

Commit

Permalink
create uses and paths at same time
Browse files Browse the repository at this point in the history
  • Loading branch information
3vorp committed Apr 18, 2024
1 parent bc1ed4f commit 524a93f
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 59 deletions.
14 changes: 13 additions & 1 deletion pages/texture/path-modal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,11 @@ export default {
required: false,
default: false,
},
first: {
type: Boolean,
required: false,
default: false,
},
data: {
type: Object,
required: true,
Expand All @@ -95,7 +100,7 @@ export default {
},
useID: {
type: String,
required: true,
required: false,
},
color: {
type: String,
Expand Down Expand Up @@ -166,6 +171,13 @@ export default {
versions: this.formData.versions.sort(this.MinecraftSorter), // ordered minecraft versions
};
// all use/path info is added in one big request on creation so we "beam" it back
if (this.first) {
delete data.use;
this.$emit("pathAdded", data);
return this.disableDialog();
}
let method = "put";
let pathId = "";
if (this.add) {
Expand Down
15 changes: 3 additions & 12 deletions pages/texture/texture-modal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@
:disableDialog="closeUseModal"
:add="useModalAdd"
:textureID="formData.id"
:usesLength="Object.keys(formData.uses).length"
:data="useModalData"
/>
<texture-remove-confirm
Expand Down Expand Up @@ -202,20 +201,12 @@ export default {
openUseModal(data, add) {
this.useModalOpen = true;
this.useModalAdd = add;
if (add) {
const textureID = String(this.formData.id);
const useIDs = Object.keys(this.formData.uses);
const useLetters = useIDs.map((uid) => uid.replace(textureID, "")[0]);
const maxLetter = useLetters.reduce((acc, cur) => (acc < cur ? cur : acc), "a");
const nextLetter = String.fromCharCode(maxLetter.charCodeAt(0) + 1);
const nextID = textureID + nextLetter;
// Autofill use id
this.useModalData = { id: nextID };
} else this.useModalData = data;
this.useModalData = data || { texture: this.formData.id };
},
closeUseModal() {
this.useModalOpen = false;
// fix for previous modal's paths showing up in blank one
this.useModalData = {};
this.getUses(this.formData.id);
this.$forceUpdate();
},
Expand Down
86 changes: 42 additions & 44 deletions pages/texture/use-modal.vue
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
v-model="pathModalOpen"
:disableDialog="closePathModal"
:add="Object.keys(pathModalData).length == 0"
:first="add"
:useID="formData.id"
:data="pathModalData"
@pathAdded="pathAdded"
/>
<texture-remove-confirm
type="path"
Expand All @@ -26,6 +28,7 @@
/>
<v-text-field
:color="color"
v-if="add == false"
required
persistent-hint
:hint="'⚠️ ' + $root.lang().database.hints.use_id"
Expand All @@ -51,10 +54,7 @@
:label="$root.lang().database.labels.use_edition"
/>
<h2 class="title">{{ $root.lang().database.subtitles.paths }}</h2>
<p v-if="add" align="center" style="color: red">
⚠️<br /><strong>{{ $root.lang().database.hints.warning_path }}</strong>
</p>
<v-list v-if="Object.keys(formData.paths).length && add == false" label="Texture Paths">
<v-list v-if="Object.keys(formData.paths).length" label="Texture Paths">
<v-list-item
class="list-item-inline"
v-for="(path, index) in formData.paths"
Expand All @@ -68,29 +68,21 @@
</v-list-item-content>

<v-list-item-action class="merged">
<v-btn icon @click="openPathModal(path)">
<v-btn icon @click="openPathModal(path, index)">
<v-icon color="lighten-1">mdi-pencil</v-icon>
</v-btn>
<v-btn icon @click="askRemovePath(path)">
<v-btn icon @click="askRemovePath(path, index)">
<v-icon color="red lighten-1">mdi-delete</v-icon>
</v-btn>
</v-list-item-action>
</v-list-item>
</v-list>

<div v-else>
<template v-if="add == false">{{
$root.lang().database.labels.no_path_found
}}</template>
{{ $root.lang().database.labels.no_path_found }}
</div>

<v-btn
block
:disabled="add"
:style="{ 'margin-top': '10px' }"
color="secondary"
@click="openPathModal()"
>
<v-btn block :style="{ 'margin-top': '10px' }" color="secondary" @click="openPathModal()">
{{ $root.lang().database.labels.add_new_path }} <v-icon right>mdi-plus</v-icon>
</v-btn>
</v-form>
Expand Down Expand Up @@ -149,10 +141,6 @@ export default {
type: String,
required: true,
},
usesLength: {
type: Number,
required: true,
},
color: {
type: String,
required: false,
Expand All @@ -168,13 +156,14 @@ export default {
id: "",
texture: "",
name: "",
paths: {},
paths: [],
},
pathModalOpen: false,
pathModalData: {},
remove: {
confirm: false,
data: {},
index: null,
},
};
},
Expand All @@ -186,16 +175,28 @@ export default {
},
},
methods: {
openPathModal(data = {}) {
openPathModal(data = {}, index = null) {
this.pathModalOpen = true;
this.pathModalData = data;
if (index !== null) this.$delete(this.formData.paths, index);
},
closePathModal() {
this.pathModalOpen = false;
this.getPaths(this.formData.id);
this.$forceUpdate();
},
pathAdded(data) {
// won't trigger update otherwise
this.$set(this.formData.paths, this.formData.paths.length, data);
},
closeAndUpdate() {
if (this.add && this.remove.index !== null) {
this.$delete(this.formData.paths, this.remove.index);
this.remove.index = null;
this.remove.confirm = false;
return;
}
this.remove.confirm = false;
this.getPaths(this.formData.id);
this.$forceUpdate();
Expand Down Expand Up @@ -230,17 +231,13 @@ export default {
edition: formData.edition,
};
let method = "put";
let useId = "";
if (this.add) {
data.id = formData.id;
data.texture = Number.parseInt(this.$props.textureID, 10);
method = "post";
} else {
useId = formData.id;
}
if (this.add) data.paths = this.formData.paths || [];
const requestPromise = this.add
? axios.post(`${this.$root.apiURL}/uses/${formData.texture}`, data, this.$root.apiOptions)
: axios.put(`${this.$root.apiURL}/uses/${formData.id}`, data, this.$root.apiOptions);
axios[method](`${this.$root.apiURL}/uses/${useId}`, data, this.$root.apiOptions)
requestPromise
.then(() => {
this.$root.showSnackBar(this.$root.lang().global.ends_success, "success");
this.disableDialog(true);
Expand All @@ -251,27 +248,28 @@ export default {
});
},
getPaths(useId) {
if (!useId) return;
axios
.get(`${this.$root.apiURL}/uses/${useId}/paths`, this.$root.apiOptions)
.then((res) => {
const temp = res.data;
this.formData.paths = {};
const paths = res.data;
for (let i = 0; i < temp.length; i++) {
temp[i].versions.sort(this.MinecraftSorter);
this.formData.paths[temp[i].id] = {
...temp[i],
use: temp[i].use || useId,
};
}
// reset path data
this.formData.paths = paths.map((p) => ({
...p,
use: p.use || useId,
versions: p.versions.sort(this.MinecraftSorter),
}));
})
.catch((err) => {
console.error(err);
});
},
askRemovePath(data) {
askRemovePath(data, index) {
this.remove.data = data;
this.remove.confirm = true;
this.remove.index = index;
},
},
watch: {
Expand All @@ -282,8 +280,8 @@ export default {
this.$nextTick(() => {
if (this.add) {
this.$refs.form.reset();
if ("id" in this.data) this.formData.id = this.data.id;
this.formData.paths = {};
this.formData.texture = this.data.texture;
this.formData.paths = [];
} else {
this.formData.edition = this.data.edition;
this.formData.id = this.data.id;
Expand Down
2 changes: 0 additions & 2 deletions resources/strings/en_US.js
Original file line number Diff line number Diff line change
Expand Up @@ -237,8 +237,6 @@ export default {
use_id: "Changing the use ID can break everything!",
path_id: "Changing the path ID can break everything!",
path_prefill: "Enter this first to generate most fields!",
warning_path:
"A use needs to be created before adding paths to it (currently not well supported and a bit buggy). Creating a use and path at the same time is planned.",
path: "The path should start from the root directory (ex: assets/…)",
example_scenario:
"Changes all instances of a Minecraft version in the database to a different one. (ex. 1.17 → 1.17.1)",
Expand Down

0 comments on commit 524a93f

Please sign in to comment.