Skip to content

Commit

Permalink
feat: also order projects by name
Browse files Browse the repository at this point in the history
Closes #79.
  • Loading branch information
williamboman committed Apr 27, 2023
1 parent 80054bd commit 6da6da8
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 10 deletions.
6 changes: 3 additions & 3 deletions dashboard/src/components/projects.js
Original file line number Diff line number Diff line change
Expand Up @@ -463,7 +463,7 @@ export default {
const toolbar = m("div.d-sm-flex.mb-4", [
m(
"h1.h3.mb-0.mr-auto.text-gray-800",
LocalState.currentProject ? LocalState.currentProject : "Projects"
LocalState.currentProject ? LocalState.currentProject.name : "Projects"
),
m("div.dropdown.mr-2", [
m(
Expand All @@ -480,9 +480,9 @@ export default {
return m(
"a.btn.dropdown-item",
{
onclick: LocalState.fetchProjectStats
onclick: () => { LocalState.fetchProjectStats(project) }
},
project
project.name
);
})
)
Expand Down
25 changes: 18 additions & 7 deletions dashboard/src/models/ProjectState.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,17 @@ import TimeRange from "./TimeRange";
import utils from "../utils.js";
import * as auth from "../auth";

/**
* @typedef {Object} Project
* @property {string} name
* @property {string} desc
* @property {number} totalSeconds
*/

const Model = {
/** @type {Project[]} */
projects: [],
/** @type {Project?} */
currentProject: null,
dates: null,
obj: null,
Expand All @@ -16,27 +25,29 @@ const Model = {
Model.dates = null;
Model.obj = null;
},
/** @param {Project[]} projects */
initProjectList: projects => {
Model.projects = _.orderBy(projects, ["totalSeconds"], ["desc"])
.map(p => p.name)
.filter(n => n !== "Other");
Model.projects = _.orderBy(projects, ["totalSeconds", "name"], ["desc", "asc"])
.filter(n => n.name !== "Other")
.sort((a, b) => a.name.localeCompare(b.name));

if (Model.projects.length > 0) {
Model.currentProject = Model.projects[0];
}

Model.fetchProjectStats();
},
fetchProjectStats: event => {
/** @param {Project?} project */
fetchProjectStats: project => {
// If it was triggered by a click event.
if (event) Model.currentProject = event.target.innerHTML;
if (project) Model.currentProject = project

const start = new Date();
const today = new Date();
start.setDate(start.getDate() - TimeRange.numOfDays);

m.request({
url: `/api/v1/users/current/projects/${Model.currentProject}`,
url: `/api/v1/users/current/projects/${Model.currentProject.name}`,
responseType: "json",
headers: {
authorization: auth.getHeaderToken()
Expand All @@ -54,7 +65,7 @@ const Model = {
new Date(obj.endDate)
);
})
.catch(err => auth.retryCall(err, () => Model.fetchProjectStats(event)));
.catch(err => auth.retryCall(err, () => Model.fetchProjectStats(project)));
}
};

Expand Down

0 comments on commit 6da6da8

Please sign in to comment.