From 8b5abf016d5e3535a84076d1cebe9ca7e8500e2a Mon Sep 17 00:00:00 2001 From: Georgi 7DIGIT Date: Thu, 12 Sep 2024 23:51:45 +0300 Subject: [PATCH] Add: shell script to create hooks --- create-hook.bash | 90 ++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 90 insertions(+) create mode 100755 create-hook.bash diff --git a/create-hook.bash b/create-hook.bash new file mode 100755 index 0000000..d1e7dd4 --- /dev/null +++ b/create-hook.bash @@ -0,0 +1,90 @@ + #!/bin/bash + +# Clear the screen +clear + +# Read the hook type from the user input +echo -n "Enter hook type (mutation/query): " +read hook_type + +if [ -z "$hook_type" ]; then + echo "Hook type is required" + exit 1 +fi + +# Check whether the hook_type is mutation or query +if [ "$hook_type" != "mutation" ] && [ "$hook_type" != "query" ]; then + echo "Hook type must be mutation or query" + exit 1 +fi + +# Read the hook name from the user input +echo -n "Enter hook name: " +read hook_name + +if [ -z "$hook_name" ]; then + echo "Hook name is required" + exit 1 +fi + +# Read the service name from the user input +echo -n "Enter service name: " +read service_name + +if [ -z "$service_name" ]; then + echo "Service name is required" + exit 1 +fi + +# Check if the hook file already exists +if [ -f "src/hooks/use$hook_name.js" ]; then + echo "Hook already exists. Please choose a different hook name." + exit 1 +fi + +# Create the hook file +touch "src/hooks/use$hook_name.js" + +# Add the hook to the hook file +if [ "$hook_type" == "mutation" ]; then + echo "import { useMutation } from '@tanstack/react-query'; +import { $service_name } from '@USupport-components-library/services'; +import { useError } from './useError'; + + +export const use$hook_name = ( + onSuccess, + onError +) => { + return useMutation({ + mutationFn: async (data) => { + + } , + onSuccess, + onError: (error) => { + const {message: errorMessage} = useError(error) + onError(errorMessage) + } + }); +}; +export default use$hook_name; +" >> "src/hooks/use$hook_name.js" +else + echo "import { useQuery } from '@tanstack/react-query'; +import { $service_name } from '@USupport-components-library/services'; + +export const use$hook_name = () => { + return useQuery({ + queryKey: [\"$hook_name\"], + queryFn: () => {} + }); +}; +export default use$hook_name; + " >> "src/hooks/use$hook_name.js" +fi + +# Add the hook to the hooks index file +echo "export { default as use$hook_name } from './use$hook_name';" >> "src/hooks/index.js" + +# Output to the user's console +echo "Successfully created use$hook_name hook in src/hooks" \ No newline at end of file