moved generic multiselect create function into component
This commit is contained in:
parent
7befa4a084
commit
3159868ba4
@ -600,11 +600,11 @@ class IngredientViewSet(viewsets.ModelViewSet):
|
|||||||
def get_queryset(self):
|
def get_queryset(self):
|
||||||
queryset = self.queryset.filter(step__recipe__space=self.request.space)
|
queryset = self.queryset.filter(step__recipe__space=self.request.space)
|
||||||
food = self.request.query_params.get('food', None)
|
food = self.request.query_params.get('food', None)
|
||||||
if food and re.match(r'^([1-9])+$', food):
|
if food and re.match(r'^(\d)+$', food):
|
||||||
queryset = queryset.filter(food_id=food)
|
queryset = queryset.filter(food_id=food)
|
||||||
|
|
||||||
unit = self.request.query_params.get('unit', None)
|
unit = self.request.query_params.get('unit', None)
|
||||||
if unit and re.match(r'^([1-9])+$', unit):
|
if unit and re.match(r'^(\d)+$', unit):
|
||||||
queryset = queryset.filter(unit_id=unit)
|
queryset = queryset.filter(unit_id=unit)
|
||||||
|
|
||||||
return queryset
|
return queryset
|
||||||
|
@ -29,16 +29,20 @@
|
|||||||
</td>
|
</td>
|
||||||
<td style="width: 30vw">
|
<td style="width: 30vw">
|
||||||
<generic-multiselect @change="i.unit = $event.val;"
|
<generic-multiselect @change="i.unit = $event.val;"
|
||||||
:initial_selection="i.unit"
|
:initial_single_selection="i.unit"
|
||||||
:model="Models.UNIT"
|
:model="Models.UNIT"
|
||||||
:search_on_load="false"
|
:search_on_load="false"
|
||||||
|
:allow_create="true"
|
||||||
|
:create_placeholder="$t('Create')"
|
||||||
:multiple="false"></generic-multiselect>
|
:multiple="false"></generic-multiselect>
|
||||||
</td>
|
</td>
|
||||||
<td style="width: 30vw">
|
<td style="width: 30vw">
|
||||||
<generic-multiselect @change="i.food = $event.val;"
|
<generic-multiselect @change="i.food = $event.val;"
|
||||||
:initial_selection="i.food"
|
:initial_single_selection="i.food"
|
||||||
:model="Models.FOOD"
|
:model="Models.FOOD"
|
||||||
:search_on_load="false"
|
:search_on_load="false"
|
||||||
|
:allow_create="true"
|
||||||
|
:create_placeholder="$t('Create')"
|
||||||
:multiple="false"></generic-multiselect>
|
:multiple="false"></generic-multiselect>
|
||||||
</td>
|
</td>
|
||||||
<td style="width: 30vw">
|
<td style="width: 30vw">
|
||||||
@ -108,7 +112,7 @@ export default {
|
|||||||
}).catch((r, e) => {
|
}).catch((r, e) => {
|
||||||
StandardToasts.makeStandardToast(StandardToasts.FAIL_UPDATE)
|
StandardToasts.makeStandardToast(StandardToasts.FAIL_UPDATE)
|
||||||
})
|
})
|
||||||
}
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
</script>
|
</script>
|
||||||
|
@ -440,7 +440,6 @@
|
|||||||
:initial_selection="settings.shopping_share"
|
:initial_selection="settings.shopping_share"
|
||||||
label="username"
|
label="username"
|
||||||
:multiple="true"
|
:multiple="true"
|
||||||
:allow_create="false"
|
|
||||||
style="flex-grow: 1; flex-shrink: 1; flex-basis: 0"
|
style="flex-grow: 1; flex-shrink: 1; flex-basis: 0"
|
||||||
:placeholder="$t('User')"
|
:placeholder="$t('User')"
|
||||||
/>
|
/>
|
||||||
|
@ -19,6 +19,7 @@
|
|||||||
@search-change="search"
|
@search-change="search"
|
||||||
@input="selectionChanged"
|
@input="selectionChanged"
|
||||||
@tag="addNew"
|
@tag="addNew"
|
||||||
|
@open="selectOpened()"
|
||||||
>
|
>
|
||||||
</multiselect>
|
</multiselect>
|
||||||
</template>
|
</template>
|
||||||
@ -26,7 +27,7 @@
|
|||||||
<script>
|
<script>
|
||||||
import Vue from "vue"
|
import Vue from "vue"
|
||||||
import Multiselect from "vue-multiselect"
|
import Multiselect from "vue-multiselect"
|
||||||
import {ApiMixin} from "@/utils/utils"
|
import {ApiMixin, StandardToasts} from "@/utils/utils"
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
name: "GenericMultiselect",
|
name: "GenericMultiselect",
|
||||||
@ -173,15 +174,30 @@ export default {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
},
|
},
|
||||||
|
selectOpened: function () {
|
||||||
|
if (this.objects.length < 1) {
|
||||||
|
this.search("")
|
||||||
|
}
|
||||||
|
},
|
||||||
selectionChanged: function () {
|
selectionChanged: function () {
|
||||||
this.$emit("change", {var: this.parent_variable, val: this.selected_objects})
|
this.$emit("change", {var: this.parent_variable, val: this.selected_objects})
|
||||||
},
|
},
|
||||||
addNew(e) {
|
addNew(e) {
|
||||||
this.$emit("new", e)
|
//TODO add ability to choose field name other than "name"
|
||||||
// could refactor as Promise - seems unnecessary
|
console.log('CREATEING NEW with -> ' , e)
|
||||||
setTimeout(() => {
|
this.genericAPI(this.model, this.Actions.CREATE, {name: e}).then(result => {
|
||||||
this.search("")
|
let createdObj = result.data?.results ?? result.data
|
||||||
}, 750)
|
StandardToasts.makeStandardToast(StandardToasts.SUCCESS_CREATE)
|
||||||
|
if (this.multiple) {
|
||||||
|
this.selected_objects.push(createdObj)
|
||||||
|
} else {
|
||||||
|
this.selected_objects = createdObj
|
||||||
|
}
|
||||||
|
this.objects.push(createdObj)
|
||||||
|
this.selectionChanged()
|
||||||
|
}).catch((r, err) => {
|
||||||
|
StandardToasts.makeStandardToast(StandardToasts.FAIL_CREATE)
|
||||||
|
})
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
@ -48,7 +48,6 @@
|
|||||||
:initial_single_selection="entryEditing.meal_type"
|
:initial_single_selection="entryEditing.meal_type"
|
||||||
:allow_create="true"
|
:allow_create="true"
|
||||||
:create_placeholder="$t('Create_New_Meal_Type')"
|
:create_placeholder="$t('Create_New_Meal_Type')"
|
||||||
@new="createMealType"
|
|
||||||
></generic-multiselect>
|
></generic-multiselect>
|
||||||
<span class="text-danger" v-if="missing_meal_type">{{ $t("Meal_Type_Required") }}</span>
|
<span class="text-danger" v-if="missing_meal_type">{{ $t("Meal_Type_Required") }}</span>
|
||||||
<small tabindex="-1" class="form-text text-muted" v-if="!missing_meal_type">{{ $t("Meal_Type") }}</small>
|
<small tabindex="-1" class="form-text text-muted" v-if="!missing_meal_type">{{ $t("Meal_Type") }}</small>
|
||||||
@ -228,20 +227,6 @@ export default {
|
|||||||
this.entryEditing.meal_type = null
|
this.entryEditing.meal_type = null
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
createMealType(event) {
|
|
||||||
if (event != "") {
|
|
||||||
let apiClient = new ApiApiFactory()
|
|
||||||
|
|
||||||
apiClient
|
|
||||||
.createMealType({ name: event })
|
|
||||||
.then((e) => {
|
|
||||||
this.$emit("reload-meal-types")
|
|
||||||
})
|
|
||||||
.catch((error) => {
|
|
||||||
StandardToasts.makeStandardToast(StandardToasts.FAIL_UPDATE)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
},
|
|
||||||
selectRecipe(event) {
|
selectRecipe(event) {
|
||||||
this.missing_recipe = false
|
this.missing_recipe = false
|
||||||
if (event.val != null) {
|
if (event.val != null) {
|
||||||
|
@ -18,7 +18,6 @@
|
|||||||
:label="list_label"
|
:label="list_label"
|
||||||
style="flex-grow: 1; flex-shrink: 1; flex-basis: 0"
|
style="flex-grow: 1; flex-shrink: 1; flex-basis: 0"
|
||||||
:placeholder="modelName"
|
:placeholder="modelName"
|
||||||
@new="addNew"
|
|
||||||
>
|
>
|
||||||
</generic-multiselect>
|
</generic-multiselect>
|
||||||
<em v-if="help" class="small text-muted">{{ help }}</em>
|
<em v-if="help" class="small text-muted">{{ help }}</em>
|
||||||
@ -119,19 +118,6 @@ export default {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
methods: {
|
methods: {
|
||||||
addNew: function (e) {
|
|
||||||
// if create a new item requires more than 1 parameter or the field 'name' is insufficient this will need reworked
|
|
||||||
// in a perfect world this would trigger a new modal and allow editing all fields
|
|
||||||
this.genericAPI(this.model, this.Actions.CREATE, { name: e })
|
|
||||||
.then((result) => {
|
|
||||||
this.new_value = result.data
|
|
||||||
StandardToasts.makeStandardToast(StandardToasts.SUCCESS_CREATE)
|
|
||||||
})
|
|
||||||
.catch((err) => {
|
|
||||||
console.log(err)
|
|
||||||
StandardToasts.makeStandardToast(StandardToasts.FAIL_CREATE)
|
|
||||||
})
|
|
||||||
},
|
|
||||||
// ordered lookups have nested attributes that need flattened attributes to drive lookup
|
// ordered lookups have nested attributes that need flattened attributes to drive lookup
|
||||||
flattenItems: function (itemlist) {
|
flattenItems: function (itemlist) {
|
||||||
let flat_items = []
|
let flat_items = []
|
||||||
|
@ -612,6 +612,18 @@ export class Models {
|
|||||||
list: {
|
list: {
|
||||||
params: ["filter_list"],
|
params: ["filter_list"],
|
||||||
},
|
},
|
||||||
|
create: {
|
||||||
|
params: [["name",]],
|
||||||
|
form: {
|
||||||
|
name: {
|
||||||
|
form_field: true,
|
||||||
|
type: "text",
|
||||||
|
field: "name",
|
||||||
|
label: "Name",
|
||||||
|
placeholder: "",
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
static MEAL_PLAN = {
|
static MEAL_PLAN = {
|
||||||
|
Loading…
Reference in New Issue
Block a user