Skip to content

Commit

Permalink
fix: consoldate stores to fix mismatched state
Browse files Browse the repository at this point in the history
  • Loading branch information
hay-kot committed May 30, 2022
1 parent f831791 commit d2a9f7c
Show file tree
Hide file tree
Showing 15 changed files with 348 additions and 350 deletions.
32 changes: 18 additions & 14 deletions frontend/components/Domain/Recipe/RecipeIngredientEditor.vue
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@

<script lang="ts">
import { computed, defineComponent, reactive, ref, toRefs } from "@nuxtjs/composition-api";
import { useFoods, useUnits } from "~/composables/recipes";
import { useFoodStore, useFoodData, useUnitStore, useUnitData } from "~/composables/store";
import { validators } from "~/composables/use-validators";
import { RecipeIngredient } from "~/types/api-types/recipe";
Expand All @@ -136,24 +136,28 @@ export default defineComponent({
setup(props) {
// ==================================================
// Foods
const { foods, workingFoodData, actions: foodActions } = useFoods();
const foodStore = useFoodStore();
const foodData = useFoodData();
const foodSearch = ref("");
async function createAssignFood() {
workingFoodData.name = foodSearch.value;
await foodActions.createOne();
props.value.food = foods.value?.find((food) => food.name === foodSearch.value);
foodData.data.name = foodSearch.value;
await foodStore.actions.createOne(foodData.data);
props.value.food = foodStore.foods.value?.find((food) => food.name === foodSearch.value);
foodData.reset();
}
// ==================================================
// Units
const { units, workingUnitData, actions: unitActions } = useUnits();
const unitStore = useUnitStore();
const unitsData = useUnitData();
const unitSearch = ref("");
async function createAssignUnit() {
workingUnitData.name = unitSearch.value;
await unitActions.createOne();
props.value.unit = units.value?.find((unit) => unit.name === unitSearch.value);
unitsData.data.name = unitSearch.value;
await unitStore.actions.createOne(unitsData.data);
props.value.unit = unitStore.units.value?.find((unit) => unit.name === unitSearch.value);
unitsData.reset();
}
const state = reactive({
Expand Down Expand Up @@ -226,22 +230,22 @@ export default defineComponent({
}
return {
...toRefs(state),
quantityFilter,
toggleOriginalText,
contextMenuOptions,
handleUnitEnter,
handleFoodEnter,
...toRefs(state),
createAssignFood,
createAssignUnit,
foods,
foods: foodStore.foods,
foodSearch,
toggleTitle,
unitActions,
units,
unitActions: unitStore.actions,
units: unitStore.units,
unitSearch,
validators,
workingUnitData,
workingUnitData: unitsData.data,
};
},
});
Expand Down
8 changes: 2 additions & 6 deletions frontend/components/Domain/Recipe/RecipePrintView.vue
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,12 @@
<div class="ingredient-grid">
<div class="ingredient-col-1">
<ul>
<li v-for="(text, index) in splitIngredients.firstHalf" :key="index">
{{ text }}
</li>
<li v-for="(text, index) in splitIngredients.firstHalf" :key="index" v-html="text" />
</ul>
</div>
<div class="ingredient-col-2">
<ul>
<li v-for="(text, index) in splitIngredients.secondHalf" :key="index">
{{ text }}
</li>
<li v-for="(text, index) in splitIngredients.secondHalf" :key="index" v-html="text" />
</ul>
</div>
</div>
Expand Down
90 changes: 90 additions & 0 deletions frontend/composables/partials/use-actions-factory.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
import { Ref, useAsync } from "@nuxtjs/composition-api";
import { useAsyncKey } from "../use-utils";
import { BaseCRUDAPI } from "~/api/_base";

type BoundT = {
id: string | number;
};

interface StoreActions<T extends BoundT> {
getAll(): Ref<T[] | null>;
refresh(): Promise<void>;
createOne(createData: T): Promise<void>;
updateOne(updateData: T): Promise<void>;
deleteOne(id: string | number): Promise<void>;
}

/**
* useStoreActions is a factory function that returns a set of methods
* that can be reused to manage the state of a data store without using
* Vuex. This is primarily used for basic CRUD operations that required
* a lot of refreshing hooks to be called on operations
*/
export function useStoreActions<T extends BoundT>(
api: BaseCRUDAPI<unknown, T, unknown>,
allRef: Ref<T[] | null> | null,
loading: Ref<boolean>
): StoreActions<T> {
function getAll() {
loading.value = true;
const allItems = useAsync(async () => {
const { data } = await api.getAll();
return data;
}, useAsyncKey());

loading.value = false;
return allItems;
}

async function refresh() {
loading.value = true;
const { data } = await api.getAll();

if (data && allRef) {
allRef.value = data;
}

loading.value = false;
}

async function createOne(createData: T) {
loading.value = true;
const { data } = await api.createOne(createData);
if (data && allRef?.value) {
allRef.value.push(data);
} else {
refresh();
}
loading.value = false;
}

async function updateOne(updateData: T) {
if (!updateData.id) {
return;
}

loading.value = true;
const { data } = await api.updateOne(updateData.id, updateData);
if (data && allRef?.value) {
refresh();
}
loading.value = false;
}

async function deleteOne(id: string | number) {
loading.value = true;
const { data } = await api.deleteOne(id);
if (data && allRef?.value) {
refresh();
}
loading.value = false;
}

return {
getAll,
refresh,
createOne,
updateOne,
deleteOne,
};
}
2 changes: 0 additions & 2 deletions frontend/composables/recipes/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
export { useFraction } from "./use-fraction";
export { useRecipe } from "./use-recipe";
export { useFoods } from "./use-recipe-foods";
export { useUnits } from "./use-recipe-units";
export { useRecipes, recentRecipes, allRecipes, useLazyRecipes, useSorter } from "./use-recipes";
export { useTags, useCategories, allCategories, allTags } from "./use-tags-categories";
export { parseIngredientText } from "./use-recipe-ingredients";
Expand Down
104 changes: 0 additions & 104 deletions frontend/composables/recipes/use-recipe-foods.ts

This file was deleted.

Loading

0 comments on commit d2a9f7c

Please sign in to comment.