Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add User Contest in Profile Page & Add Pagination component #291

Merged
merged 24 commits into from
Mar 2, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
24 commits
Select commit Hold shift + click to select a range
0b7e58a
refactor: Rebase
goo314 Jan 10, 2022
bc6c7ec
fix: Delete routers of profile-contest
goo314 Jan 11, 2022
07e6875
feat: Add ProfileContest vue
goo314 Jan 11, 2022
b9b44af
feat: Publish user contest page
goo314 Jan 14, 2022
365c56f
add: Add file in gitignore
goo314 Jan 14, 2022
1867d42
feat: Add Pagination component
goo314 Jan 21, 2022
59987a7
feat: Add methods for each button in pagination vue
goo314 Jan 24, 2022
606cd19
chore: Remove chart.js outside frontend folder
goo314 Jan 28, 2022
cdba955
fix: Show at most limit pages
goo314 Jan 28, 2022
0e421b4
style: Shorten style code
goo314 Jan 28, 2022
6694e96
fix: Add component communication(props, event)
goo314 Jan 28, 2022
fc89247
style: Apply Pagination vue
goo314 Jan 28, 2022
eea45dd
style: Edit code style
goo314 Feb 6, 2022
91e7d1e
feat: Create USerContestAPI
goo314 Feb 8, 2022
9a04321
add: Calate and return user rank in UserContestAPI
goo314 Feb 9, 2022
79abf47
style: Change code style
goo314 Feb 10, 2022
2d03088
add: Add rank field in ACMContestRank & Shorten UserContestAPI
goo314 Feb 10, 2022
61456b6
add: Connect UserContestAPI to ProfileContest page
goo314 Feb 14, 2022
8ee4300
feat: Add sorting contests function in ProfileContest
goo314 Feb 20, 2022
b8b6528
fix: Connect sorting api to ProfileContest
goo314 Feb 20, 2022
8facf45
feat: Add test for user-contest-api
goo314 Feb 26, 2022
26489aa
feat: add contestprize model
jimin9038 Feb 25, 2022
b7adf68
style: Change code style
goo314 Mar 1, 2022
03e7fe9
migrate model change
jimin9038 Mar 2, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -196,3 +196,6 @@ typings/

# devcontainer
.devcontainer/data

# MacOS file
.DS_Store
1 change: 1 addition & 0 deletions frontend/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"axios": "^0.24.0",
"bootstrap-vue": "^2.21.2",
"browser-detect": "^0.2.28",
"chart.js": "^3.7.0",
"core-js": "^3.19.3",
"highlight.js": "10.7.3",
"iview": "2",
Expand Down
108 changes: 108 additions & 0 deletions frontend/src/pages/oj/components/Pagination.vue
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<template>
<div class="page-itm">
<div @click="changePage(1)" class="page-btn leftedge">«</div>
<div @click="changePage(currentPage-1)" class="page-btn">&lt;</div>
<div
v-for="page in pageList"
:key="page"
@click="changePage(page)"
:class="[ page==currentPage? 'page-btn select': 'page-btn' ]">
{{page}}
</div>
<div @click="changePage(currentPage+1)" class="page-btn">&gt;</div>
<div @click="changePage(numberOfPages)" class="page-btn rightedge">»</div>
</div>
</template>

