-
Notifications
You must be signed in to change notification settings - Fork 85
/
RecipeEdit.vue
877 lines (822 loc) · 25.8 KB
/
RecipeEdit.vue
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
<template>
<div class="wrapper">
<div v-if="isLoading || store.loadingRecipe" class="loading-indicator">
<LoadingIndicator :size="40" :delay="800" />
</div>
<div v-else>
<div class="overlay" :class="{ hidden: !overlayVisible }" />
<EditInputField
v-model="recipe['name']"
:field-type="'text'"
:field-label="t('cookbook', 'Name')"
/>
<EditInputField
v-model="recipe['description']"
:field-type="'markdown'"
:field-label="t('cookbook', 'Description')"
:suggestion-options="allRecipeOptions"
/>
<EditInputField
v-model="recipe['url']"
:field-type="'url'"
:field-label="t('cookbook', 'URL')"
/>
<EditImageField
v-model="recipe['image']"
:field-label="t('cookbook', 'Image')"
/>
<EditTimeField
v-model="prepTime"
:field-label="
t('cookbook', 'Preparation time (hours:minutes:seconds)')
"
/>
<EditTimeField
v-model="cookTime"
:field-label="
t('cookbook', 'Cooking time (hours:minutes:seconds)')
"
/>
<EditTimeField
v-model="totalTime"
:field-label="
t('cookbook', 'Total time (hours:minutes:seconds)')
"
/>
<EditMultiselect
v-model="recipe['recipeCategory']"
:field-label="t('cookbook', 'Category')"
:placeholder="t('cookbook', 'Choose category')"
:options="allCategories"
:taggable="true"
:multiple="false"
:loading="isFetchingCategories"
@tag="addCategory"
/>
<EditMultiselect
v-model="selectedKeywords"
:field-label="t('cookbook', 'Keywords')"
:placeholder="t('cookbook', 'Choose keywords')"
:options="allKeywords"
:taggable="true"
:multiple="true"
:tag-width="60"
:loading="isFetchingKeywords"
@tag="addKeyword"
/>
<EditInputField
:value="String(recipe['recipeYield'])"
:field-type="'number'"
:field-label="t('cookbook', 'Servings')"
:hide="!showRecipeYield"
@input="recipe['recipeYield'] = Number($event)"
>
<NcActions>
<NcActionButton
class="btn-enable-recipe-yield"
:aria-label="
// prettier-ignore
t('cookbook', 'Toggle if the number of servings is present')
"
@click="toggleShowRecipeYield"
>
<template #icon><numeric-icon :size="20" /></template>
</NcActionButton>
</NcActions>
</EditInputField>
<EditMultiselectInputGroup
v-model="recipe['nutrition']"
:field-label="t('cookbook', 'Nutrition Information')"
:options="availableNutritionFields"
:label-select-placeholder="t('cookbook', 'Pick option')"
/>
<EditInputGroup
v-model="recipe['tool']"
:field-name="'tool'"
:field-type="'text'"
:field-label="t('cookbook', 'Tools')"
:create-fields-on-newlines="true"
:suggestion-options="allRecipeOptions"
/>
<EditInputGroup
v-model="recipe['recipeIngredient']"
:field-name="'recipeIngredient'"
:field-type="'text'"
:field-label="t('cookbook', 'Ingredients')"
:create-fields-on-newlines="true"
:suggestion-options="allRecipeOptions"
/>
<EditInputGroup
v-model="recipe['recipeInstructions']"
:field-name="'recipeInstructions'"
:field-type="'textarea'"
:field-label="t('cookbook', 'Instructions')"
:create-fields-on-newlines="true"
:show-step-number="true"
:suggestion-options="allRecipeOptions"
/>
<div class="cookbook-footer">
<button class="button" @click="save()">
<span
:class="
$store.state.savingRecipe
? 'icon-loading-small'
: 'icon-checkmark'
"
></span>
{{ t('cookbook', 'Save') }}
</button>
</div>
</div>
<!-- Recipe editor container -->
</div>
</template>
<script setup>
import {
computed,
getCurrentInstance,
nextTick,
onBeforeUnmount,
onMounted,
ref,
watch,
} from 'vue';
import {
onBeforeRouteLeave,
onBeforeRouteUpdate,
useRoute,
} from 'vue-router/composables';
import { NcActions, NcActionButton } from '@nextcloud/vue';
import api from 'cookbook/js/api-interface';
import helpers from 'cookbook/js/helper';
import NumericIcon from 'icons/Numeric.vue';
import {
showSimpleAlertModal,
showSimpleConfirmModal,
} from 'cookbook/js/modals';
import EditImageField from './FormComponents/EditImageField.vue';
import EditInputField from './FormComponents/EditInputField.vue';
import EditInputGroup from './FormComponents/EditInputGroup.vue';
import EditMultiselect from './FormComponents/EditMultiselect.vue';
import EditMultiselectInputGroup from './FormComponents/EditMultiselectInputGroup.vue';
import EditTimeField from './FormComponents/EditTimeField.vue';
import LoadingIndicator from './Utilities/LoadingIndicator.vue';
import { useStore } from '../store';
import emitter from '../bus';
const log = getCurrentInstance().proxy.$log;
const route = useRoute();
const store = useStore();
// prettier-ignore
const CONFIRM_MSG = t('cookbook', 'You have unsaved changes! Do you still want to leave?');
// ===================
// Props
// ===================
defineProps({
id: {
type: String,
default: '',
},
});
// ===================
// Reactive properties
// ===================
/**
* @type {import('vue').Ref<boolean>}
*/
const isLoading = ref(false);
// Initialize the recipe schema, otherwise v-models in child components may not work
const recipe = ref({
id: 0,
name: '',
description: '',
url: '',
image: '',
prepTime: '',
cookTime: '',
totalTime: '',
recipeCategory: '',
keywords: '',
recipeYield: '',
tool: [],
recipeIngredient: [],
recipeInstructions: [],
nutrition: {},
});
// ==========================
// These are helper variables
/**
* Changes have been made to the initial values of the form
* @type {import('vue').Ref<boolean>}
*/
const formDirty = ref(false);
/**
* the save button has been clicked
* @type {import('vue').Ref<boolean>}
*/
const savingRecipe = ref(false);
const prepTime = ref({ time: [0, 0, 0], paddedTime: '' });
const cookTime = ref({ time: [0, 0, 0], paddedTime: '' });
const totalTime = ref({ time: [0, 0, 0], paddedTime: '' });
const allCategories = ref([]);
/**
* @type {import('vue').Ref<boolean>}
*/
const isFetchingCategories = ref(true);
/**
* @type {import('vue').Ref<boolean>}
*/
const isFetchingKeywords = ref(true);
/**
* @type {import('vue').Ref<Array>}
*/
const allKeywords = ref([]);
/**
* @type {import('vue').Ref<Array>}
*/
const selectedKeywords = ref([]);
/**
* @type {import('vue').Ref<Array>}
*/
const allRecipes = ref([]);
/**
* @type {import('vue').Ref<Array>}
*/
const availableNutritionFields = ref([
{
key: 'calories',
label: t('cookbook', 'Calories'),
// prettier-ignore
placeholder: t('cookbook','E.g.: 450 kcal (amount & unit)'),
},
{
key: 'carbohydrateContent',
label: t('cookbook', 'Carbohydrate content'),
placeholder: t('cookbook', 'E.g.: 2 g (amount & unit)'),
},
{
key: 'cholesterolContent',
label: t('cookbook', 'Cholesterol content'),
placeholder: t('cookbook', 'E.g.: 2 g (amount & unit)'),
},
{
key: 'fatContent',
label: t('cookbook', 'Fat content'),
placeholder: t('cookbook', 'E.g.: 2 g (amount & unit)'),
},
{
key: 'fiberContent',
label: t('cookbook', 'Fiber content'),
placeholder: t('cookbook', 'E.g.: 2 g (amount & unit)'),
},
{
key: 'proteinContent',
label: t('cookbook', 'Protein content'),
placeholder: t('cookbook', 'E.g.: 2 g (amount & unit)'),
},
{
key: 'saturatedFatContent',
label: t('cookbook', 'Saturated-fat content'),
placeholder: t('cookbook', 'E.g.: 2 g (amount & unit)'),
},
{
key: 'servingSize',
label: t('cookbook', 'Serving size'),
// prettier-ignore
placeholder: t('cookbook','Enter serving size (volume or mass)'),
},
{
key: 'sodiumContent',
label: t('cookbook', 'Sodium content'),
placeholder: t('cookbook', 'E.g.: 2 g (amount & unit)'),
},
{
key: 'sugarContent',
label: t('cookbook', 'Sugar content'),
placeholder: t('cookbook', 'E.g.: 2 g (amount & unit)'),
},
{
key: 'transFatContent',
label: t('cookbook', 'Trans-fat content'),
placeholder: t('cookbook', 'E.g.: 2 g (amount & unit)'),
},
{
key: 'unsaturatedFatContent',
label: t('cookbook', 'Unsaturated-fat content'),
placeholder: t('cookbook', 'E.g.: 2 g (amount & unit)'),
},
]);
/**
* @type {import('vue').Ref<boolean>}
*/
const loadingRecipeReferences = ref(true);
/**
* @type {import('vue').Ref<boolean>}
*/
const showRecipeYield = ref(true);
// ===================
// Computed properties
// ===================
const allRecipeOptions = computed(() =>
allRecipes.value.map((r) => ({
recipe_id: r.recipe_id,
title: `${r.recipe_id}: ${r.name}`,
})),
);
const overlayVisible = computed(
() =>
store.state.loadingRecipe ||
store.state.reloadingRecipe ||
(store.state.categoryUpdating &&
store.state.categoryUpdating === recipe.value.recipeCategory),
);
const recipeWithCorrectedYield = computed(() => {
const r = recipe.value;
if (!showRecipeYield.value) {
r.recipeYield = null;
}
return r;
});
// Whether navigation would lose data, etc.
const isNavigationDangerous = computed(
() => !savingRecipe.value && formDirty.value,
);
// ===================
// Watchers
// ===================
watch(
() => prepTime.value,
() => {
recipe.value.prepTime = prepTime.value.paddedTime;
},
{ deep: true },
);
watch(
() => cookTime.value,
() => {
recipe.value.cookTime = cookTime.value.paddedTime;
},
{ deep: true },
);
watch(
() => totalTime.value,
() => {
recipe.value.totalTime = totalTime.value.paddedTime;
},
{ deep: true },
);
watch(
() => selectedKeywords.value,
() => {
// convert keyword array to comma-separated string
recipe.value.keywords = selectedKeywords.value.join();
},
{ deep: true },
);
watch(
() => recipe.value,
() => {
formDirty.value = true;
},
{ deep: true },
);
// ===================
// Methods
// ===================
/**
* Add newly created category and set as selected.
*/
const addCategory = (newCategory) => {
allCategories.value.push(newCategory);
recipe.value.recipeCategory = newCategory;
};
/**
* Add newly created keyword.
*/
const addKeyword = (newKeyword) => {
allKeywords.value.push(newKeyword);
selectedKeywords.value.push(newKeyword);
};
const beforeWindowUnload = (e) => {
// We cannot use our fancy modal here because `beforeunload` does not wait for promises to resolve
// However, we can avoid `window.confirm` by using `e.returnValue`
if (isNavigationDangerous.value) {
// Cancel the window unload event
e.preventDefault();
e.returnValue = CONFIRM_MSG;
}
};
/**
* Fetch and display recipe categories
*/
const fetchCategories = async () => {
try {
const response = await api.categories.getAll();
const json = response.data || [];
allCategories.value = [];
for (let i = 0; i < json.length; i++) {
if (json[i].name !== '*') {
allCategories.value.push(json[i].name);
}
}
isFetchingCategories.value = false;
} catch (e) {
await showSimpleAlertModal(t('cookbook', 'Failed to fetch categories'));
if (e && e instanceof Error) {
throw e;
}
}
};
/**
* Fetch and display recipe keywords
*/
const fetchKeywords = async () => {
try {
const response = await api.keywords.getAll();
const json = response.data || [];
if (json) {
allKeywords.value = [];
for (let i = 0; i < json.length; i++) {
if (json[i].name !== '*') {
allKeywords.value.push(json[i].name);
}
}
}
isFetchingKeywords.value = false;
} catch (e) {
await showSimpleAlertModal(t('cookbook', 'Failed to fetch keywords'));
if (e && e instanceof Error) {
throw e;
}
}
};
const save = async () => {
savingRecipe.value = true;
store.dispatch('setSavingRecipe', { saving: true });
const request = (() => {
if (route.name !== 'recipe-clone' && (route.params.id ?? false)) {
return store.dispatch('updateRecipe', {
recipe: recipeWithCorrectedYield.value,
});
}
return store.dispatch('createRecipe', {
recipe: recipeWithCorrectedYield.value,
});
})();
try {
const response = await request;
helpers.goTo(`/recipe/${response.data}`);
} catch (e) {
if (e.response) {
// Non 2xx state returned
switch (e.response.status) {
case 409:
case 422:
await showSimpleAlertModal(e.response.data.msg);
break;
default:
await showSimpleAlertModal(
// prettier-ignore
t('cookbook','Unknown answer returned from server. See logs.'),
);
log.error(e.response);
}
} else if (e.request) {
await showSimpleAlertModal(
t('cookbook', 'No answer for request was received.'),
);
log.error(e);
} else {
await showSimpleAlertModal(
// prettier-ignore
t('cookbook','Could not start request to save recipe.'),
);
log.error(e);
}
} finally {
store.dispatch('setSavingRecipe', {
saving: false,
});
savingRecipe.value = false;
}
};
const initEmptyRecipe = () => {
prepTime.value = { time: [0, 0, 0], paddedTime: '' };
cookTime.value = { time: [0, 0, 0], paddedTime: '' };
totalTime.value = { time: [0, 0, 0], paddedTime: '' };
// this.nutrition = {}
recipe.value = {
id: 0,
name: '',
description: '',
url: '',
image: '',
prepTime: '',
cookTime: '',
totalTime: '',
recipeCategory: '',
keywords: '',
recipeYield: '',
tool: [],
recipeIngredient: [],
recipeInstructions: [],
nutrition: {},
};
formDirty.value = false;
showRecipeYield.value = true;
};
/**
* Modifies the current recipe's name property to indicate that it is a clone.
* Resets id to prevent overwriting original recipe.
*/
const initClone = () => {
// Reset id to prevent overwriting original
recipe.value.id = 0;
// Update
// eslint-disable-next-line no-template-curly-in-string
recipe.value.name = t('cookbook', 'Clone of {name}', {
name: recipe.value.name,
});
};
const setup = async () => {
fetchCategories();
fetchKeywords();
if (route.params.id) {
// Parse time values
let timeComps = recipe.value.prepTime
? recipe.value.prepTime.match(/PT(\d+?)H(\d+?)M(\d+?)S/)
: null;
prepTime.value = {
time: timeComps
? [timeComps[1], timeComps[2], timeComps[3]]
: [0, 0, 0],
paddedTime: recipe.value.prepTime,
};
timeComps = recipe.value.cookTime
? recipe.value.cookTime.match(/PT(\d+?)H(\d+?)M(\d+?)S/)
: null;
cookTime.value = {
time: timeComps
? [timeComps[1], timeComps[2], timeComps[3]]
: [0, 0, 0],
paddedTime: recipe.value.cookTime,
};
timeComps = recipe.value.totalTime
? recipe.value.totalTime.match(/PT(\d+?)H(\d+?)M(\d+?)S/)
: null;
totalTime.value = {
time: timeComps
? [timeComps[1], timeComps[2], timeComps[3]]
: [0, 0, 0],
paddedTime: recipe.value.totalTime,
};
selectedKeywords.value = recipe.value.keywords
.split(',')
.map((kw) => kw.trim())
// Remove any empty keywords
// If the response from the server is just an empty
// string, split will create an array of a single empty
// string
.filter((kw) => kw !== '');
// fallback if fetching all keywords fails
selectedKeywords.value.forEach((kw) => {
if (!allKeywords.value.includes(kw)) {
allKeywords.value.push(kw);
}
});
// fallback if fetching all categories fails
if (!allCategories.value.includes(recipe.value.recipeCategory)) {
allCategories.value.push(recipe.value.recipeCategory);
}
if (recipe.value.recipeYield === null) {
showRecipeYield.value = false;
} else if (!recipe.value.recipeYield) {
showRecipeYield.value = false;
recipe.value.recipeYield = null;
} else {
showRecipeYield.value = true;
}
// Modify title and update id for clone to indicate difference from original recipe and prevent overwriting
// original recipe
if (route.name === 'recipe-clone') {
initClone();
}
// Always set the active page last!
if (route.name !== 'recipe-clone') {
store.dispatch('setPage', { page: 'edit' });
} else {
store.dispatch('setPage', { page: 'create' });
}
} else {
initEmptyRecipe();
store.dispatch('setPage', { page: 'create' });
}
recipe.value.valueInit = JSON.parse(JSON.stringify(recipe.value));
await nextTick();
formDirty.value = false;
};
const loadRecipeData = async () => {
isLoading.value = true;
if (!store.state.recipe) {
// Make the control row show that a recipe is loading
store.dispatch('setLoadingRecipe', {
recipe: -1,
});
} else if (store.state.recipe.id === parseInt(route.params.id, 10)) {
// Make the control row show that the recipe is reloading
store.dispatch('setReloadingRecipe', {
recipe: route.params.id,
});
}
try {
const response = await api.recipes.get(route.params.id);
store.dispatch('setRecipe', { recipe: response.data });
recipe.value = response.data;
await setup();
} catch {
await showSimpleAlertModal(t('cookbook', 'Loading recipe failed'));
// Disable loading indicator
if (store.state.loadingRecipe) {
store.dispatch('setLoadingRecipe', { recipe: 0 });
} else if (store.state.reloadingRecipe) {
store.dispatch('setReloadingRecipe', {
recipe: 0,
});
}
// Browse to new recipe creation
helpers.goTo('/recipe/create');
} finally {
isLoading.value = false;
}
};
const toggleShowRecipeYield = () => {
showRecipeYield.value = !showRecipeYield.value;
formDirty.value = true;
};
// ===================
// Vue lifecycle
// ===================
onBeforeRouteUpdate((to, from, next) => {
// beforeRouteUpdate is called when the static route stays the same
next();
// Check if we should reload the component content
if (helpers.shouldReloadContent(from.fullPath, to.fullPath)) {
setup();
}
});
/**
* This is one tricky feature of Vue router. If different paths lead to
* the same component (such as '/recipe/create' and '/recipe/xxx/edit
* or /recipe/xxx/edit and /recipe/yyy/edit)', the view will not automatically
* reload. So we have to check for these conditions and reload manually.
* This can also be used to confirm that the user wants to leave the page
* if there are unsaved changes.
*/
onBeforeRouteLeave(async (to, from, next) => {
// beforeRouteLeave is called when the static route changes.
// Cancel the navigation, if the form has unsaved edits and the user did not
// confirm leave. This prevents accidentally losing changes
if (
isNavigationDangerous.value &&
!(await showSimpleConfirmModal(CONFIRM_MSG))
) {
next(false);
} else {
// We have to check if the target component stays the same and reload.
// However, we should not reload if the component changes; otherwise
// reloaded data may overwrite the data loaded at the target component
// which will at the very least result in incorrect breadcrumb path!
next();
}
// Check if we should reload the component content
if (helpers.shouldReloadContent(from.fullPath, to.fullPath)) {
setup();
}
});
onMounted(() => {
log.info('RecipeEdit mounted');
window.addEventListener('beforeunload', beforeWindowUnload);
// Store the initial recipe configuration for possible later use
if (recipe.value.valueInit === null) {
recipe.value.valueInit = recipe.value;
}
// Register save method hook for access from the controls components
// The event hook must first be destroyed to avoid it from firing multiple
// times if the same component is loaded again
emitter.off('saveRecipe');
emitter.on('saveRecipe', () => {
save();
});
// Register data load method hook for access from the controls components
emitter.off('reloadRecipeEdit');
emitter.on('reloadRecipeEdit', () => {
loadRecipeData();
});
emitter.off('categoryRenamed');
emitter.on('categoryRenamed', (val) => {
// Update selectable categories
const idx = allCategories.value.findIndex((c) => c === val[1]);
if (idx >= 0) {
// eslint-disable-next-line prefer-destructuring
allCategories.value[idx] = val[0];
}
// Update selected category if the currently selected was renamed
if (recipe.value.recipeCategory === val[1]) {
// eslint-disable-next-line prefer-destructuring
recipe.value.recipeCategory = val[0];
}
});
savingRecipe.value = false;
// Load data for all recipes to be used in recipe-reference popup suggestions
api.recipes
.getAll()
.then((response) => {
allRecipes.value = response.data;
})
.catch((e) => {
log.error(e);
})
.then(() => {
// finally
loadingRecipeReferences.value = false;
});
});
onBeforeUnmount(() => {
window.removeEventListener('beforeunload', beforeWindowUnload);
});
// Initially load recipe data if necessary
if (route.params.id) {
if (store.state.recipe?.id === route.params.id) {
// Load the recipe from store and make edits to a local copy first
recipe.value = { ...store.state.recipe };
} else {
loadRecipeData();
}
}
setup();
</script>
<script>
export default {
// We can check if the user has browsed from the same recipe's view to this
// edit and save some time by not reloading the recipe data, leading to a
// more seamless experience.
// This assumes that the data has not been changed some other way between
// loading the view component and loading this edit component. If that is
// the case, the user can always manually reload by clicking the breadcrumb.
// beforeRouteEnter(to, from, next) {
// if (helpers.isSameItemInstance(from.fullPath, to.fullPath)) {
// next((vm) => {
// vm.setup()
// })
// } else if (to.params && to.params.id) {
// next((vm) => {
// console.log(vm)
// vm.loadRecipeData()
// })
// } else {
// next((vm) => {
// vm.setup()
// })
// }
// },
name: 'RecipeEdit',
};
</script>
<style scoped>
.wrapper {
width: 100%;
padding: 1rem;
}
.loading-indicator {
display: flex;
justify-content: center;
padding: 3rem 0;
}
.overlay {
position: absolute;
z-index: 1000;
top: 0;
left: 0;
display: block;
width: 100%;
height: 100%;
background-color: var(--color-main-background);
opacity: 0.75;
}
.overlay.hidden {
display: none;
}
.references-popup {
position: fixed;
display: none;
}
.references-popup.visible {
display: block;
}
.cookbook-footer {
margin-top: 3.5em;
text-align: end;
}
.btn-enable-recipe-yield {
vertical-align: bottom;
}
</style>