This repository has been archived by the owner on May 11, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #153 from igorkamyshev/custom-tip
Custom tip
- Loading branch information
Showing
37 changed files
with
525 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
CREATE TABLE public.custom_tip ( | ||
"id" character varying NOT NULL, | ||
"title" character varying NOT NULL, | ||
"text" character varying NOT NULL, | ||
"link" character varying, | ||
"expireAt" timestamp with time zone NOT NULL, | ||
"important" BOOLEAN NOT NULL DEFAULT FALSE | ||
); | ||
|
||
ALTER TABLE ONLY public.custom_tip | ||
ADD CONSTRAINT "PK_custom_tip_id" PRIMARY KEY ("id"); | ||
|
||
#DOWN | ||
|
||
DROP TABLE public.custom_tip; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Injectable } from '@nestjs/common' | ||
|
||
import { EntitySaver } from '@back/db/EntitySaver' | ||
import { CustomTipModel } from '@shared/models/mind/CustomTipModel' | ||
import { IdGenerator } from '@back/utils/infrastructure/IdGenerator/IdGenerator' | ||
|
||
import { DisabledTip } from '../domain/DisabledTip.entity' | ||
import { CustomTip } from '../domain/CustomTip.entity' | ||
|
||
@Injectable() | ||
export class TipsCreator { | ||
public constructor( | ||
private readonly entitySaver: EntitySaver, | ||
private readonly idGenerator: IdGenerator, | ||
) {} | ||
|
||
public async createCustom(fields: CustomTipModel): Promise<void> { | ||
const id = await this.idGenerator.getId() | ||
const { title, text, expireAt, important, link } = fields | ||
|
||
const tip = new CustomTip(id, title, text, expireAt, important, link) | ||
|
||
await this.entitySaver.save(tip) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
import * as md5 from 'md5' | ||
|
||
import { TipModel } from '@shared/models/mind/TipModel' | ||
import { CustomTip } from '@back/mind/domain/CustomTip.entity' | ||
import { CustomTipRepository } from '@back/mind/domain/CustomTipRepository' | ||
import { TipAction } from '@shared/enum/TipAction' | ||
|
||
import { Adviser } from '../../infrastructure/adviser/helpers/Adviser' | ||
import { IsAdviser } from '../../infrastructure/adviser/helpers/IsAdviser' | ||
|
||
@IsAdviser() | ||
export class CustomAdviser implements Adviser { | ||
public constructor(private readonly customTipRepo: CustomTipRepository) {} | ||
|
||
public async giveAdvice(): Promise<TipModel[]> { | ||
const tips = await this.customTipRepo.findActual() | ||
const now = new Date() | ||
|
||
return tips.map(tip => ({ | ||
action: TipAction.Custom, | ||
meta: tip, | ||
token: this.createToken(tip, TipAction.Custom), | ||
date: now, | ||
})) | ||
} | ||
|
||
private createToken(tip: CustomTip, action: TipAction): string { | ||
const payload = { | ||
id: tip.id, | ||
action, | ||
} | ||
|
||
return md5(JSON.stringify(payload)) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import { Column, Entity, PrimaryColumn } from 'typeorm' | ||
|
||
import { CustomTipModel } from '@shared/models/mind/CustomTipModel' | ||
|
||
@Entity() | ||
export class CustomTip implements CustomTipModel { | ||
@PrimaryColumn() | ||
public readonly id: string | ||
|
||
@Column() | ||
public readonly title: string | ||
|
||
@Column() | ||
public readonly text: string | ||
|
||
@Column() | ||
public readonly link?: string | ||
|
||
@Column() | ||
public readonly expireAt: Date | ||
|
||
@Column() | ||
public readonly important: boolean | ||
|
||
public constructor( | ||
id: string, | ||
title: string, | ||
text: string, | ||
expireAt: Date, | ||
important: boolean = false, | ||
link?: string, | ||
) { | ||
this.id = id | ||
this.title = title | ||
this.text = text | ||
this.expireAt = expireAt | ||
this.important = important | ||
this.link = link | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { Injectable } from '@nestjs/common' | ||
import { InjectRepository } from '@nestjs/typeorm' | ||
import { Repository } from 'typeorm' | ||
|
||
import { CustomTip } from './CustomTip.entity' | ||
|
||
@Injectable() | ||
class CustomTipRepo { | ||
public constructor( | ||
@InjectRepository(CustomTip) | ||
private readonly customTipRepo: Repository<CustomTip>, | ||
) {} | ||
|
||
public async findActual(): Promise<CustomTip[]> { | ||
const now = new Date().toISOString() | ||
|
||
return this.customTipRepo | ||
.createQueryBuilder('tip') | ||
.where('tip.expireAt >= :now', { now }) | ||
.getMany() | ||
} | ||
} | ||
|
||
export const CustomTipRepository = CustomTipRepo | ||
export type CustomTipRepository = CustomTipRepo |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
20 changes: 20 additions & 0 deletions
20
back/src/mind/presentation/http/request/CustomTipRequest.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { ApiModelProperty } from '@nestjs/swagger' | ||
|
||
import { CustomTipModel } from '@shared/models/mind/CustomTipModel' | ||
|
||
export class CustomTipRequest implements CustomTipModel { | ||
@ApiModelProperty({ example: 'Alert' }) | ||
public readonly title: string | ||
|
||
@ApiModelProperty({ example: 'All cool' }) | ||
public readonly text: string | ||
|
||
@ApiModelProperty({ example: 'https://google.com', required: false }) | ||
public readonly link?: string | ||
|
||
@ApiModelProperty({ example: new Date() }) | ||
public readonly expireAt: Date | ||
|
||
@ApiModelProperty({ example: true }) | ||
public readonly important: boolean | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,11 @@ | ||
import * as React from 'react' | ||
|
||
import { Manager } from '@front/features/manager' | ||
|
||
export default class HisotryPage extends React.Component { | ||
public static isSecure = true | ||
|
||
public render() { | ||
return <p>MANAGER</p> | ||
return <Manager /> | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { fetchOrFail } from '@front/domain/store' | ||
import { CustomTipModel } from '@shared/models/mind/CustomTipModel' | ||
|
||
import { createTipRequest } from '../api/createTipRequest' | ||
import { actions as tipFetchingActions } from '../reducer/createTipFetching' | ||
|
||
export const createTip = (tip: CustomTipModel) => | ||
fetchOrFail(tipFetchingActions, async (_, getApi) => { | ||
await createTipRequest(getApi())(tip) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { Api } from '@front/domain/api' | ||
import { CustomTipModel } from '@shared/models/mind/CustomTipModel' | ||
|
||
export const createTipRequest = (api: Api) => ( | ||
tip: CustomTipModel, | ||
): Promise<void> => | ||
api.client.post('/mind/tip/create', tip).then(response => response.data) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
import { createFetchingRedux, FetchingState } from 'redux-clear' | ||
|
||
type State = FetchingState | ||
|
||
const { reducer, actions } = createFetchingRedux('manager/create-tip-fetching') | ||
|
||
export { reducer, actions, State } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,19 @@ | ||
import { combineReducers } from 'redux' | ||
|
||
import { reducer as tipsReducer, State as TipsState } from './tips' | ||
import { | ||
reducer as createTipFetchingReducer, | ||
State as CreateTipTipState, | ||
} from './createTipFetching' | ||
|
||
interface State { | ||
tips: TipsState | ||
createTipFetching: CreateTipTipState | ||
} | ||
|
||
const reducer = combineReducers<State>({ | ||
tips: tipsReducer, | ||
createTipFetching: createTipFetchingReducer, | ||
}) | ||
|
||
export { reducer, State } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
import { State } from '@front/domain/store' | ||
|
||
export const getCreateTipFetching = (state: State) => | ||
state.mind.createTipFetching |
23 changes: 23 additions & 0 deletions
23
front/src/features/app/features/now/tips/custom/Custom.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { TipModel } from '@shared/models/mind/TipModel' | ||
import { Card } from '@front/ui/components/layout/card' | ||
|
||
import { CustomMeta } from './CustomMeta' | ||
import { DismissButton } from '../components/dismiss-button' | ||
import { ExternalLink } from '@front/ui/components/controls/external-link' | ||
|
||
interface Props { | ||
tip: TipModel<CustomMeta> | ||
} | ||
|
||
export const Custom = ({ tip: { meta, token } }: Props) => { | ||
const { title, text, link, important } = meta | ||
|
||
const actions = !!link && <ExternalLink href={link}>Open</ExternalLink> | ||
const dismiss = important && <DismissButton token={token} /> | ||
|
||
return ( | ||
<Card title={title} extra={dismiss} actions={[actions].filter(Boolean)}> | ||
<p>{text}</p> | ||
</Card> | ||
) | ||
} |
3 changes: 3 additions & 0 deletions
3
front/src/features/app/features/now/tips/custom/CustomMeta.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { CustomTipModel } from '@shared/models/mind/CustomTipModel' | ||
|
||
export type CustomMeta = CustomTipModel |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export { Custom } from './Custom' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { Field, FieldRenderProps } from 'react-final-form' | ||
import { Diff } from 'utility-types' | ||
|
||
import { | ||
Checkbox as JustCheckbox, | ||
CheckboxProps, | ||
} from '@front/ui/components/form/checkbox' | ||
|
||
interface OwnProps { | ||
name: string | ||
} | ||
|
||
type ComponentProps = Diff<CheckboxProps, FieldRenderProps['input']> | ||
|
||
export const Checkbox = ({ | ||
name, | ||
...componentProps | ||
}: OwnProps & ComponentProps) => ( | ||
<Field | ||
name={name} | ||
render={({ input }) => <JustCheckbox {...componentProps} {...input} />} | ||
/> | ||
) |
Oops, something went wrong.