Skip to content

Commit

Permalink
Show list of dates in home tab
Browse files Browse the repository at this point in the history
  • Loading branch information
joonashak committed Aug 8, 2023
1 parent 1c38424 commit 06c7eb5
Show file tree
Hide file tree
Showing 8 changed files with 83 additions and 18 deletions.
20 changes: 14 additions & 6 deletions app-nest/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 4 additions & 2 deletions app-nest/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,11 @@
"@nestjs/platform-express": "^10.0.0",
"@nestjs/typeorm": "^10.0.0",
"@slack/bolt": "^3.13.2",
"dayjs": "^1.11.9",
"lodash": "^4.17.21",
"pg": "^8.11.1",
"reflect-metadata": "^0.1.13",
"rxjs": "^7.8.1",
"slack-block-builder": "^2.7.2",
"typeorm": "^0.3.17"
},
"devDependencies": {
Expand All @@ -48,6 +49,7 @@
"@nestjs/testing": "^10.0.0",
"@types/express": "^4.17.17",
"@types/jest": "^29.5.2",
"@types/lodash": "^4.14.196",
"@types/node": "^20.3.1",
"@types/supertest": "^2.0.12",
"@typescript-eslint/eslint-plugin": "^5.59.11",
Expand Down Expand Up @@ -81,4 +83,4 @@
"coverageDirectory": "../coverage",
"testEnvironment": "node"
}
}
}
2 changes: 1 addition & 1 deletion app-nest/src/gui/gui.module.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Module } from "@nestjs/common";
import { UserModule } from "../entities/user/user.module";
import { UserService } from "../entities/user/user.service";
import { HomeTabController } from "./tabs/home-tab.controller";
import { HomeTabController } from "./tabs/home/home-tab.controller";

@Module({
imports: [UserModule],
Expand Down
33 changes: 33 additions & 0 deletions app-nest/src/gui/tabs/home/day-list.blocks.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import dayjs, { Dayjs } from "dayjs";
import { flatten } from "lodash";
import getDayListItemBlocks from "./day-list.item.blocks";

/**
* Get range of days from today (inclusive) for the next `len` working days (defined as Mon-Fri).
*/
const dayRange = (len: number, days: Dayjs[] = [], i = 0): Dayjs[] => {
if (i >= len) {
return days;
}

if (days.length === 0) {
days.push(dayjs());
return dayRange(len, days, i + 1);
}

const prevDate = days.at(-1);
const prevDay = prevDate.day();
// Skip Saturdays (day 6) and Sundays (day 0).
const daysToAdd = prevDay === 5 ? 3 : prevDay === 6 ? 2 : 1;
days.push(prevDate.add(daysToAdd, "d"));

return dayRange(len, days, i + 1);
};

const getDayListBlocks = () => {
const dates = dayRange(14);
const blockLists = dates.map((date) => getDayListItemBlocks({ date }));
return flatten(blockLists);
};

export default getDayListBlocks;
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import devTools from "../dev/dev-tools";
import { Dayjs } from "dayjs";

const getHomeTabBlocks = () => [
...devTools,
type DayListItemProps = {
date: Dayjs;
};

const getDayListItemBlocks = ({ date }: DayListItemProps) => [
{
type: "header",
text: {
type: "plain_text",
text: "Ilmoittautumiset",
text: date.format("dd D.M."),
},
},
{
Expand Down Expand Up @@ -75,4 +78,4 @@ const getHomeTabBlocks = () => [
},
];

export default getHomeTabBlocks;
export default getDayListItemBlocks;
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Controller } from "@nestjs/common";
import BoltEvent from "../../bolt/decorators/bolt-event.decorator";
import BoltEvents from "../../bolt/enums/bolt-events.enum";
import { AppHomeOpenedArgs } from "../../bolt/types/bolt-event-types";
import { UserService } from "../../entities/user/user.service";
import BoltEvent from "../../../bolt/decorators/bolt-event.decorator";
import BoltEvents from "../../../bolt/enums/bolt-events.enum";
import { AppHomeOpenedArgs } from "../../../bolt/types/bolt-event-types";
import { UserService } from "../../../entities/user/user.service";
import getDayListBlocks from "./day-list.blocks";
import getHomeTabBlocks from "./home-tab.view";

@Controller()
Expand All @@ -13,6 +14,7 @@ export class HomeTabController {
async getView({ event, client, logger }: AppHomeOpenedArgs) {
const users = await this.userService.findAll();
const { slackId } = users[0];
getDayListBlocks();

try {
const result = await client.views.publish({
Expand Down
16 changes: 16 additions & 0 deletions app-nest/src/gui/tabs/home/home-tab.view.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import devTools from "../../dev/dev-tools";
import getDayListBlocks from "./day-list.blocks";

const getHomeTabBlocks = () => [
...devTools,
{
type: "header",
text: {
type: "plain_text",
text: "Ilmoittautumiset",
},
},
...getDayListBlocks(),
];

export default getHomeTabBlocks;
1 change: 1 addition & 0 deletions app-nest/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
{
"compilerOptions": {
"module": "commonjs",
"esModuleInterop": true,
"declaration": true,
"removeComments": true,
"emitDecoratorMetadata": true,
Expand Down

0 comments on commit 06c7eb5

Please sign in to comment.