autocomplete refactor

This commit is contained in:
sindhu
2024-05-14 08:57:03 +07:00
parent 93186320d7
commit a99342edc7
8 changed files with 416 additions and 367 deletions

View File

@@ -0,0 +1,206 @@
package dev_services
import (
"cpone/db"
"cpone/utils"
"fmt"
"strconv"
)
func NewServicesAutoComplete(uStore db.AppStore) *ServicesAutoComplete {
return &ServicesAutoComplete{
AutoCompleteStore: uStore,
}
}
type ServicesAutoComplete struct {
AutoCompleteStore db.AppStore
}
// example autocomplete
func (su *ServicesAutoComplete) GetAutoComplete(param string, pageOfParam string, limitOfStr string) (string, string, string, []AutoComplete, error) {
// fmt.Println("ini param : ", param)
dummyAutoComplete := []AutoComplete{
{
Name: "Google",
},
{
Name: "Google 1",
},
{
Name: "Google 2",
},
{
Name: "Google 3",
},
{
Name: "Google 4",
},
{
Name: "Google 5",
},
{
Name: "Google 6",
},
{
Name: "Gojek",
},
{
Name: "Mozilla Firefox",
},
{
Name: "Apple",
},
{
Name: "Microsoft",
},
{
Name: "Facebook",
},
{
Name: "Amazon",
},
{
Name: "Twitter",
},
{
Name: "Instagram",
},
{
Name: "Snapchat",
},
{
Name: "LinkedIn",
},
{
Name: "Netflix",
},
{
Name: "YouTube",
},
{
Name: "Reddit",
},
{
Name: "WhatsApp",
},
{
Name: "Pinterest",
},
{
Name: "TikTok",
},
{
Name: "Spotify",
},
{
Name: "Zoom",
},
}
var textFound string
var results []AutoComplete
// var currentRes []AutoComplete
// var oldResults []AutoComplete
// var limit int = 3
var count int = 0
var foundCount int = 0
var totalData int = 0
var startIndex int = 0
var endIndex int = 3
if len(dummyAutoComplete) > 0 {
totalData = len(dummyAutoComplete)
}
// pageOf, err := strconv.Atoi(pageOfParam)
// if err != nil {
// return "", "", "", nil, err
// }
limit, err := strconv.Atoi(limitOfStr)
if err != nil {
return "", "", "", nil, err
}
// find data search
if pageOfParam == "1" {
count = 0
// for _, item := range dummyAutoComplete {
// if utils.Contains(item.Name, param) {
// results = append(results, item)
// count++
// if count == limit {
// break
// }
// }
// }
for _, item := range dummyAutoComplete {
if utils.Contains(item.Name, param) {
// Check if the item is already in results
found := false
for _, res := range results {
if res.Name == item.Name {
found = true
break
}
}
// If not found, append it
if !found {
results = append(results, item)
count++
if count == limit {
break
}
}
}
}
} else {
// ketika klik load more
// count = 0
for _, item := range dummyAutoComplete {
if utils.Contains(item.Name, param) {
results = append(results, item)
count++
if count == limit {
break
}
}
}
startIndex = 0
endIndex = limit
if startIndex >= len(dummyAutoComplete) {
return "", "max", "", nil, nil
}
if endIndex > len(dummyAutoComplete) {
endIndex = len(dummyAutoComplete)
}
for _, item := range dummyAutoComplete[startIndex:endIndex] {
found := false
for _, res := range results {
if res.Name == item.Name {
found = true
break
}
}
// If not found, append it
if !found {
results = append(results, item)
}
}
}
foundCount = len(results)
totalCount := len(dummyAutoComplete)
textFound = fmt.Sprintf("Ditemukan %d dari %d", foundCount, totalCount)
return strconv.Itoa(totalData), strconv.Itoa(foundCount), textFound, results, nil
}

View File

@@ -6,7 +6,6 @@ import (
"cpone/db"
"cpone/models"
"cpone/utils"
)
func NewServicesMasterMenuUserGroup(u MasterMenu, uStore db.AppStore) *ServicesMasterMenuUserGroup {
@@ -552,192 +551,6 @@ func (su *ServicesMasterMenuUserGroup) GetMasterMenus() ([]MasterMenu, error) {
return dummyMenu, nil
}
// example autocomplete
func (su *ServicesMasterMenuUserGroup) GetAutoComplete(param string, pageOfParam string, limitOfStr string) (string, string, string, []AutoComplete, error) {
// fmt.Println("ini param : ", param)
dummyAutoComplete := []AutoComplete{
{
Name: "Google",
},
{
Name: "Google 1",
},
{
Name: "Google 2",
},
{
Name: "Google 3",
},
{
Name: "Google 4",
},
{
Name: "Google 5",
},
{
Name: "Google 6",
},
{
Name: "Gojek",
},
{
Name: "Mozilla Firefox",
},
{
Name: "Apple",
},
{
Name: "Microsoft",
},
{
Name: "Facebook",
},
{
Name: "Amazon",
},
{
Name: "Twitter",
},
{
Name: "Instagram",
},
{
Name: "Snapchat",
},
{
Name: "LinkedIn",
},
{
Name: "Netflix",
},
{
Name: "YouTube",
},
{
Name: "Reddit",
},
{
Name: "WhatsApp",
},
{
Name: "Pinterest",
},
{
Name: "TikTok",
},
{
Name: "Spotify",
},
{
Name: "Zoom",
},
}
var textFound string
var results []AutoComplete
// var currentRes []AutoComplete
// var oldResults []AutoComplete
// var limit int = 3
var count int = 0
var foundCount int = 0
var totalData int = 0
var startIndex int = 0
var endIndex int = 3
if len(dummyAutoComplete) > 0 {
totalData = len(dummyAutoComplete)
}
// pageOf, err := strconv.Atoi(pageOfParam)
// if err != nil {
// return "", "", "", nil, err
// }
limit, err := strconv.Atoi(limitOfStr)
if err != nil {
return "", "", "", nil, err
}
// find data search
if pageOfParam == "1" {
count = 0
// for _, item := range dummyAutoComplete {
// if utils.Contains(item.Name, param) {
// results = append(results, item)
// count++
// if count == limit {
// break
// }
// }
// }
for _, item := range dummyAutoComplete {
if utils.Contains(item.Name, param) {
// Check if the item is already in results
found := false
for _, res := range results {
if res.Name == item.Name {
found = true
break
}
}
// If not found, append it
if !found {
results = append(results, item)
count++
if count == limit {
break
}
}
}
}
} else {
// ketika klik load more
// count = 0
for _, item := range dummyAutoComplete {
if utils.Contains(item.Name, param) {
results = append(results, item)
count++
if count == limit {
break
}
}
}
startIndex = 0
endIndex = limit
if startIndex >= len(dummyAutoComplete) {
return "", "max", "", nil, nil
}
if endIndex > len(dummyAutoComplete) {
endIndex = len(dummyAutoComplete)
}
for _, item := range dummyAutoComplete[startIndex:endIndex] {
found := false
for _, res := range results {
if res.Name == item.Name {
found = true
break
}
}
// If not found, append it
if !found {
results = append(results, item)
}
}
}
foundCount = len(results)
totalCount := len(dummyAutoComplete)
textFound = fmt.Sprintf("Ditemukan %d dari %d", foundCount, totalCount)
return strconv.Itoa(totalData), strconv.Itoa(foundCount), textFound, results, nil
}
func (ug *ServicesMasterMenuUserGroup) GetUserGroup() ([]models.UserGroup, error) {
data := []models.UserGroup{
{UserGroupID: 1, UserGroupKode: "A", UserGroupName: "UG1"},