<script>
export default {
name: 'Pagination',
props: {
totalRows: { // number of total rows in table
type: Number
},
perPage: { // number of rows in table per one page
type: Number
},
limit: {
type: String
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

limit은 어떤 목적으로 사용되는 변수인가요? String type으로 되어있는데, 아래에는 숫자 연산(/, *)이 있어서 확인 부탁드려요.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pagination에 페이지 번호가 최대 몇개 보여질지 나타내는 변수입니다.
총 페이지가 10이고, limit가 3일때 방향키를 누를때마다 (1,2,3), (4,5,6), (7,8,9), (10) 의 페이지가 차례대로 보여요.
b-pagination과 호환되게 코드를 작성하라고 하셔서,
b-pagination의 경우 props를 :limit="3" 가 아닌 limit="3" 문자열으로 받기에 이렇게 코드를 작성했습니다.
javascript상에서 문자열 3과 숫자 3과 동일하다고 들어서 코드의 실행에는 상관이 없지만 혹시 문제가 된다면 바꾸겠습니다 😃

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BootstrapVue 문서 확인해보니까 Number or String으로 명시되어있는데, 여기도 그렇게 하는 게 나을 것 같아요.
https://bootstrap-vue.org/docs/components/pagination#__BVID__579__row_limit

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

BootstrapVue 코드도 확인해봤는데, 이렇게 integer로 명시적으로(explicit) 변환하는 함수가 있더라고요.
https://github.com/bootstrap-vue/bootstrap-vue/blob/c645a33790ccaa0e4695dc7b74f9c9d7a812aa8d/src/mixins/pagination.js#L79

const sanitizeLimit = value => {
  const limit = toInteger(value) || 1
  return limit < 1 ? DEFAULT_LIMIT : limit
}

물론 javascript가 dynamic typed라서 string도 number 연산이 가능하긴 하지만, 이런 암시적인(implicit) 코드를 짜면 어느 부분에서 에러가 날지 예측하기도 어렵고, 가독성도 떨어져서 가급적 지양하는 게 좋아요.
이 부분에서는 computed로 localLimit () { return Number(this.limit) }처럼 사용하는 게 바람직해 보이네요.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정했어요!

}
},
data () {
return {
currentPage: 1
}
},
methods: {
changePage (page) {
if (page >= 1 && page <= this.numberOfPages) {
this.currentPage = page
this.$emit('input', this.currentPage)
}
}
},
computed: {
numberOfPages () { // number of pages
return Math.ceil(this.totalRows / this.perPage)
},
startPage () {
var start = (Math.trunc((this.currentPage - 1) / this.limit)) * this.limit + 1
return start
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

한 줄로 수정해주세요. 참고로 var은 가능한 쓰지 않고, 변하지 않는 값은 모두 const로 고쳐주세요.

return Math.trunc((this.currentPage - 1) / this.limit) * this.limit + 1

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아하 네

},
endPage () {
var end = this.startPage + Number(this.limit) - 1
return end <= this.numberOfPages ? end : this.numberOfPages
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

return this.startPage + Number(this.limit) - 1 <= this.numberOfPages ? end : this.numberOfPages

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이것도 곧 수정하겠습니다

},
pageList () {
var pages = []
for (let i = this.startPage; i <= this.endPage; i++) {
pages.push(i)
}
return pages
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

구글링하다 나온건데, 이렇게 쓰는 게 좀 더 깔끔할 거 같아요.
https://stackoverflow.com/questions/3895478/does-javascript-have-a-method-like-range-to-generate-a-range-within-the-supp

/* this.limit의 type이 Number라면 Number()는 필요 없음 */
return [...Array(Number(this.limit)).keys()].map(i => i + this.startPage)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

만약 페이지 수가 3이고, limit 값이 5이면 오류가 날 수 있을 것 같아요.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

한 줄로 고쳤습니다

}
}
}
</script>

<style lang="scss" scoped>
.page-itm {
width: 95%;
margin: 20px 5% 16px 0;
display: flex;
justify-content: flex-end !important;
flex-direction: row;
}

.page-btn {
width: 35px;
height: 38px;
text-align: center;
margin-left: -1px;
line-height: 35px;
color: #bdbdbd;
border: thin solid #dadada;
cursor: pointer;
}

.page-btn.leftedge {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

어차피 scoped style 썼으니까 .page-btn.leftedge 대신에 .leftedge로 써도 됩니다.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

아래 라인들도 마찬가지입니다!

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

수정했습니다

border-top-left-radius: 0.25rem !important;
border-bottom-left-radius: 0.25rem !important;
}

.page-btn.rightedge {
border-top-right-radius: 0.25rem !important;
border-bottom-right-radius: 0.25rem !important;
}

.page-btn.select {
background-color: #bdbdbd;
border: thin solid #bdbdbd;
color: white;
pointer-events: none;
}

.page-btn:hover {
background-color: #e9ecee;
}

</style>
153 changes: 135 additions & 18 deletions frontend/src/pages/oj/components/user/ProfileContest.vue
Original file line number Diff line number Diff line change
@@ -1,38 +1,155 @@
<script src="./node_modules/chart.js/dist/chart.js"></script>
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Chart.js 패키지는 NPM으로 설치했으니까, 이 라인은 필요 없습니다. node_modules에 있는 패키지가 사용될 거에요.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

네!

<template>
<div class="profile-contest">
<div class="section-title mt-4 mb-3">
My Contest Rank Transition
<div class="section-title mt-4 mb-3">My Contest Rank Transition</div>

<div class="rank-chart">
<canvas id="myChart"></canvas>
</div>

<div class="sort-container">
<b-dropdown text="Date" class="mr-4">
<b-dropdown-item>All</b-dropdown-item>
<b-dropdown-item>Rank</b-dropdown-item>
<b-dropdown-item>Prize</b-dropdown-item>
</b-dropdown>
</div>

