Skip to content

Commit

Permalink
Merge pull request #116 from Baihhh/mergeDev
Browse files Browse the repository at this point in the history
fix: fix the bottom out animation
  • Loading branch information
IRONICBo authored Feb 6, 2024
2 parents 9e031ff + 0d83067 commit 18e0336
Show file tree
Hide file tree
Showing 4 changed files with 96 additions and 86 deletions.
21 changes: 14 additions & 7 deletions cmd/gopcomm/yap/home_yap.html
Original file line number Diff line number Diff line change
Expand Up @@ -257,10 +257,11 @@ <h1 class="text-4xl"> Have no data!</h1>
</n-list>

<!-- Loading Status -->
<div class="mt-2 text-center">
<n-spin v-if="!isBottom" size="small" stroke="#3182ce"></n-spin>
<!-- TODO: bug 列表末尾无法显示这行提示语 -->
<span v-else class="text-xs text-gray-400">No more data🙌</span>
<div class="mt-2 text-center" v-if="!isBottom" key="span">
<n-spin :key="spin" size="small" stroke="#3182ce"></n-spin>
</div>
<div class="mt-2 text-center" v-if="!haveMoreData" key="span">
<span class="text-xs text-gray-400">No more data🙌</span>
</div>
</div>
</div>
Expand All @@ -275,12 +276,13 @@ <h1 class="text-4xl"> Have no data!</h1>
console.log(articleList)

const next = ref("{{.Next}}"); // the page size
const haveMoreData = next.value === "eof" ? ref(false) : ref(true);

function listenBottom() {
let scrollTop = window.scrollY;
let clientHeight = document.documentElement.clientHeight;
let scrollHeight = document.documentElement.scrollHeight;
if (isBottom.value && scrollTop + clientHeight + 10>= scrollHeight) {
if (isBottom.value && haveMoreData.value && scrollTop + clientHeight + 10>= scrollHeight) {

// reaching bottom detected
loadMore();
Expand Down Expand Up @@ -309,14 +311,17 @@ <h1 class="text-4xl"> Have no data!</h1>
console.log("todos.next",todos.next)
articleList.value.push(...todos.items);
next.value = todos.next;
if(todos.next === "eof") {
haveMoreData.value = false;
}
isBottom.value = true
}
});
}, 1500)
}, 500)
}else{
isBottom.value = true
haveMoreData.value = false;
}
console.log(isBottom)
};

