generated from yandex-praktikum/middle.messenger.praktikum.yandex
-
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.
Add Block realisation, add validation, add HTTPTransport
- Loading branch information
Showing
54 changed files
with
3,080 additions
and
195 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,12 @@ | ||
{ | ||
"plugins": ["stylelint-less"], | ||
"rules": { | ||
"at-rule-no-unknown": null, | ||
"color-no-invalid-hex": true, | ||
"less/color-no-invalid-hex": true | ||
}, | ||
"ignoreFiles": [ | ||
"dist/*", | ||
"node_modules/*" | ||
] | ||
} |
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,12 @@ | ||
export default [ | ||
{ | ||
files: ["src/**/*.ts"], | ||
rules: { | ||
"max-len": [2, 100], | ||
"max-params": [2, 3], | ||
"no-console": "off", | ||
"eol-last": ['error', 'always'], | ||
}, | ||
ignores: ["dist", "node_modules"], | ||
} | ||
]; |
Large diffs are not rendered by default.
Oops, something went wrong.
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,16 @@ | ||
import './button.less'; | ||
import Block, {Props} from '../../core/Block'; | ||
import ButtonTmpl from './button.hbs?raw'; | ||
|
||
export class Button extends Block { | ||
constructor(props: Props) { | ||
super({...props}); | ||
this.props.events = { | ||
click: this.props.onClick || (() => {}), | ||
}; | ||
} | ||
|
||
render(): string { | ||
return ButtonTmpl; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
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,6 +1,6 @@ | ||
<div class="error container"> | ||
<h1 class="error title">{{errorTitle}}</h1> | ||
<span class="error text">{{errorText}} | ||
{{> Button class="error back-button" label="Chat" page="chatPage" }} | ||
{{{ Button class="error back-button" label="Chat" page="ChatPage" }}} | ||
</span> | ||
</div> |
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,13 @@ | ||
import './error.less'; | ||
import Block, {Props} from "../../core/Block"; | ||
import ErrorTmpl from './error.hbs?raw'; | ||
|
||
export class Error extends Block { | ||
constructor(props: Props) { | ||
super(props); | ||
} | ||
|
||
render(): string { | ||
return ErrorTmpl; | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
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,2 +1,12 @@ | ||
<label for="{{ name }}">{{ label }}</label> | ||
<input type="{{ type }}" name="{{ name }}" class="input {{ class }}" placeholder="{{ placeholder }}" value="{{ value }}" /> | ||
<div class="input-wrapper {{#if hasError}}has-error{{/if}}"> | ||
<label for="{{ name }}">{{ label }}</label> | ||
<input | ||
type="{{ type }}" | ||
name="{{ name }}" | ||
class="input {{ class }}" | ||
placeholder="{{ placeholder }}" | ||
value="{{ value }}" | ||
{{#if autocomplete}}autocomplete="{{ autocomplete }}"{{/if}} | ||
/> | ||
<div class="error-message">{{ error }}</div> | ||
</div> |
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,68 @@ | ||
import Block, { Props } from '../../core/Block'; | ||
import template from './input.hbs?raw'; | ||
import { validateField } from '../../utils/validation'; | ||
|
||
export class Input extends Block { | ||
constructor(props: Props) { | ||
super({ | ||
...props, | ||
value: props.value || '', | ||
error: '', | ||
hasError: false, | ||
events: { | ||
blur: (e: FocusEvent) => { | ||
this.validate(); | ||
if (typeof this.props.onBlur === 'function') { | ||
this.props.onBlur(e); | ||
} | ||
}, | ||
input: (e: Event) => { | ||
const input = e.target as HTMLInputElement; | ||
this.props.value = input.value; | ||
} | ||
} | ||
}); | ||
} | ||
|
||
public setValue(value: string): void { | ||
this.setProps({ value }); | ||
} | ||
|
||
public getValue(): unknown { | ||
return this.props.value; | ||
} | ||
|
||
public validate(): boolean { | ||
const errorMessage: string | null = validateField(<string>this.props.name, <string>this.props.value); | ||
const hasError: boolean = !!errorMessage; | ||
this.setProps({ | ||
error: errorMessage, | ||
hasError: hasError | ||
}); | ||
console.log(!!errorMessage); | ||
return !errorMessage; | ||
} | ||
|
||
render(): string { | ||
return template; | ||
} | ||
|
||
protected componentDidUpdate(oldProps: any, newProps: any): boolean { | ||
if (this._element) { | ||
if (newProps.hasError !== oldProps.hasError) { | ||
this._element.classList.toggle('has-error', newProps.hasError); | ||
} | ||
|
||
const errorElement = this._element.querySelector('.error-message'); | ||
if (errorElement) { | ||
errorElement.textContent = newProps.error || ''; | ||
} | ||
|
||
const inputElement = this._element.querySelector('input') as HTMLInputElement; | ||
if (inputElement && inputElement.value !== newProps.value) { | ||
inputElement.value = newProps.value; | ||
} | ||
} | ||
return false; | ||
} | ||
} |
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,53 @@ | ||
import Block, { Props } from './Block'; | ||
import { Input } from '../components/input/input'; | ||
|
||
export class BaseForm extends Block { | ||
constructor(props: Props) { | ||
super({ | ||
...props, | ||
events: { | ||
submit: (event: Event) => { | ||
event.preventDefault(); | ||
this.onSubmit(); | ||
}, | ||
}, | ||
}); | ||
} | ||
|
||
protected onSubmit() { | ||
const formData: Record<string, string> = {}; | ||
let isValid: boolean = true; | ||
|
||
Object.values(this.children).forEach((child: Block | Element) => { | ||
if (child instanceof Input) { | ||
const { name } = child.props; | ||
formData[name] = child.getValue(); | ||
if (!child.validate()) { | ||
isValid = false; | ||
} | ||
} | ||
}); | ||
|
||
if (isValid) { | ||
this.onValid(formData); | ||
} else { | ||
this.onInvalid(); | ||
} | ||
} | ||
|
||
protected onValid(formData: Record<string, string>) { | ||
// This method should be overridden in child classes | ||
} | ||
|
||
protected onInvalid() { | ||
// This method can be overridden in child classes if needed | ||
} | ||
|
||
protected navigate(page: string) { | ||
const event = new CustomEvent('navigate', { | ||
bubbles: true, | ||
detail: { page } | ||
}); | ||
document.dispatchEvent(event); | ||
} | ||
} |
Oops, something went wrong.