editing.go 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package e21_presents
  2. import (
  3. "fmt"
  4. "io/ioutil"
  5. "net/http"
  6. "github.com/qor5/admin/presets"
  7. "github.com/qor5/ui/tiptap"
  8. "github.com/qor5/web"
  9. "github.com/sunfmin/reflectutils"
  10. h "github.com/theplant/htmlgo"
  11. "gorm.io/gorm"
  12. )
  13. // @snippet_begin(PresetsEditingCustomizationDescriptionSample)
  14. func PresetsEditingCustomizationDescription(b *presets.Builder) (
  15. cust *presets.ModelBuilder,
  16. cl *presets.ListingBuilder,
  17. ce *presets.EditingBuilder,
  18. db *gorm.DB,
  19. ) {
  20. cust, cl, ce, db = PresetsListingCustomizationBulkActions(b)
  21. b.URIPrefix(PresetsEditingCustomizationDescriptionPath)
  22. b.ExtraAsset("/tiptap.js", "text/javascript", tiptap.JSComponentsPack())
  23. b.ExtraAsset("/tiptap.css", "text/css", tiptap.CSSComponentsPack())
  24. ce.Only("Name", "CompanyID", "Description")
  25. ce.Field("Description").ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  26. return tiptap.TipTapEditor().
  27. FieldName(field.Name).
  28. Value(field.Value(obj).(string))
  29. })
  30. return
  31. }
  32. const PresetsEditingCustomizationDescriptionPath = "/samples/presets-editing-customization-description"
  33. // @snippet_end
  34. // @snippet_begin(PresetsEditingCustomizationFileTypeSample)
  35. type MyFile string
  36. type Product struct {
  37. ID int
  38. Title string
  39. MainImage MyFile
  40. }
  41. func PresetsEditingCustomizationFileType(b *presets.Builder) (
  42. cust *presets.ModelBuilder,
  43. cl *presets.ListingBuilder,
  44. ce *presets.EditingBuilder,
  45. db *gorm.DB,
  46. ) {
  47. cust, cl, ce, db = PresetsEditingCustomizationDescription(b)
  48. err := db.AutoMigrate(&Product{})
  49. if err != nil {
  50. panic(err)
  51. }
  52. b.URIPrefix(PresetsEditingCustomizationFileTypePath)
  53. b.FieldDefaults(presets.WRITE).
  54. FieldType(MyFile("")).
  55. ComponentFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) h.HTMLComponent {
  56. val := field.Value(obj).(MyFile)
  57. var img h.HTMLComponent
  58. if len(string(val)) > 0 {
  59. img = h.Img(string(val))
  60. }
  61. var er h.HTMLComponent
  62. if len(field.Errors) > 0 {
  63. er = h.Div().Text(field.Errors[0]).Style("color:red")
  64. }
  65. return h.Div(
  66. img,
  67. er,
  68. h.Input("").Type("file").Attr(web.VFieldName(fmt.Sprintf("%s_NewFile", field.Name))...),
  69. )
  70. }).
  71. SetterFunc(func(obj interface{}, field *presets.FieldContext, ctx *web.EventContext) (err error) {
  72. ff, _, _ := ctx.R.FormFile(fmt.Sprintf("%s_NewFile", field.Name))
  73. if ff == nil {
  74. return
  75. }
  76. req, err := http.NewRequest("PUT", "https://transfer.sh/myfile.png", ff)
  77. if err != nil {
  78. return
  79. }
  80. var res *http.Response
  81. res, err = http.DefaultClient.Do(req)
  82. if err != nil {
  83. panic(err)
  84. }
  85. var b []byte
  86. b, err = ioutil.ReadAll(res.Body)
  87. if err != nil {
  88. return
  89. }
  90. if res.StatusCode == 500 {
  91. err = fmt.Errorf("%s", string(b))
  92. return
  93. }
  94. err = reflectutils.Set(obj, field.Name, MyFile(b))
  95. return
  96. })
  97. mb := b.Model(&Product{})
  98. mb.Editing("Title", "MainImage")
  99. return
  100. }
  101. const PresetsEditingCustomizationFileTypePath = "/samples/presets-editing-customization-file-type"
  102. // @snippet_end
  103. // @snippet_begin(PresetsEditingCustomizationValidationSample)
  104. func PresetsEditingCustomizationValidation(b *presets.Builder) (
  105. cust *presets.ModelBuilder,
  106. cl *presets.ListingBuilder,
  107. ce *presets.EditingBuilder,
  108. db *gorm.DB,
  109. ) {
  110. cust, cl, ce, db = PresetsEditingCustomizationDescription(b)
  111. b.URIPrefix(PresetsEditingCustomizationValidationPath)
  112. ce.ValidateFunc(func(obj interface{}, ctx *web.EventContext) (err web.ValidationErrors) {
  113. cus := obj.(*Customer)
  114. if len(cus.Name) < 10 {
  115. err.FieldError("Name", "name is too short")
  116. }
  117. return
  118. })
  119. return
  120. }
  121. const PresetsEditingCustomizationValidationPath = "/samples/presets-editing-customization-validation"
  122. // @snippet_end