/*======= register vue component =======*/
Expand All @@ -330,6 +335,8 @@ <h1 class="text-4xl"> Have no data!</h1>
data() {
return {
articleList,
isBottom,
haveMoreData
}
},
methods: {
Expand Down
20 changes: 10 additions & 10 deletions docs/architecture/03_article_package.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,23 +51,23 @@ community, _ = core.New(todo, conf) // Instantiate the community

[func PutArticle(ctx context.Context, uid string, trans string, article *Article) (id string, err error)](#func-putarticle)

[func TransHtmlUrl(ctx context.Context, id string) (htmlUrl string, err error)](https://github.com/goplus/community/blob/dev/internal/core/community.go#L164)
[func TransHtmlUrl(ctx context.Context, id string) (htmlUrl string, err error)](https://github.com/goplus/community/blob/dev/internal/core/community.go#L163)

[func SaveHtml(ctx context.Context, uid, htmlStr, mdData, id string) (articleId string, err error)](https://github.com/goplus/community/blob/dev/internal/core/community.go#L194)
[func SaveHtml(ctx context.Context, uid, htmlStr, mdData, id string) (articleId string, err error)](https://github.com/goplus/community/blob/dev/internal/core/community.go#L193)

[func DeleteArticle(ctx context.Context, uid, id string) (err error)](https://github.com/goplus/community/blob/dev/internal/core/community.go#L250)
[func DeleteArticle(ctx context.Context, uid, id string) (err error)](https://github.com/goplus/community/blob/dev/internal/core/community.go#L292)

[func DeleteArticles(ctx context.Context, uid string) (err error)](https://github.com/goplus/community/blob/dev/internal/core/community.go#L269)
[func DeleteArticles(ctx context.Context, uid string) (err error)](https://github.com/goplus/community/blob/dev/internal/core/community.go#L327)

[func Articles(ctx context.Context, page int, limit int, searchValue string) (items []*ArticleEntry, total int, err error)](https://github.com/goplus/community/blob/dev/internal/core/community.go#L296)
[func ListArticle(ctx context.Context, from string, limit int, searchValue string) (items []*ArticleEntry, next string, err error)](https://github.com/goplus/community/blob/dev/internal/core/community.go#L390)

[func GetArticlesByUid(ctx context.Context, uid string) (items []*ArticleEntry, err error)](https://github.com/goplus/community/blob/dev/internal/core/community.go#L359)
[func GetArticlesByUid(ctx context.Context, uid string) (items []*ArticleEntry, err error)](https://github.com/goplus/community/blob/dev/internal/core/community.go#L438)



### Types

#### type [ArticleEntry](https://github.com/goplus/community/blob/dev/internal/core/community.go#L46)
#### type [ArticleEntry](https://github.com/goplus/community/blob/dev/internal/core/community.go#L49)

> Article Brief information.
Expand All @@ -85,7 +85,7 @@ type ArticleEntry struct {
}
```

#### type [Article](https://github.com/goplus/community/blob/dev/internal/core/community.go#L58)
#### type [Article](https://github.com/goplus/community/blob/dev/internal/core/community.go#L61)

> All information of article.
Expand All @@ -100,7 +100,7 @@ type Article struct {

### Functions

#### func [Article](https://github.com/goplus/community/blob/dev/internal/core/community.go#L136)
#### func [Article](https://github.com/goplus/community/blob/dev/internal/core/community.go#L135)

```go
func Article(ctx context.Context, id string) (article *Article, err error)
Expand All @@ -123,7 +123,7 @@ if editable, _ := community.canEditable(todo, uid, id); !editable {
article, _ := community.article(todo, id)
```

#### func [PutArticle](https://github.com/goplus/community/blob/dev/internal/core/community.go#L221)
#### func [PutArticle](https://github.com/goplus/community/blob/dev/internal/core/community.go#L226)

```go
func PutArticle(ctx context.Context, uid string, trans string, article *Article) (id string, err error)
Expand Down
85 changes: 44 additions & 41 deletions internal/core/community.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,16 +120,16 @@ func New(ctx context.Context, conf *Config) (ret *Community, err error) {
return &Community{bucket, db, domain, casdoorConf, xLog}, nil
}

func (p *Community) getTotal(ctx context.Context, searchValue string) (total int, err error) {
if searchValue != "" {
sqlStr := "select count(*) from article where title like ?"
err = p.db.QueryRow(sqlStr, "%"+searchValue+"%").Scan(&total)
} else {
sqlStr := "select count(*) from article"
err = p.db.QueryRow(sqlStr).Scan(&total)
}
return
}
// func (p *Community) getTotal(ctx context.Context, searchValue string) (total int, err error) {
// if searchValue != "" {
// sqlStr := "select count(*) from article where title like ?"
// err = p.db.QueryRow(sqlStr, "%"+searchValue+"%").Scan(&total)
// } else {
// sqlStr := "select count(*) from article"
// err = p.db.QueryRow(sqlStr).Scan(&total)
// }
// return
// }

// Article returns an article.
func (p *Community) Article(ctx context.Context, id string) (article *Article, err error) {
Expand Down Expand Up @@ -351,40 +351,40 @@ const (
)

// Articles lists articles from a position.
func (p *Community) Articles(ctx context.Context, page int, limit int, searchValue string) (items []*ArticleEntry, total int, err error) {
total, err = p.getTotal(ctx, searchValue)
if err != nil || total == 0 {
return []*ArticleEntry{}, 0, err
}
// func (p *Community) Articles(ctx context.Context, page int, limit int, searchValue string) (items []*ArticleEntry, total int, err error) {
// total, err = p.getTotal(ctx, searchValue)
// if err != nil || total == 0 {
// return []*ArticleEntry{}, 0, err
// }

sqlStr := "select id, title, ctime, user_id, tags, abstract, cover from article order by ctime desc limit ? offset ?"
rows, err := p.db.Query(sqlStr, limit, (page-1)*limit)
if searchValue != "" {
sqlStr := "select id, title, ctime, user_id, tags, abstract, cover from article where title like ? order by ctime desc limit ? offset ?"
rows, err = p.db.Query(sqlStr, "%"+searchValue+"%", limit, (page-1)*limit)
}
if err != nil {
return []*ArticleEntry{}, 0, err
}
defer rows.Close()
// sqlStr := "select id, title, ctime, user_id, tags, abstract, cover from article order by ctime desc limit ? offset ?"
// rows, err := p.db.Query(sqlStr, limit, (page-1)*limit)
// if searchValue != "" {
// sqlStr := "select id, title, ctime, user_id, tags, abstract, cover from article where title like ? order by ctime desc limit ? offset ?"
// rows, err = p.db.Query(sqlStr, "%"+searchValue+"%", limit, (page-1)*limit)
// }
// if err != nil {
// return []*ArticleEntry{}, 0, err
// }
// defer rows.Close()

for rows.Next() {
article := &ArticleEntry{}
err := rows.Scan(&article.ID, &article.Title, &article.Ctime, &article.UId, &article.Tags, &article.Abstract, &article.Cover)
if err != nil {
return []*ArticleEntry{}, 0, err
}
// add author info
user, err := p.GetUserById(article.UId)
if err != nil {
return []*ArticleEntry{}, 0, err
}
article.User = *user
// for rows.Next() {
// article := &ArticleEntry{}
// err := rows.Scan(&article.ID, &article.Title, &article.Ctime, &article.UId, &article.Tags, &article.Abstract, &article.Cover)
// if err != nil {
// return []*ArticleEntry{}, 0, err
// }
// // add author info
// user, err := p.GetUserById(article.UId)
// if err != nil {
// return []*ArticleEntry{}, 0, err
// }
// article.User = *user

items = append(items, article)
}
return items, total, nil
}
// items = append(items, article)
// }
// return items, total, nil
// }

// ListArticle lists articles from a position.
func (p *Community) ListArticle(ctx context.Context, from string, limit int, searchValue string) (items []*ArticleEntry, next string, err error) {
Expand Down Expand Up @@ -427,6 +427,9 @@ func (p *Community) ListArticle(ctx context.Context, from string, limit int, sea
if rowLen == 0 {
return []*ArticleEntry{}, MarkEnd, io.EOF
}
if rowLen < limit {
return items, MarkEnd, io.EOF
}
next = strconv.Itoa(fromInt + rowLen)
return items, next, nil
}
Expand Down
56 changes: 28 additions & 28 deletions internal/core/community_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -235,34 +235,34 @@ func TestDeleteArticle(t *testing.T) {
}
}

func TestArticles(t *testing.T) {
conf := &Config{}
todo := context.TODO()
community, err := New(todo, conf)

if err != nil {
t.Skip(err)
}

// test data
tests := []struct {
page int
limit int
searchValue string
expectedTotal int
}{
{1, 10, "", 10}, // home
{1, 10, "test", 10}, // search
}

for _, tt := range tests {
_, total, _ := community.Articles(todo, tt.page, tt.limit, tt.searchValue)

if total != tt.expectedTotal {
t.Errorf("Articles(%d, %d, %s) returned total: %d, expected: %d", tt.page, tt.limit, tt.searchValue, total, tt.expectedTotal)
}
}
}
// func TestArticles(t *testing.T) {
// conf := &Config{}
// todo := context.TODO()
// community, err := New(todo, conf)

// if err != nil {
// t.Skip(err)
// }

// // test data
// tests := []struct {
// page int
// limit int
// searchValue string
// expectedTotal int
// }{
// {1, 10, "", 10}, // home
// {1, 10, "test", 10}, // search
// }

// for _, tt := range tests {
// _, total, _ := community.Articles(todo, tt.page, tt.limit, tt.searchValue)

// if total != tt.expectedTotal {
// t.Errorf("Articles(%d, %d, %s) returned total: %d, expected: %d", tt.page, tt.limit, tt.searchValue, total, tt.expectedTotal)
// }
// }
// }

func TestGetArticlesByUid(t *testing.T) {
conf := &Config{}
Expand Down

0 comments on commit 18e0336

Please sign in to comment.