-
-
Notifications
You must be signed in to change notification settings - Fork 782
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: boc-the-git <[email protected]> Co-authored-by: Kuchenpirat <[email protected]>
- Loading branch information
1 parent
ee87a14
commit 3807778
Showing
22 changed files
with
860 additions
and
10 deletions.
There are no files selected for viewing
52 changes: 52 additions & 0 deletions
52
alembic/versions/2024-04-07-01.05.20_7788478a0338_add_group_recipe_actions.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
"""add group recipe actions | ||
Revision ID: 7788478a0338 | ||
Revises: d7c6efd2de42 | ||
Create Date: 2024-04-07 01:05:20.816270 | ||
""" | ||
|
||
import sqlalchemy as sa | ||
|
||
import mealie.db.migration_types | ||
from alembic import op | ||
|
||
# revision identifiers, used by Alembic. | ||
revision = "7788478a0338" | ||
down_revision = "d7c6efd2de42" | ||
branch_labels = None | ||
depends_on = None | ||
|
||
|
||
def upgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.create_table( | ||
"recipe_actions", | ||
sa.Column("id", mealie.db.migration_types.GUID(), nullable=False), | ||
sa.Column("group_id", mealie.db.migration_types.GUID(), nullable=False), | ||
sa.Column("action_type", sa.String(), nullable=False), | ||
sa.Column("title", sa.String(), nullable=False), | ||
sa.Column("url", sa.String(), nullable=False), | ||
sa.Column("created_at", sa.DateTime(), nullable=True), | ||
sa.Column("update_at", sa.DateTime(), nullable=True), | ||
sa.ForeignKeyConstraint( | ||
["group_id"], | ||
["groups.id"], | ||
), | ||
sa.PrimaryKeyConstraint("id"), | ||
) | ||
op.create_index(op.f("ix_recipe_actions_action_type"), "recipe_actions", ["action_type"], unique=False) | ||
op.create_index(op.f("ix_recipe_actions_created_at"), "recipe_actions", ["created_at"], unique=False) | ||
op.create_index(op.f("ix_recipe_actions_group_id"), "recipe_actions", ["group_id"], unique=False) | ||
op.create_index(op.f("ix_recipe_actions_title"), "recipe_actions", ["title"], unique=False) | ||
# ### end Alembic commands ### | ||
|
||
|
||
def downgrade(): | ||
# ### commands auto generated by Alembic - please adjust! ### | ||
op.drop_index(op.f("ix_recipe_actions_title"), table_name="recipe_actions") | ||
op.drop_index(op.f("ix_recipe_actions_group_id"), table_name="recipe_actions") | ||
op.drop_index(op.f("ix_recipe_actions_created_at"), table_name="recipe_actions") | ||
op.drop_index(op.f("ix_recipe_actions_action_type"), table_name="recipe_actions") | ||
op.drop_table("recipe_actions") | ||
# ### end Alembic commands ### |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,98 @@ | ||
import { computed, reactive, ref } from "@nuxtjs/composition-api"; | ||
import { useStoreActions } from "./partials/use-actions-factory"; | ||
import { useUserApi } from "~/composables/api"; | ||
import { GroupRecipeActionOut, RecipeActionType } from "~/lib/api/types/group"; | ||
import { Recipe } from "~/lib/api/types/recipe"; | ||
|
||
const groupRecipeActions = ref<GroupRecipeActionOut[] | null>(null); | ||
const loading = ref(false); | ||
|
||
export function useGroupRecipeActionData() { | ||
const data = reactive({ | ||
id: "", | ||
actionType: "link" as RecipeActionType, | ||
title: "", | ||
url: "", | ||
}); | ||
|
||
function reset() { | ||
data.id = ""; | ||
data.actionType = "link"; | ||
data.title = ""; | ||
data.url = ""; | ||
} | ||
|
||
return { | ||
data, | ||
reset, | ||
}; | ||
} | ||
|
||
export const useGroupRecipeActions = function ( | ||
orderBy: string | null = "title", | ||
orderDirection: string | null = "asc", | ||
) { | ||
const api = useUserApi(); | ||
|
||
async function refreshGroupRecipeActions() { | ||
loading.value = true; | ||
const { data } = await api.groupRecipeActions.getAll(1, -1, { orderBy, orderDirection }); | ||
groupRecipeActions.value = data?.items || null; | ||
loading.value = false; | ||
} | ||
|
||
const recipeActions = computed<GroupRecipeActionOut[] | null>(() => { | ||
return groupRecipeActions.value; | ||
}); | ||
|
||
function parseRecipeActionUrl(url: string, recipe: Recipe): string { | ||
/* eslint-disable no-template-curly-in-string */ | ||
return url | ||
.replace("${url}", window.location.href) | ||
.replace("${id}", recipe.id || "") | ||
.replace("${slug}", recipe.slug || "") | ||
/* eslint-enable no-template-curly-in-string */ | ||
}; | ||
|
||
async function execute(action: GroupRecipeActionOut, recipe: Recipe): Promise<void | Response> { | ||
const url = parseRecipeActionUrl(action.url, recipe); | ||
|
||
switch (action.actionType) { | ||
case "link": | ||
window.open(url, "_blank")?.focus(); | ||
break; | ||
case "post": | ||
return await fetch(url, { | ||
method: "POST", | ||
headers: { | ||
// The "text/plain" content type header is used here to skip the CORS preflight request, | ||
// since it may fail. This is fine, since we don't care about the response, we just want | ||
// the request to get sent. | ||
"Content-Type": "text/plain", | ||
}, | ||
body: JSON.stringify(recipe), | ||
}).catch((error) => { | ||
console.error(error); | ||
}); | ||
default: | ||
break; | ||
} | ||
}; | ||
|
||
if (!groupRecipeActions.value && !loading.value) { | ||
refreshGroupRecipeActions(); | ||
}; | ||
|
||
const actions = { | ||
...useStoreActions<GroupRecipeActionOut>(api.groupRecipeActions, groupRecipeActions, loading), | ||
flushStore() { | ||
groupRecipeActions.value = []; | ||
} | ||
} | ||
|
||
return { | ||
actions, | ||
execute, | ||
recipeActions, | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.