add feature tambah data
This commit is contained in:
@@ -6,12 +6,15 @@ import (
|
||||
navbarmenu "cpone/component/navbar"
|
||||
"cpone/component/pagination"
|
||||
sidebaruserprofile "cpone/component/sidebar_user_profile"
|
||||
customtoastv2 "cpone/component/toastbootstrap"
|
||||
"cpone/models"
|
||||
"cpone/services"
|
||||
"cpone/utils"
|
||||
dev_mdnatunitview "cpone/views/dev/mdnatunit"
|
||||
dev_mdusergroupview "cpone/views/dev/mdusergroup"
|
||||
"fmt"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"github.com/a-h/templ"
|
||||
"github.com/labstack/echo/v4"
|
||||
@@ -21,6 +24,7 @@ import (
|
||||
type MdNatUnitServices interface {
|
||||
GetListMdNatUnit(search string, currentPage int, rowPerPage int) ([]models.NatUnit, int, error)
|
||||
GetMdNatUnitBreadCrumb(title string) (models.BreadCrumbV1, error)
|
||||
AddMdNatUnit(code string, name string) (models.NatUnit, error)
|
||||
}
|
||||
|
||||
func NeWMdNatUnitHandler(nu MdNatUnitServices) *MdNatUnitHandler {
|
||||
@@ -39,6 +43,8 @@ func (nh *MdNatUnitHandler) HandleShowMdNatUnitScreen(c echo.Context) error {
|
||||
tableID := utils.GenerateRandomID("tablebody")
|
||||
paginationID := utils.GenerateRandomID("paginationid")
|
||||
searchID := utils.GenerateRandomID("searchid")
|
||||
dialogAddID := utils.GenerateRandomID("dialogaddID")
|
||||
dialogAddBodyID := utils.GenerateRandomID("dialogaddbodyID")
|
||||
dataMenu, err := services.GetMenu()
|
||||
fmt.Println(dataMenu)
|
||||
fmt.Println(err)
|
||||
@@ -128,15 +134,67 @@ func (nh *MdNatUnitHandler) HandleShowMdNatUnitScreen(c echo.Context) error {
|
||||
dev_mdnatunitview.AfterRequestContent(),
|
||||
)
|
||||
|
||||
// modal add form
|
||||
dialogAddBodyCmp := dev_mdnatunitview.BodyFormNatUnit(
|
||||
models.CustomTextFieldv2Prm{
|
||||
Label: "Kode Unit",
|
||||
Name: "natunitcode",
|
||||
Placeholder: "Kode Unit",
|
||||
Type: "text", ID: "natunitinputname"},
|
||||
models.CustomTextFieldv2Prm{
|
||||
Label: "Nama Unit",
|
||||
Name: "natunitname",
|
||||
Placeholder: "Nama Unit",
|
||||
Type: "text", ID: "natunitinputname"},
|
||||
models.CustomTextFieldv2Prm{
|
||||
Label: "Kode Unit",
|
||||
Name: "natunitid",
|
||||
Placeholder: "Kode Unit",
|
||||
Type: "hidden", ID: "natunitinputid"},
|
||||
dialogAddBodyID, dev_mdnatunitview.JsHideModal(""),
|
||||
)
|
||||
|
||||
modalActionAddCmp := dev_mdnatunitview.ActionFormNatUnit(
|
||||
"/dev/md/natunit/closeaddform",
|
||||
"#"+dialogAddBodyID,
|
||||
"outerHTML",
|
||||
"#"+dialogAddID,
|
||||
)
|
||||
|
||||
btnCloseModalAdd := dev_mdnatunitview.BtnCloseFormNatUnit(
|
||||
"/dev/md/natunit/closeaddform",
|
||||
"#"+dialogAddBodyID,
|
||||
"outerHTML",
|
||||
"#"+dialogAddID,
|
||||
)
|
||||
|
||||
modalAddFormComponent := dev_mdnatunitview.NatUnitForm(
|
||||
models.NatUnitFormComponent{
|
||||
IDComponent: "formnatunit",
|
||||
Link: "/dev/md/natunit/add",
|
||||
HxTarget: "#" + dialogAddBodyID,
|
||||
HxSwap: "outerHTML",
|
||||
HxInclude: "#tableID, #paginationID, #searchID, #dialogAddBodyID, #dialogAddID, #" + searchID + ", #currpage" + paginationID,
|
||||
ModalID: dialogAddID,
|
||||
ModalTitle: "New - Unit",
|
||||
DialogBody: dialogAddBodyCmp,
|
||||
DialogAction: modalActionAddCmp,
|
||||
ButtonCLose: btnCloseModalAdd,
|
||||
},
|
||||
)
|
||||
|
||||
// content, css, js
|
||||
content := dev_mdnatunitview.MdNatUnitScreen(
|
||||
tableID,
|
||||
paginationID,
|
||||
searchID,
|
||||
dialogAddID,
|
||||
dialogAddBodyID,
|
||||
breadcrumbComponent,
|
||||
tableComponent,
|
||||
natUnitFilterComponent,
|
||||
natUnitPaginationComponent,
|
||||
modalAddFormComponent,
|
||||
)
|
||||
css := dev_mdnatunitview.CssMdNatUnit()
|
||||
js := dev_mdnatunitview.JsMdNatUnit()
|
||||
@@ -234,3 +292,195 @@ func (nh *MdNatUnitHandler) HandlerChangePageMdNatUnit(c echo.Context) error {
|
||||
retval = append(retval, natUnitPaginationComponent)
|
||||
return utils.ViewMulti(c, retval)
|
||||
}
|
||||
|
||||
func (nh *MdNatUnitHandler) HandlerCloseFormAdd(c echo.Context) error {
|
||||
dialogAddBodyID := c.FormValue("dialogAddBodyID")
|
||||
newForm := dev_mdnatunitview.BodyFormNatUnit(
|
||||
models.CustomTextFieldv2Prm{
|
||||
Label: "Kode Unit",
|
||||
Name: "natunitcode",
|
||||
Placeholder: "Kode Unit",
|
||||
Type: "text", ID: "natunitinputname"},
|
||||
models.CustomTextFieldv2Prm{
|
||||
Label: "Nama Unit",
|
||||
Name: "natunitname",
|
||||
Placeholder: "Nama Unit",
|
||||
Type: "text", ID: "natunitinputname"},
|
||||
models.CustomTextFieldv2Prm{
|
||||
Label: "Kode Unit",
|
||||
Name: "natunitid",
|
||||
Placeholder: "Kode Unit",
|
||||
Type: "hidden", ID: "natunitinputid"},
|
||||
dialogAddBodyID, dev_mdusergroupview.JsHideModal(""))
|
||||
return utils.View(c, newForm)
|
||||
}
|
||||
|
||||
func (nh *MdNatUnitHandler) HandlerAddNatUnit(c echo.Context) error {
|
||||
logger, _ := zap.NewProduction()
|
||||
defer logger.Sync()
|
||||
|
||||
// prm form addnatunit
|
||||
code := c.FormValue("natunitcode")
|
||||
name := c.FormValue("natunitname")
|
||||
tableID := c.FormValue("tableID")
|
||||
paginationID := c.FormValue("paginationID")
|
||||
searchID := c.FormValue("searchID")
|
||||
search := c.FormValue("search")
|
||||
currPagePrm := c.FormValue("currpage" + paginationID)
|
||||
dialogAddBodyID := c.FormValue("dialogAddBodyID")
|
||||
dialogAddID := c.FormValue("dialogAddID")
|
||||
|
||||
currPage, err := strconv.Atoi(currPagePrm)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
logger.Info("ADD UNIT PARAM",
|
||||
zap.Any("code", code),
|
||||
zap.Any("nama", name),
|
||||
zap.Any("tableid", tableID),
|
||||
zap.Any("search", search),
|
||||
zap.Any("currpage", currPage),
|
||||
)
|
||||
|
||||
formValidation := ""
|
||||
codeValidation := ""
|
||||
nameValidation := ""
|
||||
if strings.TrimSpace(code) == "" {
|
||||
formValidation = "Code unit tidak boleh kosong"
|
||||
codeValidation = "Code unit tidak boleh kosong"
|
||||
nameValidation = ""
|
||||
}
|
||||
if strings.TrimSpace(name) == "" {
|
||||
formValidation = "Nama unit tidak boleh kosong"
|
||||
codeValidation = ""
|
||||
nameValidation = "Nama unit tidak boleh kosong"
|
||||
}
|
||||
if strings.TrimSpace(code) == "" && strings.TrimSpace(name) == "" {
|
||||
formValidation = "Code dan nama unit tidak boleh kosong"
|
||||
codeValidation = "Code unit tidak boleh kosong"
|
||||
nameValidation = "Nama unit tidak boleh kosong"
|
||||
|
||||
}
|
||||
|
||||
if formValidation != "" || codeValidation != "" || nameValidation != "" {
|
||||
newForm := dev_mdnatunitview.BodyFormNatUnit(
|
||||
models.CustomTextFieldv2Prm{
|
||||
Label: "Kode Unit",
|
||||
Name: "natunitcode",
|
||||
Placeholder: "Kode Unit",
|
||||
ErrorMsg: codeValidation,
|
||||
Value: code,
|
||||
Type: "text"},
|
||||
models.CustomTextFieldv2Prm{
|
||||
Label: "Nama Unit",
|
||||
Name: "natunitname",
|
||||
Placeholder: "Nama Unit",
|
||||
ErrorMsg: nameValidation,
|
||||
Value: name,
|
||||
Type: "text"},
|
||||
models.CustomTextFieldv2Prm{
|
||||
Label: "Kode Unit",
|
||||
Name: "natunitid",
|
||||
Placeholder: "Kode Unit",
|
||||
Type: "hidden", ID: "natunitinputid"},
|
||||
dialogAddBodyID, dev_mdnatunitview.JsHideModal(""))
|
||||
return utils.ViewMulti(c, []templ.Component{customtoastv2.CustomToastV2Show("Warning", formValidation, "warning"), newForm})
|
||||
}
|
||||
|
||||
// add nat unit service
|
||||
dataNatUnit, err := nh.MdNatUnitServices.AddMdNatUnit(code, name)
|
||||
if err != nil {
|
||||
logger.Info("Error",
|
||||
zap.Any("Error", err),
|
||||
zap.Any("code", code),
|
||||
zap.Any("name", name),
|
||||
)
|
||||
newForm := dev_mdnatunitview.BodyFormNatUnit(
|
||||
models.CustomTextFieldv2Prm{
|
||||
Label: "Kode Unit",
|
||||
Name: "natunitcode",
|
||||
Placeholder: "Kode Unit",
|
||||
ErrorMsg: codeValidation,
|
||||
Value: code,
|
||||
Type: "text"},
|
||||
models.CustomTextFieldv2Prm{
|
||||
Label: "Nama Unit",
|
||||
Name: "natunitname",
|
||||
Placeholder: "Nama Unit",
|
||||
ErrorMsg: nameValidation,
|
||||
Value: name,
|
||||
Type: "text"},
|
||||
models.CustomTextFieldv2Prm{
|
||||
Label: "Kode Unit",
|
||||
Name: "natunitid",
|
||||
Placeholder: "Kode Unit",
|
||||
Type: "hidden", ID: "natunitinputid"},
|
||||
dialogAddBodyID, dev_mdnatunitview.JsHideModal(""))
|
||||
return utils.ViewMulti(c, []templ.Component{
|
||||
customtoastv2.CustomToastV2Show("Error", err.Error(), "danger"),
|
||||
newForm})
|
||||
}
|
||||
logger.Info("ADD UNIT",
|
||||
zap.Any("return", dataNatUnit),
|
||||
zap.Any("code", code),
|
||||
zap.Any("name", name),
|
||||
)
|
||||
|
||||
// get list nat unit for table
|
||||
dataNatUnitList, totalPage, err := nh.MdNatUnitServices.GetListMdNatUnit(search, currPage, 5)
|
||||
if err != nil {
|
||||
defer logger.Sync()
|
||||
logger.Info("ERROR GET UNIT",
|
||||
zap.Any("error", err),
|
||||
)
|
||||
fmt.Println(dataNatUnit)
|
||||
return err
|
||||
}
|
||||
|
||||
// table component
|
||||
tableComponent := dev_mdnatunitview.TableNatUnit(
|
||||
dataNatUnitList,
|
||||
tableID,
|
||||
)
|
||||
|
||||
// pagination component
|
||||
natUnitPaginationComponent := pagination.PaginationV3(
|
||||
totalPage,
|
||||
currPage,
|
||||
"/dev/md/natunit/changepage",
|
||||
paginationID,
|
||||
"#tableID, #searchID, #"+searchID+", #paginationID",
|
||||
"#"+paginationID,
|
||||
"outerHTML", "", "true",
|
||||
dev_mdnatunitview.BeforeRequestContent(),
|
||||
dev_mdnatunitview.AfterRequestContent(),
|
||||
)
|
||||
|
||||
newForm := dev_mdnatunitview.BodyFormNatUnit(
|
||||
models.CustomTextFieldv2Prm{
|
||||
Label: "Kode Unit",
|
||||
Name: "natunitcode",
|
||||
Placeholder: "Kode Unit",
|
||||
ErrorMsg: codeValidation,
|
||||
Type: "text"},
|
||||
models.CustomTextFieldv2Prm{
|
||||
Label: "Nama Unit",
|
||||
Name: "natunitname",
|
||||
Placeholder: "Nama Unit",
|
||||
ErrorMsg: nameValidation,
|
||||
Type: "text"},
|
||||
models.CustomTextFieldv2Prm{
|
||||
Label: "Kode Unit",
|
||||
Name: "natunitid",
|
||||
Placeholder: "Kode Unit",
|
||||
Type: "hidden", ID: "natunitinputid"},
|
||||
dialogAddBodyID, dev_mdnatunitview.JsHideModal("#"+dialogAddID))
|
||||
toastSuccess := customtoastv2.CustomToastV2Show("Success", "Berhasil insert unit "+name, "success")
|
||||
|
||||
return utils.ViewMulti(c, []templ.Component{
|
||||
toastSuccess,
|
||||
tableComponent,
|
||||
natUnitPaginationComponent,
|
||||
newForm,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -253,4 +253,6 @@ func SetupRoutesDev(app *echo.Echo, appStore db.AppStore) {
|
||||
dev.GET("/md/natunit", devMdNatUnitHandlers.HandleShowMdNatUnitScreen)
|
||||
dev.GET("/md/natunit/filter", devMdNatUnitHandlers.HandleFilterMdNatUnit)
|
||||
dev.GET("/md/natunit/changepage", devMdNatUnitHandlers.HandlerChangePageMdNatUnit)
|
||||
dev.POST("/md/natunit/closeaddform", devMdNatUnitHandlers.HandlerCloseFormAdd)
|
||||
dev.POST("/md/natunit/add", devMdNatUnitHandlers.HandlerAddNatUnit)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user