<div class="table">
<b-table hover :items="items" :fields="fields" head-variant="light">
<template #cell(title)="data">{{ data.item.title }}</template>
<template #cell(prize)="data">
<b-icon
icon="circle-fill"
style="color: #ff6663"
font-scale="1.2"
></b-icon>
{{ data.item.prize }}
</template>
</b-table>
</div>
<div class="pagination">
<Pagination
v-model="currentPage"
:total-rows="rows"
:per-page="perPage"
limit="5"
></Pagination>
</div>
</div>
</template>

<script>
import { Chart, registerables } from "chart.js";
import Pagination from "../Pagination.vue";
Chart.register(...registerables);

export default {
name: 'ProfileContest',
components: {
},
props: {
},
data () {
components: { Pagination },
props: {},
data() {
return {
}
rows: 100,
currentPage: 1,
perPage: 3,
Chart,
fields: [
{ key: "date", label: "Date" },
{ key: "title", label: "Title" },
{ key: "rank", label: "Rank" },
{ key: "prize", label: "Prize" },
],
items: [
{
date: "2021-12-31",
title: "SKKU Coding Platform 모의대회",
rank: "1",
prize: "Top3",
},
{
date: "2021-12-31",
title: "SKKU Coding Platform 2차 모의대회",
rank: "2",
prize: "Top5",
},
{
date: "2021-12-31",
title: "SKKU Coding Platform 3차 모의대회",
rank: "100",
prize: "Top2",
},
],
};
},
async mounted () {
async mounted() {
this.drawChart();
},
methods: {

drawChart() {
const ctx = document.getElementById("myChart");
const myChart = new Chart(ctx, {
type: "line",
data: {
labels: [
"Contest1",
"Contest2",
"Contest3",
"Contest4",
"Contest5",
"Contest6",
],
datasets: [
{
label: "Me",
data: [30, 85, 20, 100, 50, 1],
borderColor: "#FF6663",
backgroundColor: "#FF6663",
},
],
},
options: {
responsive: true,
plugins: {
legend: {
position: "top",
},
},
},
});
},
},
computed: {
}
}
computed: {},
};
</script>

<style lang="scss" scoped>
.profile-contest {
display: flex;
flex-direction: column;
align-items: center
}
.profile-contest {
display: flex;
flex-direction: column;
align-items: center;
}
.rank-chart {
width: 80%;
}

.sort-container {
width: 95%;
margin-top: 30px;
display: flex;
justify-content: flex-end !important;
}

.table {
width: 95%;
margin: 0 auto;
cursor: pointer;
}
.pagination {
width: 95%;
margin-right: 5%;
margin-top: 20px;
display: flex;
justify-content: flex-end !important;
}
</style>
2 changes: 1 addition & 1 deletion frontend/src/pages/oj/router/routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ export default [
{
name: 'profile',
path: '/profile',
meta: { requiresAuth: true, title: 'My Profile' },
meta: { title: 'My Profile' },
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

이 부분은 왜 수정하셨나요?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

백엔드 서버 없이 실행하면서 작업하다 보니 (ㅠㅠㅠ) 저부분은 무시해주세요!

component: Profile
},
{
Expand Down
5 changes: 5 additions & 0 deletions frontend/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -2752,6 +2752,11 @@ chardet@^0.7.0:
resolved "https://registry.yarnpkg.com/chardet/-/chardet-0.7.0.tgz#90094849f0937f2eedc2425d0d28a9e5f0cbad9e"
integrity sha512-mT8iDcrh03qDGRRmoA2hmBJnxpllMR+0/0qlzjqZES6NdiWDcZkCNAk4rPFZ9Q85r27unkiNNg8ZOiwZXBHwcA==

chart.js@^3.7.0:
version "3.7.0"
resolved "https://registry.yarnpkg.com/chart.js/-/chart.js-3.7.0.tgz#7a19c93035341df801d613993c2170a1fcf1d882"
integrity sha512-31gVuqqKp3lDIFmzpKIrBeum4OpZsQjSIAqlOpgjosHDJZlULtvwLEZKtEhIAZc7JMPaHlYMys40Qy9Mf+1AAg==

check-types@^8.0.3:
version "8.0.3"
resolved "https://registry.yarnpkg.com/check-types/-/check-types-8.0.3.tgz#3356cca19c889544f2d7a95ed49ce508a0ecf552"
Expand Down