Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #341 from hexlet-codebattle/heapmap
Heapmap
- Loading branch information
Showing
18 changed files
with
989 additions
and
550 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import React, { Component } from 'react'; | ||
import ReactLoading from 'react-loading'; | ||
|
||
export default class Messages extends Component { | ||
render() { | ||
return ( | ||
<div className="d-flex my-0 py-1 justify-content-center" > | ||
<ReactLoading type="spin" color="#6c757d" height={50} width={50} /> | ||
</div> | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
import React from 'react'; | ||
import CalendarHeatmap from 'react-calendar-heatmap'; | ||
import Loading from '../components/Loading.jsx'; | ||
import axios from 'axios'; | ||
|
||
class Heatmap extends React.Component { | ||
constructor(props) { | ||
super(props); | ||
this.state = { | ||
activities: null, | ||
}; | ||
} | ||
|
||
colorScale(count) { | ||
if (count >= 5) { | ||
return 'color-huge'; | ||
} else if (count >= 3) { | ||
return 'color-large'; | ||
} else if (count >= 1) { | ||
return 'color-small'; | ||
} | ||
return 'color-empty'; | ||
} | ||
|
||
componentDidMount() { | ||
axios.get('/api/v1/activity') | ||
.then((response) => { console.log(response.data); this.setState(response.data); }); | ||
} | ||
|
||
render() { | ||
const { activities } = this.state; | ||
if (!activities) { | ||
return (<Loading />); | ||
} | ||
return ( | ||
<div className="card shadow rounded"> | ||
<div className="d-flex my-0 py-1 justify-content-center card-header font-weight-bold" > | ||
Activity | ||
</div> | ||
<div className="card-body py-0 my-0" > | ||
<CalendarHeatmap | ||
viewBox="0 0 0 0" | ||
showWeekdayLabels | ||
values={activities} | ||
classForValue={(value) => { | ||
if (!value) { | ||
return 'color-empty'; | ||
} | ||
return this.colorScale(value.count); | ||
}} | ||
titleForValue={(value) => { | ||
if (!value) { | ||
return 'No games'; | ||
} | ||
return `${value.count} games on ${value.date}`; | ||
}} | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} | ||
} | ||
export default Heatmap; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
services/app/lib/codebattle_web/controllers/api/activity_controller.ex
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
defmodule CodebattleWeb.Api.V1.ActivityController do | ||
@all [:index, :show] | ||
|
||
use CodebattleWeb, :controller | ||
|
||
alias Codebattle.{Repo, User, UserGame} | ||
import Ecto.Query, only: [from: 2] | ||
|
||
defmacro to_char(field, format) do | ||
quote do | ||
fragment("to_char(?, ?)", unquote(field), unquote(format)) | ||
end | ||
end | ||
plug(CodebattleWeb.Plugs.RequireAuth when action in @all) | ||
|
||
def show(conn, _params) do | ||
current_user = conn.assigns.current_user | ||
query = from ug in UserGame, | ||
where: ug.user_id == ^current_user.id, | ||
group_by: to_char(ug.inserted_at, "YYYY-mm-dd"), | ||
select: %{date: to_char(ug.inserted_at, "YYYY-mm-dd"), count: count(ug.id)} | ||
|
||
user_games = Repo.all(query) | ||
json conn, %{ activities: user_games } | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.