Commit d6c50dc1 authored by Live Jortveit Sagebakken's avatar Live Jortveit Sagebakken
Browse files

(#3) Filter commits, earlier weeks and this month

parent 99b13c91
import { useEffect, useState } from "react";
import Commit from "./commit";
import DatePicker from "react-datepicker";
  • Glemte å fjerne denne, men gjør det ved neste commit! Fant ut at datepicker er unødvendig her, siden er jo bare commits fra september og fram til 3. oktober, så lite vits å ha med så mange valgmuligheter.

Please register or sign in to reply
function CommitView() {
// eslint-disable-next-line react-hooks/rules-of-hooks
const [commits, setCommits] = useState([]);
let token = "3Nhst8cnMWx5puz8KEzR";
const [filterParam, setFilterParam] = useState("all");
// eslint-disable-next-line react-hooks/rules-of-hooks
useEffect(() => {
// Kode hentet fra https://www.freecodecamp.org/news/fetch-data-react/
......@@ -37,13 +39,122 @@ function CommitView() {
function getCurrentDate() {
var dateObject = new Date();
var day = dateObject.getDate();
var month = dateObject.getMonth() + 1; // Must add 1 as January is equal to 0 if not.
var correctFormatMonth = month < 10 ? "0" + month : month; // Must add 0 if month earlier than october to get correct format.
var month = dateObject.getMonth() + 1; // Must add 1 as January is equal to 0 if not
var correctFormatMonth = month < 10 ? "0" + month : month; // Must have 0 if month earlier than october to get correct dateformat
var year = dateObject.getFullYear();
var separator = "-";
var displayDate = year + separator + correctFormatMonth + separator + day;
return displayDate;
}
function getCurrentDay(){
return getCurrentDate().substring(8,10);
}
function getCurrentMonth(){
return getCurrentDate().substring(5,7);
}
function getCurrentYear(){
return getCurrentDate().substring(0,4);
}
function getDay(date: string){
return date.substring(8,10);
}
function getMonth(date: string){
return date.substring(5,7);
}
function getYear(date: string){
return date.substring(0,4);
}
function getStartDate(){
var separator = "-";
if(filterParam === "lastWeek"){
return getCurrentYear() + separator + getCurrentMonth() + separator + (parseInt(getCurrentDay()) - 7);
}
else if(filterParam === "lastTwoWeeks") {
return getCurrentYear() + separator + getCurrentMonth() + separator + (parseInt(getCurrentDay()) - 14);
}
else if(filterParam === "lastThreeWeeks" ) {
return getCurrentYear() + separator + getCurrentMonth() + separator + (parseInt(getCurrentDay()) - 21);
}
else if(filterParam === "thisMonth") {
return getCurrentYear() + separator + getCurrentMonth() + separator + "01";
}
else {
return "No commits"
}
}
function getMonthLength(month: number) {
if(month === 2){
return 28; // Dont care for skuddår right now
}
else if(month === 4 || month === 6 || month === 9 || month === 12) {
return 30;
}
else {
return 31;
}
}
function getDayDifference(month: number) {
if(filterParam === "lastWeek"){
return 7;
}
else if (filterParam === "lastTwoWeeks"){
return 14;
}
else if(filterParam === "lastThreeWeeks") {
return 21;
}
else if(filterParam === "thisMonth"){
return getMonthLength(month);
}
else {
return 0;
}
}
function filterOnWeeks(){
var startDate = getStartDate();
console.log(startDate);
var startDay = parseInt(getDay(startDate.toString()));
var month = parseInt(getMonth(startDate.toString()));
var year = getYear(startDate.toString());
var separator = "-";
var commitsList: any = [];
var dayDifference = getDayDifference(month);
var dateFormat = "";
console.log("DayDifference:" + dayDifference);
for(var i = 0; i <= dayDifference; i++) {
dateFormat = (year + separator + (month < 10 ? "0" + (month) : (month)) + separator + (startDay < 10 ? "0" + (startDay) : (startDay)));
console.log("DateFormat:" + dateFormat);
console.log("StartDay:" + startDay);
console.log()
console.log("Month:" + month)
if(startDay === getMonthLength(month) && month.toString().includes(month.toString())) {
Please register or sign in to reply
startDay = 0;
month ++;
}
console.log(startDay);
commitsList.push(commits.filter((issue: {committed_date: string}) =>
issue.committed_date.includes(dateFormat)).map(
(commit: { message: string; created_at: number }, index: React.Key) => (
<Commit
key={index}
title={commit.message}
date={commit.created_at}
/>)))
startDay += 1;
}
return commitsList;
}
function displayCommits(){
if(filterParam === "all"){
......@@ -62,9 +173,10 @@ function CommitView() {
key={index}
title={commit.message}
date={commit.created_at}
/>))}
else{
return "No commits";
/>))
}
else {
return filterOnWeeks();
}
}
......@@ -79,10 +191,11 @@ function CommitView() {
<option value="lastWeek">Last week</option>
<option value="lastTwoWeeks">Two weeks ago</option>
<option value="lastThreeWeeks">Three weeks ago</option>
<option value="lastMonth">Last month</option>
<option value="thisMonth">This month</option>
</select>
</label>
{displayCommits()}
</div>
);
}
......
  • Logikken er ikke helt feilfri enda, ettersom det er mulig å få negativ verdi for startdato o.l., men skal fikse det til i neste commit!

Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment