diff --git a/cmd/api/main.go b/cmd/api/main.go index d9dd1f3..aeda58e 100644 --- a/cmd/api/main.go +++ b/cmd/api/main.go @@ -18,9 +18,10 @@ func main() { e.GET("/demo/style.css", handler.Stylesheet) - e.GET("/v/:ulid", handler.Configuration) + e.GET("/v/:ulid", handler.GetConfiguration) + e.GET("/v/new", handler.GetConfiguration) + e.POST("/v/:ulid", handler.UpdateConfig) - e.GET("/v/new", handler.Configuration) e.POST("/v/new", handler.IntroVideoCode) e.POST("/v/config", handler.CreateConfig) @@ -36,4 +37,3 @@ func main() { e.Logger.Fatal(e.Start(":" + port)) } - diff --git a/db/migrations/1715450001_video.down.sql b/db/migrations/1715450001_video.down.sql deleted file mode 100644 index fe2c03a..0000000 --- a/db/migrations/1715450001_video.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE `videos`; diff --git a/db/migrations/1715450001_video.up.sql b/db/migrations/1715450001_video.up.sql deleted file mode 100644 index 44fdb86..0000000 --- a/db/migrations/1715450001_video.up.sql +++ /dev/null @@ -1,9 +0,0 @@ -CREATE TABLE `videos` ( - `id` INTEGER PRIMARY KEY NOT NULL, - `url` TEXT NOT NULL, - `weight` INTEGER DEFAULT 1, - `configuration_id` INTEGER NOT NULL, - `instance_id` INTEGER NOT NULL, - FOREIGN KEY(`configuration_id`) REFERENCES `configurations`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION, - FOREIGN KEY(`instance_id`) REFERENCES `instances`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION -); diff --git a/db/migrations/1715450002_instance.down.sql b/db/migrations/1715450002_instance.down.sql deleted file mode 100644 index 80ecbb1..0000000 --- a/db/migrations/1715450002_instance.down.sql +++ /dev/null @@ -1 +0,0 @@ -DROP TABLE `instances`; diff --git a/db/migrations/1715450002_instance.up.sql b/db/migrations/1715450002_instance.up.sql deleted file mode 100644 index 8701079..0000000 --- a/db/migrations/1715450002_instance.up.sql +++ /dev/null @@ -1,6 +0,0 @@ -CREATE TABLE `instances` ( - `id` INTEGER PRIMARY KEY NOT NULL, - `external_id` BLOB UNIQUE NOT NULL -); - -CREATE INDEX instances_external_id_idx ON instances(external_id); diff --git a/db/migrations/1722361838_configuration.down.sql b/db/migrations/1722361838_configuration.down.sql deleted file mode 100644 index b1472ed..0000000 --- a/db/migrations/1722361838_configuration.down.sql +++ /dev/null @@ -1,27 +0,0 @@ -DROP TABLE IF EXISTS `videos`; -DROP TABLE IF EXISTS `instances`; -DROP TABLE IF EXISTS `configurations`; - -CREATE TABLE `configurations` ( - `id` INTEGER PRIMARY KEY NOT NULL, - `theme` TEXT DEFAULT 'default', - `bubble_enabled` BOOLEAN DEFAULT false, - `bubble_text_content` TEXT, - `cta_enabled` BOOLEAN DEFAULT false, - `cta_text_content` TEXT -); -CREATE TABLE `videos` ( - `id` INTEGER PRIMARY KEY NOT NULL, - `url` TEXT NOT NULL, - `weight` INTEGER DEFAULT 1, - `configuration_id` INTEGER NOT NULL, - `instance_id` INTEGER NOT NULL, - FOREIGN KEY(`configuration_id`) REFERENCES `configurations`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION, - FOREIGN KEY(`instance_id`) REFERENCES `instances`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION -); -CREATE TABLE `instances` ( - `id` INTEGER PRIMARY KEY NOT NULL, - `external_id` BLOB UNIQUE NOT NULL -); - -CREATE INDEX instances_external_id_idx ON instances(external_id); diff --git a/db/migrations/1722361838_configuration.up.sql b/db/migrations/1722361838_configuration.up.sql deleted file mode 100644 index d491815..0000000 --- a/db/migrations/1722361838_configuration.up.sql +++ /dev/null @@ -1,13 +0,0 @@ -DROP TABLE IF EXISTS `videos`; -DROP TABLE IF EXISTS `instances`; -DROP TABLE IF EXISTS `configurations`; - -CREATE TABLE `configurations` ( - `id` BLOB UNIQUE NOT NULL, - `theme` TEXT DEFAULT 'default', - `bubble_enabled` BOOLEAN DEFAULT false, - `bubble_text_content` TEXT, - `cta_enabled` BOOLEAN DEFAULT false, - `cta_text_content` TEXT, - `video_url` TEXT -); diff --git a/db/migrations/1715450000_configuration.down.sql b/db/migrations/1725105528_configuration.down.sql similarity index 100% rename from db/migrations/1715450000_configuration.down.sql rename to db/migrations/1725105528_configuration.down.sql diff --git a/db/migrations/1715450000_configuration.up.sql b/db/migrations/1725105528_configuration.up.sql similarity index 70% rename from db/migrations/1715450000_configuration.up.sql rename to db/migrations/1725105528_configuration.up.sql index 968e468..1dabd15 100644 --- a/db/migrations/1715450000_configuration.up.sql +++ b/db/migrations/1725105528_configuration.up.sql @@ -1,9 +1,9 @@ CREATE TABLE `configurations` ( - `id` INTEGER PRIMARY KEY NOT NULL, + `id` BLOB UNIQUE NOT NULL, `theme` TEXT DEFAULT 'default', `bubble_enabled` BOOLEAN DEFAULT false, `bubble_text_content` TEXT, `cta_enabled` BOOLEAN DEFAULT false, - `cta_text_content` TEXT + `cta_text_content` TEXT, + `video_url` TEXT ); - diff --git a/db/migrations/schema.sql b/db/migrations/schema.sql index 6b574b2..4b66018 100644 --- a/db/migrations/schema.sql +++ b/db/migrations/schema.sql @@ -1,22 +1,15 @@ -CREATE TABLE schema_migrations (id VARCHAR(255) NOT NULL PRIMARY KEY); + +-- +-- LibSQL SQL Schema dump automatic generated by geni +-- + +CREATE TABLE schema_migrations (id VARCHAR(255) NOT NULL PRIMARY KEY) CREATE TABLE `configurations` ( -\t`id` integer PRIMARY KEY NOT NULL, - `bubble_enabled` boolean DEFAULT false, - `bubble_text_content` text, - `bubble_type` text DEFAULT 'default', - `cta_enabled` boolean DEFAULT false, - `cta_text_content` text, - `cta_type` text DEFAULT 'default' -); -CREATE TABLE `videos` ( -\t`id` integer PRIMARY KEY NOT NULL, - `url` text NOT NULL, - `weight` integer DEFAULT 1, - `configuration_id` integer NOT NULL, - `instance_id` integer NOT NULL, - FOREIGN KEY(`configuration_id`) REFERENCES `configurations`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION, - FOREIGN KEY(`instance_id`) REFERENCES `instances`(`id`) ON UPDATE NO ACTION ON DELETE NO ACTION -); -CREATE TABLE `instances` ( -\t`id` integer PRIMARY KEY NOT NULL -); + `id` BLOB UNIQUE NOT NULL, + `theme` TEXT DEFAULT 'default', + `bubble_enabled` BOOLEAN DEFAULT false, + `bubble_text_content` TEXT, + `cta_enabled` BOOLEAN DEFAULT false, + `cta_text_content` TEXT, + `video_url` TEXT +) \ No newline at end of file diff --git a/internal/data/instance.go b/internal/data/configuration.go similarity index 72% rename from internal/data/instance.go rename to internal/data/configuration.go index 40ea28e..74d079e 100644 --- a/internal/data/instance.go +++ b/internal/data/configuration.go @@ -2,8 +2,8 @@ package data import ( "database/sql" - "os" "fmt" + "os" "github.com/crocoder-dev/intro-video/internal/config" "github.com/joho/godotenv" @@ -11,37 +11,11 @@ import ( _ "github.com/tursodatabase/libsql-client-go/libsql" ) -type Instance struct { - Id int32 - ExternalId []byte - Videos map[int32]Video - Configurations map[int32]Configuration -} - -type NewVideo struct { - Weight int32 - URL string -} - -type Video struct { - Id int32 - Weight int32 - ConfigurationId int32 - URL string -} - -type NewConfiguration struct { - VideoUrl string - Theme config.Theme - Bubble config.Bubble - Cta config.Cta -} - type Configuration struct { - Id []byte - Theme config.Theme - Bubble config.Bubble - Cta config.Cta + Id []byte + Theme config.Theme + Bubble config.Bubble + Cta config.Cta VideoUrl string } @@ -61,7 +35,7 @@ func NewStore() (Store, error) { } -func (s *Store) LoadConfig(id []byte) (Configuration, error) { +func (s *Store) LoadConfiguration(id []byte) (Configuration, error) { db, err := sql.Open(s.DriverName, s.DatabaseUrl) if err != nil { return Configuration{}, err @@ -113,7 +87,7 @@ func (s *Store) LoadConfig(id []byte) (Configuration, error) { } return Configuration{ - Id: configId, + Id: configId, Theme: config.Theme(theme), Bubble: config.Bubble{ Enabled: bubbleEnabled, @@ -127,7 +101,7 @@ func (s *Store) LoadConfig(id []byte) (Configuration, error) { }, nil } -func (s *Store) CreateConfiguration(configuration NewConfiguration) (Configuration, error) { +func (s *Store) CreateConfiguration(configuration Configuration) (Configuration, error) { db, err := sql.Open(s.DriverName, s.DatabaseUrl) if err != nil { return Configuration{}, err @@ -179,17 +153,17 @@ func (s *Store) CreateConfiguration(configuration NewConfiguration) (Configurati } newConfiguration := Configuration{ - Id: configurationId, - Theme: configuration.Theme, - Bubble: config.Bubble{Enabled: configuration.Bubble.Enabled, TextContent: configuration.Bubble.TextContent}, - Cta: config.Cta{Enabled: configuration.Cta.Enabled, TextContent: configuration.Cta.TextContent}, + Id: configurationId, + Theme: configuration.Theme, + Bubble: config.Bubble{Enabled: configuration.Bubble.Enabled, TextContent: configuration.Bubble.TextContent}, + Cta: config.Cta{Enabled: configuration.Cta.Enabled, TextContent: configuration.Cta.TextContent}, VideoUrl: configuration.VideoUrl, } return newConfiguration, nil } -func (s *Store) UpdateConfiguration(id []byte, configuration NewConfiguration) (Configuration, error) { +func (s *Store) UpdateConfiguration(id []byte, configuration Configuration) (Configuration, error) { db, err := sql.Open(s.DriverName, s.DatabaseUrl) if err != nil { return Configuration{}, err @@ -232,13 +206,12 @@ func (s *Store) UpdateConfiguration(id []byte, configuration NewConfiguration) ( } updatedConfiguration := Configuration{ - Id: id, - Theme: configuration.Theme, - Bubble: config.Bubble{Enabled: configuration.Bubble.Enabled, TextContent: configuration.Bubble.TextContent}, - Cta: config.Cta{Enabled: configuration.Cta.Enabled, TextContent: configuration.Cta.TextContent}, + Id: id, + Theme: configuration.Theme, + Bubble: config.Bubble{Enabled: configuration.Bubble.Enabled, TextContent: configuration.Bubble.TextContent}, + Cta: config.Cta{Enabled: configuration.Cta.Enabled, TextContent: configuration.Cta.TextContent}, VideoUrl: configuration.VideoUrl, } return updatedConfiguration, nil } - diff --git a/internal/data/instance_test.go b/internal/data/configuration_test.go similarity index 96% rename from internal/data/instance_test.go rename to internal/data/configuration_test.go index 668cfbd..fb24e8e 100644 --- a/internal/data/instance_test.go +++ b/internal/data/configuration_test.go @@ -108,7 +108,7 @@ func TestCreateConfiguration(t *testing.T) { store := data.Store{DatabaseUrl: dbName, DriverName: "sqlite3"} - newConfiguration := data.NewConfiguration{ + newConfiguration := data.Configuration{ Theme: config.DefaultTheme, Bubble: config.Bubble{ Enabled: true, @@ -153,7 +153,7 @@ func TestLoadConfiguration(t *testing.T) { store := data.Store{DatabaseUrl: dbName, DriverName: "sqlite3"} - configuration, err := store.LoadConfig(binUlid) + configuration, err := store.LoadConfiguration(binUlid) if err != nil { t.Fatalf("failed to load instance: %v", err) } @@ -196,7 +196,7 @@ func TestUpdateConfiguration(t *testing.T) { store := data.Store{DatabaseUrl: dbName, DriverName: "sqlite3"} - newConfiguration := data.NewConfiguration{ + newConfiguration := data.Configuration{ Theme: config.DefaultTheme, Bubble: config.Bubble{ Enabled: true, @@ -214,7 +214,7 @@ func TestUpdateConfiguration(t *testing.T) { t.Fatalf("failed to create instance: %v", err) } - updatedConfiguration := data.NewConfiguration{ + updatedConfiguration := data.Configuration{ Theme: config.ShadcnThemeDark, Bubble: config.Bubble{ Enabled: false, @@ -224,7 +224,7 @@ func TestUpdateConfiguration(t *testing.T) { Enabled: false, TextContent: "updated cta text", }, - VideoUrl: "updated url", + VideoUrl: "updated url", } expected := data.Configuration{ @@ -241,7 +241,6 @@ func TestUpdateConfiguration(t *testing.T) { VideoUrl: "updated url", } - newConfig, err := store.UpdateConfiguration(configuration.Id, updatedConfiguration) if err != nil { t.Fatalf("failed to update instance: %v", err) @@ -250,4 +249,3 @@ func TestUpdateConfiguration(t *testing.T) { t.Fatalf("Expected updated configuration %+v, got %+v", newConfig, expected) } } - diff --git a/internal/handler/configuration.go b/internal/handler/configuration.go index f47532b..ab6bb92 100644 --- a/internal/handler/configuration.go +++ b/internal/handler/configuration.go @@ -5,9 +5,9 @@ import ( "fmt" "io" "net/http" + "net/url" "os" "strconv" - "net/url" "github.com/crocoder-dev/intro-video/internal" "github.com/crocoder-dev/intro-video/internal/config" @@ -21,84 +21,102 @@ import ( _ "github.com/tursodatabase/libsql-client-go/libsql" ) -func Configuration(c echo.Context) error { - defaultConfig := config.IntroVideoFormValues{ - Url: "", - Theme: config.DefaultTheme, - CtaEnabled: false, - BubbleEnabled: false, - CtaText: "", - BubbleText: "", - } - - id := c.Param("ulid") +func GetConfiguration(c echo.Context) error { + videoFormValues := config.IntroVideoFormValues{ + Url: template.PLACEHOLDER_URL, + Theme: config.DefaultTheme, + CtaEnabled: false, + BubbleEnabled: false, + CtaText: template.PLACEHOLDER_CTA_TEXT, + BubbleText: template.PLACEHOLDER_BUBBLE_TEXT, + } + configuration := data.Configuration{ + VideoUrl: template.PLACEHOLDER_URL, + Theme: config.DefaultTheme, + Bubble: config.Bubble{ + Enabled: false, + TextContent: template.PLACEHOLDER_BUBBLE_TEXT, + }, + Cta: config.Cta{ + Enabled: false, + TextContent: template.PLACEHOLDER_BUBBLE_TEXT, + }, + } - configuration := defaultConfig + id := c.Param("ulid") - if id != "" && id != "new" { - err := godotenv.Load(".env") - if err != nil { - return err - } + if id != "" && id != "new" { + err := godotenv.Load(".env") + if err != nil { + return err + } - dbUrl := os.Getenv("DATABASE_URL") - authToken := os.Getenv("TURSO_AUTH_TOKEN") - if dbUrl == "" || authToken == "" { - return fmt.Errorf("DATABASE_URL and TURSO_AUTH_TOKEN must be set in .env file") - } + dbUrl := os.Getenv("DATABASE_URL") + if dbUrl == "" { + return fmt.Errorf("DATABASE_URL must be set in .env file") + } - store := data.Store{DatabaseUrl: dbUrl + "?authToken=" + authToken, DriverName: "libsql"} + store := data.Store{DatabaseUrl: dbUrl, DriverName: "libsql"} - byteId, err := ulid.Parse(id) - if err != nil { + byteId, err := ulid.Parse(id) + if err != nil { return shared.ErrorToast("Failed to parse ULID: %v. Using default configuration.").Render(context.Background(), c.Response().Writer) - } else { - loadedConfig, err := store.LoadConfig(byteId.Bytes()) - if err != nil { + } else { + loadedConfig, err := store.LoadConfiguration(byteId.Bytes()) + if err != nil { return shared.ErrorToast("Failed to load configuration: %v. Using default configuration.").Render(context.Background(), c.Response().Writer) - } else { - configuration = config.IntroVideoFormValues{ - Theme: loadedConfig.Theme, - CtaEnabled: loadedConfig.Cta.Enabled, - BubbleEnabled: loadedConfig.Bubble.Enabled, - BubbleText: loadedConfig.Bubble.TextContent, - CtaText: loadedConfig.Cta.TextContent, - Url: loadedConfig.VideoUrl, - } - } - } - } - - themeOptions := []template.ThemeOption{ - {Caption: "Default Theme", Value: config.DefaultTheme}, - {Caption: "Shadcn Theme - Light", Value: config.ShadcnThemeLight}, - {Caption: "Shadcn Theme - Dark", Value: config.ShadcnThemeDark}, - {Caption: "MaterialUi Theme - Light", Value: config.MaterialUiThemeLight}, - {Caption: "MaterialUi Theme - Dark", Value: config.MaterialUiThemeDark}, - {Caption: "Tailwind Theme - Dark", Value: config.TailwindThemeDark}, - {Caption: "Tailwind Theme - Light", Value: config.TailwindThemeLight}, - {Caption: "CroCoder Theme", Value: config.Crocoder, Selected: true}, - {Caption: "None", Value: config.NoneTheme}, - } - - file, err := os.Open("internal/template/script/base.js") - if err != nil { + } + + configuration = loadedConfig + + videoFormValues = config.IntroVideoFormValues{ + Theme: loadedConfig.Theme, + CtaEnabled: loadedConfig.Cta.Enabled, + BubbleEnabled: loadedConfig.Bubble.Enabled, + BubbleText: loadedConfig.Bubble.TextContent, + CtaText: loadedConfig.Cta.TextContent, + Url: loadedConfig.VideoUrl, + } + } + } + + themeOptions := []template.ThemeOption{ + {Caption: "Default Theme", Value: config.DefaultTheme}, + {Caption: "Shadcn Theme - Light", Value: config.ShadcnThemeLight}, + {Caption: "Shadcn Theme - Dark", Value: config.ShadcnThemeDark}, + {Caption: "MaterialUi Theme - Light", Value: config.MaterialUiThemeLight}, + {Caption: "MaterialUi Theme - Dark", Value: config.MaterialUiThemeDark}, + {Caption: "Tailwind Theme - Dark", Value: config.TailwindThemeDark}, + {Caption: "Tailwind Theme - Light", Value: config.TailwindThemeLight}, + {Caption: "CroCoder Theme", Value: config.Crocoder, Selected: true}, + {Caption: "None", Value: config.NoneTheme}, + } + + file, err := os.Open("internal/template/script/base.js") + if err != nil { return shared.ErrorToast("Something went wrong!").Render(context.Background(), c.Response().Writer) - } - defer file.Close() - base, err := io.ReadAll(file) - if err != nil { + } + defer file.Close() + base, err := io.ReadAll(file) + if err != nil { return shared.ErrorToast("Failed to read the base script file.").Render(context.Background(), c.Response().Writer) - } - basePreviewJs := "" - component := template.Configuration( - themeOptions, - basePreviewJs, - configuration, - id, - ) - return component.Render(context.Background(), c.Response().Writer) + } + basePreviewJS := "" + videoFormProps := template.VideoFormProps{ + ThemeOptions: themeOptions, + BasePreviewJS: basePreviewJS, + FormValues: videoFormValues, + Ulid: id, + } + videoPreviewProps, err := createVideoPreviewProps(configuration) + + component := template.Configuration( + videoFormProps, + videoPreviewProps, + ) + + return component.Render(context.Background(), c.Response().Writer) } func initializeStore() (data.Store, error) { @@ -107,15 +125,13 @@ func initializeStore() (data.Store, error) { return data.Store{}, err } dbUrl := os.Getenv("DATABASE_URL") - authToken := os.Getenv("TURSO_AUTH_TOKEN") - if dbUrl == "" || authToken == "" { - return data.Store{}, fmt.Errorf("DATABASE_URL and TURSO_AUTH_TOKEN must be set in .env file") + if dbUrl == "" { + return data.Store{}, fmt.Errorf("DATABASE_URL must be set in .env file") } - return data.Store{DatabaseUrl: dbUrl + "?authToken=" + authToken, DriverName: "libsql"}, nil + return data.Store{DatabaseUrl: dbUrl, DriverName: "libsql"}, nil } -func parseFormValues(c echo.Context) (data.NewConfiguration, error) { - newVideo := data.NewVideo{Weight: 100, URL: c.FormValue(template.URL)} +func parseFormValues(c echo.Context) (data.Configuration, error) { newTheme := config.Theme(c.FormValue(template.THEME)) newBubbleEnabledStr := c.FormValue(template.BUBBLE_ENABLED) @@ -124,7 +140,7 @@ func parseFormValues(c echo.Context) (data.NewConfiguration, error) { } newBubbleEnabled, err := strconv.ParseBool(newBubbleEnabledStr) if err != nil { - return data.NewConfiguration{}, err + return data.Configuration{}, err } newCtaEnabledStr := c.FormValue(template.CTA_ENABLED) @@ -134,12 +150,12 @@ func parseFormValues(c echo.Context) (data.NewConfiguration, error) { newCtaEnabled, err := strconv.ParseBool(newCtaEnabledStr) if err != nil { - return data.NewConfiguration{}, err + return data.Configuration{}, err } - return data.NewConfiguration{ - VideoUrl: newVideo.URL, - Theme: newTheme, + return data.Configuration{ + VideoUrl: c.FormValue(template.URL), + Theme: newTheme, Bubble: config.Bubble{ Enabled: newBubbleEnabled, TextContent: c.FormValue(template.BUBBLE_TEXT), @@ -151,7 +167,7 @@ func parseFormValues(c echo.Context) (data.NewConfiguration, error) { }, nil } -func validateConfiguration(config data.NewConfiguration) error { +func validateConfiguration(config data.Configuration) error { if err := validateURL(config.VideoUrl); err != nil { return err } @@ -169,7 +185,6 @@ func validateURL(videoUrl string) error { return fmt.Errorf("Video url is invalid!") } - return nil } @@ -231,98 +246,56 @@ func UpdateConfig(c echo.Context) error { return c.NoContent(http.StatusOK) } -func IntroVideoCode(c echo.Context) error { - fmt.Println( - "url", c.FormValue(template.URL), "\n", - "bubbleEnabled", c.FormValue(template.BUBBLE_ENABLED), "\n", - "bubbleText", c.FormValue(template.BUBBLE_TEXT), "\n", - "theme", c.FormValue(template.THEME), "\n", - "ctaEnabled", c.FormValue(template.CTA_ENABLED), "\n", - "ctaText", c.FormValue(template.CTA_TEXT), - ) - - url := c.FormValue(template.URL) - - if url == "" { - url = template.DEFAULT_URL - } +func createVideoPreviewProps(config data.Configuration) (template.VideoPreviewProps, error) { - theme, err := config.NewTheme(c.FormValue(template.THEME)) - if err != nil { - fmt.Println(err) - return shared.ErrorToast("There was an issue generating the theme. Please check the theme value and try again.").Render(context.Background(), c.Response().Writer) + processableFileProps := internal.ProcessableFileProps{ + URL: config.VideoUrl, + Theme: config.Theme, + Bubble: config.Bubble, + Cta: config.Cta, } - bubbleEnabledRaw := c.FormValue(template.BUBBLE_ENABLED) - - var bubbleEnabled bool - - if bubbleEnabledRaw == "" || bubbleEnabledRaw == "off" { - bubbleEnabled = false - } else if bubbleEnabledRaw == "on" || bubbleEnabledRaw == "true" { - bubbleEnabled = true - } + previewScript, err := internal.Script{}.Process(processableFileProps, internal.ProcessableFileOpts{Preview: true}) + previewScript = "" - var bubbleTextContent string + previewStyle, err := internal.Stylesheet{}.Process(processableFileProps, internal.ProcessableFileOpts{Preview: true}) + previewStyle = "" - if bubbleEnabled { - if c.FormValue(template.BUBBLE_TEXT) != "" { - bubbleTextContent = c.FormValue(template.BUBBLE_TEXT) - } else { - bubbleTextContent = template.DEFAULT_BUBBLE_TEXT - } + js, err := internal.Script{}.Process(processableFileProps, internal.ProcessableFileOpts{Minify: true}) + if err != nil { + return template.VideoPreviewProps{}, err } - var ctaEnabled bool - - ctaEnabledRaw := c.FormValue(template.CTA_ENABLED) - if ctaEnabledRaw == "on" || ctaEnabledRaw == "true" { - ctaEnabled = true - } else if ctaEnabledRaw == "off" || ctaEnabledRaw == "" { - ctaEnabled = false + css, err := internal.Stylesheet{}.Process(processableFileProps, internal.ProcessableFileOpts{Minify: true}) + if err != nil { + return template.VideoPreviewProps{}, err } + return template.VideoPreviewProps{ + JS: js, + CSS: css, + PreviewScript: previewScript, + PreviewStyle: previewStyle, + }, nil +} - var ctaTextContent string +func IntroVideoCode(c echo.Context) error { - if ctaEnabled { - if c.FormValue(template.CTA_TEXT) != "" { - ctaTextContent = c.FormValue(template.CTA_TEXT) - } else { - ctaTextContent = template.DEFAULT_CTA_TEXT - } - } + configuration, err := parseFormValues(c) - processableFileProps := internal.ProcessableFileProps{ - URL: url, - Theme: theme, - Bubble: config.Bubble{ - Enabled: bubbleEnabled, - TextContent: bubbleTextContent, - }, - Cta: config.Cta{ - Enabled: ctaEnabled, - TextContent: ctaTextContent, - }, + if err != nil { + return shared.ErrorToast("Something went wrong!").Render(context.Background(), c.Response().Writer) } - previewScript, err := internal.Script{}.Process(processableFileProps, internal.ProcessableFileOpts{Preview: true}) - previewScript = "" - - previewStyle, err := internal.Stylesheet{}.Process(processableFileProps, internal.ProcessableFileOpts{Preview: true}) - previewStyle = "" - - js, err := internal.Script{}.Process(processableFileProps, internal.ProcessableFileOpts{Minify: true}) + err = validateConfiguration(configuration) if err != nil { - fmt.Println(err) - return shared.ErrorToast("An error occurred while generating the script. Please try again later.").Render(context.Background(), c.Response().Writer) + return shared.ErrorToast(err.Error()).Render(context.Background(), c.Response().Writer) } - css, err := internal.Stylesheet{}.Process(processableFileProps, internal.ProcessableFileOpts{Minify: true}) + videoPreviewProps, err := createVideoPreviewProps(configuration) if err != nil { - fmt.Println(err) - return shared.ErrorToast("An error occurred while generating the stylesheet. Please try again later.").Render(context.Background(), c.Response().Writer) + return shared.ErrorToast(err.Error()).Render(context.Background(), c.Response().Writer) } - component := template.IntroVideoPreview(js, css, previewScript, previewStyle) + + component := template.IntroVideoPreview(videoPreviewProps) return component.Render(context.Background(), c.Response().Writer) } - diff --git a/internal/template/configuration.templ b/internal/template/configuration.templ index fb8597a..b92e417 100644 --- a/internal/template/configuration.templ +++ b/internal/template/configuration.templ @@ -3,8 +3,8 @@ package template import "github.com/crocoder-dev/intro-video/internal/config" type ThemeOption struct { - Caption string - Value config.Theme + Caption string + Value config.Theme Selected bool } @@ -24,7 +24,7 @@ script handleErrors() { }); } -templ Configuration(themeOptions []ThemeOption, basePreviewJs string, formInputValues config.IntroVideoFormValues, id string) { +templ Configuration(videoFormProps VideoFormProps, videoPreviewProps VideoPreviewProps) {
@@ -55,23 +55,18 @@ templ Configuration(themeOptions []ThemeOption, basePreviewJs string, formInputV