Skip to content

Commit

Permalink
check min read time before page complete
Browse files Browse the repository at this point in the history
  • Loading branch information
ortwic committed Mar 28, 2024
1 parent b9631c0 commit a2d362d
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 17 deletions.
2 changes: 1 addition & 1 deletion src/app/models/blog.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export interface BlogPost {
images: PostImage[];
content: BlogContent[];
tags: string[];
status: 'published' | 'draft';
status: 'published' | 'beta' | 'draft';
publish_date: Date;
created_on: Date;
}
Expand Down
2 changes: 1 addition & 1 deletion src/app/models/content.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ export interface FormContent {
}

export type InputDefinition = SelectList | Textarea;
export type InputValue = string[] | string | boolean | undefined;
export type InputValue = string[] | string | number | boolean | undefined;

interface SelectList {
type: 'select';
Expand Down
7 changes: 4 additions & 3 deletions src/app/models/page.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,19 +6,20 @@ export interface Page {
slug: string;
hero_section: HeroSection,
content: PageContent[];
sidebar: {
sidebar?: {
title: string;
content: string;
},
seo_metadata: {
seo_metadata?: {
meta_title: string;
meta_description: string;
focus_keywords: string;
},
footer_override: string;
publish_date: Date;
last_updated: Date;
is_published: boolean;
status: 'published' | 'beta' | 'draft';
min_read_time: number;
}

export interface HeroSection {
Expand Down
31 changes: 20 additions & 11 deletions src/app/pages/page/page.component.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { CommonModule } from '@angular/common';
import { Component, inject } from '@angular/core';
import { Component, inject, isDevMode } from '@angular/core';
import { FormsModule } from '@angular/forms';
import { ActivatedRoute, RouterModule } from '@angular/router';
import { MatButtonModule } from '@angular/material/button';
Expand All @@ -11,7 +11,7 @@ import { ImageSliderComponent } from '../../components/image-slider/image-slider
import { InputSectionComponent } from '../../components/input-section/input-section.component';
import { ContinueEventArgs, InputStepperComponent } from '../../components/input-stepper/input-stepper.component';
import { GuideService } from '../../services/guide.service';
import { UserDataService, pageDoneKey } from '../../services/user-data.service';
import { UserDataService, pageReadTime } from '../../services/user-data.service';
import { SafeUrlPipe } from '../../pipes/safe-url.pipe';
import { MarkedPipe } from '../../pipes/marked.pipe';
import { PageContent } from '../../models/page.model';
Expand Down Expand Up @@ -40,8 +40,10 @@ import { InputValue } from '../../models/content.model';
animations: [ expandTrigger('next') ],
})
export class PageComponent {
private readonly _route = inject(ActivatedRoute);
private _route = inject(ActivatedRoute);
readonly unitIndex = +this._route.snapshot.params['unit'];
private _startTime!: number;
private _minReadTime!: number;

private _step = 0;
continue!: (args: ContinueEventArgs) => void;
Expand Down Expand Up @@ -69,7 +71,11 @@ export class PageComponent {
});
}),
tap(page => this.continue = this.initBreakpoints(page.content, page.slug)),
tap(page => document.title = page.title + " | Why App")
tap(page => document.title = page.title + " | Why App"),
tap(page => {
this._startTime = Date.now();
this._minReadTime = !isDevMode() ? page.min_read_time : .1;
})
);

private initBreakpoints(content: PageContent[], pageId: string) {
Expand Down Expand Up @@ -97,12 +103,15 @@ export class PageComponent {
}

complete(data: Record<string, InputValue>) {
this.continue({
completed: true,
data: {
...data,
[pageDoneKey]: true
}
});
const elapsedTime = Date.now() - this._startTime;
if (elapsedTime > this._minReadTime * 60 * 1000) {
this.continue({
completed: true,
data: {
...data,
[pageReadTime]: elapsedTime
}
});
}
}
}
2 changes: 1 addition & 1 deletion src/app/services/user-data.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { Injectable, Signal, computed, signal } from '@angular/core';

export const localKey = 'user-data';
export const pageDoneKey = '__page-done';
export const pageReadTime = '__page-read-in';

function startDownload(url: string, filename: string) {
const link = document.createElement('a');
Expand Down

0 comments on commit a2d362d

Please sign in to comment.