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

Updated home page, connected it with the API #18

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -129,3 +129,4 @@ modules.xml
# Project-level:
config/**
!config/**example**
node_modules/
58 changes: 58 additions & 0 deletions src/scripts/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
var dayButtons = new Vue({
el: "#dayButtons",

data: {
days: []
},

methods: {
colorToHex(color) {
function compToHex(c) {
Copy link
Member

Choose a reason for hiding this comment

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

What does the function name mean?

let hex = c.toString(16);
return hex.length === 1 ? "0" + hex : hex;
}
return "#" + compToHex(color[0]) + compToHex(color[1]) + compToHex(color[2]);
},

isDayFilled(day) {
for(let filledDay of this.days) {
if(Date.parse(filledDay.date) === Date.parse(day.toISOString())) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't think Date.parse should be called each time. You should (probably) store dates as javascript objects, not as strings. And there definitely should be a better way of converting day from a Date object to a timestamp, so at least the right value should be calculated without a call to Date.parse and outside of the loop

Copy link
Member

Choose a reason for hiding this comment

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

Still not resolved

return true;
}
}
return false;
},

getFilledDayColor(day) {
for(let filledDay of this.days) {
if(Date.parse(filledDay.date) === Date.parse(day.toISOString())) {
Copy link
Member

Choose a reason for hiding this comment

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

Same as above

Copy link
Member

Choose a reason for hiding this comment

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

Because you have to similar pieces of code, I would move them to a separate function - something like findDayByDate

return filledDay.average_color;
}
}
return [169, 169, 169];
},

getDisplayedWeeks() {
let closestSunday = new Date();
closestSunday = new Date(Date.UTC(closestSunday.getFullYear(), closestSunday.getMonth(),
closestSunday.getDate() + 7 - closestSunday.getDay()));
Comment on lines +37 to +39
Copy link
Member

Choose a reason for hiding this comment

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

I don't think if it is worth renaming the variable, but I would at least add a comment that this is not the closest Sunday, but the closest Sunday in future. It is not obvious at all unless you delve into the expression used to calculate it

let threeWeeks = [];
for(let i = 2; i >= 0; --i) {
let currentWeek = [];
for(let j = 6; j >= 0; --j) {
currentWeek.push(new Date(closestSunday));
currentWeek[6 - j].setUTCDate(closestSunday.getUTCDate() - j - i * 7);
}
threeWeeks.push(currentWeek);
}
Copy link
Member

Choose a reason for hiding this comment

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

I don't like these loops are going backward and you are pushing to these arrays because you know the arrays' sizes in advance

return threeWeeks;
}
},

mounted() {
axios.get("/api/days/brief").then((response) => {
this.days = response.data;
});
Copy link
Member

Choose a reason for hiding this comment

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

I guess, should be something like this.days = response.data.days;. Also, don't forget to handle an API error if it occurs (i. e. response.data.ok is false). At least give some message about it (I think an alert should be enough for now)

}

});
Copy link
Member

Choose a reason for hiding this comment

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

We love newlines at the ends of files. Add one, please

110 changes: 90 additions & 20 deletions src/templates/index.html
Original file line number Diff line number Diff line change
@@ -1,22 +1,92 @@
{{define "/"}}
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Welcome!</title>
</head>

<body>
<h1>Welcome to the Life Calendar</h1>
<p>
Unfortunately, this page is empty at the moment. Try the following pages: <a href="/login">/login</a>,
<a href="/logout">/logout</a>, <a href="/api">/api</a>/...
<br>
Currently you are
{{if not .}}
not
{{end}}
logged in
</p>
</body>
</html>
<html lang="en">
<title>Welcome to Life Calender!</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Roboto">
<link rel="stylesheet" href="https://fonts.googleapis.com/css?family=Montserrat">
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
<style>
.w3-sidebar a {font-family: "Roboto", sans-serif}
body,h1,h2,h3,h4,h5,h6,.w3-wide {font-family: "Montserrat", sans-serif;}
Copy link
Member

Choose a reason for hiding this comment

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

Should a font name be wrapped in quotes even if it only consists of one word Montserrat

.square {
height: 70px;
width: 70px;
Copy link
Member

Choose a reason for hiding this comment

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

Is it ok that these values are constants? What if the user's screen is smaller than 7 * 70 px?
(there are similar issues among the file, pay attention to them)

background-color: #0b6;
}
</style>
Copy link
Member

Choose a reason for hiding this comment

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

Where is <head>?!


<body class="w3-content" style="max-width:1200px">
Copy link
Member

Choose a reason for hiding this comment

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

Why max-width:1200px?..

{{if not .}}
Unfortunately, you are not logged in now :(<br>
<a href="/login">Log in!</a>
{{else}}
Copy link
Member

Choose a reason for hiding this comment

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

It will look better if you will use if condition ... else ... instead of if not condition ... else ...

<!-- Sidebar/menu -->
<nav class="w3-sidebar w3-bar-block w3-white w3-collapse w3-top" style="z-index:3;width:265px" id="mySidebar">
Copy link
Member

Choose a reason for hiding this comment

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

(not sure) Probably id="mySidebar" shouldn't get into master (there are similar issues among this file)

Copy link
Member

Choose a reason for hiding this comment

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

What is z-index for here? I don't see any other usages of z-index on this page...

<div class="w3-container w3-display-container w3-padding-16">
<i onclick="w3_close()" class="fa fa-remove w3-hide-large w3-button w3-display-topright"></i>
<h3 class="w3-wide"><b>Life Calendar</b></h3>
Copy link
Member

Choose a reason for hiding this comment

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

<h3><b>? Have you done it on purpose?

</div>
<div class="w3-padding-64 w3-large w3-text-grey" style="font-weight:bold">
Copy link
Member

Choose a reason for hiding this comment

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

Not sure, how serious is this, but it should instead be

Suggested change
<div class="w3-padding-64 w3-large w3-text-grey" style="font-weight:bold">
<div class="w3-padding-64 w3-large w3-text-grey" style="font-weight:bold;">

Please, note that this mistake also appears in many lines around. If it should be fixed, it should be fixed everywhere

<a class="w3-bar-item w3-button w3-border-bottom w3-large" href="#">
<img src="https://telegram.org/file/464001801/4/pPObBDJVv-M.32191.png/9963667389a3218249" style="width:26%;">
Egor Filatov
</a>
<a href="/friends" class="w3-bar-item w3-button">Friends</a>
<a href="/analyze" class="w3-bar-item w3-button">Analyze</a>
<a href="/settings" class="w3-bar-item w3-button">Settings</a>
</div>
<a href="/logout" class="w3-bar-item w3-button w3-padding">Logout</a>
</nav>

<!-- Top menu on small screens -->
<header class="w3-bar w3-top w3-hide-large w3-black w3-large">
<div class="w3-bar-item w3-padding-24 w3-wide">Life Calendar</div>
<a href="javascript:void(0)" class="w3-bar-item w3-button w3-padding-24 w3-right" onclick="w3_open()"><i class="fa fa-bars"></i></a>
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
<a href="javascript:void(0)" class="w3-bar-item w3-button w3-padding-24 w3-right" onclick="w3_open()"><i class="fa fa-bars"></i></a>
<a href="javascript:void(0);" class="w3-bar-item w3-button w3-padding-24 w3-right" onclick="w3_open()"><i class="fa fa-bars"></i></a>

, if I am not mistaken

</header>

<!-- Overlay effect when opening sidebar on small screens -->
<div class="w3-overlay w3-hide-large" onclick="w3_close()" style="cursor:pointer" title="close side menu" id="myOverlay"></div>

<!-- !PAGE CONTENT! -->
<div class="w3-main w3-row-padding w3-margin-top" style="margin-left:265px">

<!-- Push down content on small screens -->
<div class="w3-hide-large" style="margin-top:83px"></div>

<!-- Day grid -->
<div id="dayButtons">
<table cols=7 class="w3-table">
<tr v-for="week in getDisplayedWeeks()">
<td v-for="day in week">
<!--<a v-bind:href="getDayUrl(day)">-->
Copy link
Member

Choose a reason for hiding this comment

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

Looks like a debug comment. Should it be removed?

<div class="w3-col square" v-if="isDayFilled(day)"
v-bind:style="{backgroundColor: colorToHex(getFilledDayColor(day))}"></div>
<div class="w3-col square" v-else style="background-color: darkgrey"></div>
<!--</a>-->
</td>
</tr>
</table>
</div>
</div>


<script src="https://unpkg.com/vue"></script>
<script src="https://unpkg.com/axios/dist/axios.min.js"></script>
<script src="/scripts/index.js"></script>
Comment on lines +73 to +75
Copy link
Member

Choose a reason for hiding this comment

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

Shouldn't these scripts be included with <link rel...>s in the <head>?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I'm not sure, because I don't which value of rel is related to scripts or can be used here. If @FixedOctocat (or someone else) knows which one, then maybe these scripts should be moved

Copy link
Member

Choose a reason for hiding this comment

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

@tanya-kta,

  1. The fact you don't know which rel to use should not affect the decision whether this should be moved to <head> or not, of course. I'm asking because I am not closely familiar with the rules of placing scripts on web pages, but I have seen such scripts loaded with the link tag inside of head, that's why I am proposing this change
  2. Answering your question, rel="preload" is usually used for js

<script>
function w3_open() {
document.getElementById("mySidebar").style.display = "block";
document.getElementById("myOverlay").style.display = "block";
}

function w3_close() {
document.getElementById("mySidebar").style.display = "none";
document.getElementById("myOverlay").style.display = "none";
}
Copy link
Member

Choose a reason for hiding this comment

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

These should be renamed, to my mind

</script>
Copy link
Member

Choose a reason for hiding this comment

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

Pretty sure these two scripts should be moved above the place where they are called for several purposes, including code readability

{{end}}
</body>
</html>
{{end}}
11 changes: 11 additions & 0 deletions src/webhandlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,17 @@ func main() {
r.HandleFunc("/api/days/brief", HandleApiDaysBrief).Methods("GET")
//r.HandleFunc("/api/days/{id:[0-9]+}")

//r.Handle("/src", )
Copy link
Member

Choose a reason for hiding this comment

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

I think this is a mistake

r.HandleFunc("/scripts/*", func(w http.ResponseWriter, r *http.Request) {
r.URL.Path = r.URL.Path[len("/scripts"):]
http.FileServer(http.Dir("src/scripts")).ServeHTTP(w, r)
})

Copy link
Member

Choose a reason for hiding this comment

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

This shouldn't be here either... The following handler does everything you want

r.PathPrefix("/scripts/").Handler(http.StripPrefix("/scripts/", http.FileServer(http.Dir("src/scripts")))).
Methods("GET")

// TODO: change style: use `r.Methods(...).Handle...` instead of `r.Handle....Methods(...)`

listenAddr := "localhost:4000"
fmt.Println("Listening at http://" + listenAddr)
panic(http.ListenAndServe(listenAddr, r))
Expand Down