Skip to content

Instantly share code, notes, and snippets.

@WeltonThomasFerreira
Last active January 6, 2023 23:48
Show Gist options
  • Save WeltonThomasFerreira/0a97dc07daeea64cba2a3a7d0005038a to your computer and use it in GitHub Desktop.
Save WeltonThomasFerreira/0a97dc07daeea64cba2a3a7d0005038a to your computer and use it in GitHub Desktop.
// type, model, interface
// type.ts
export interface IUser {
name: string;
lastname: string;
birthday: string | Date;
skills: {
id: number;
value: string;
level: string;
}[]
}
// service
// service.ts
import { IUser } from './../../dont/types/user';
export class UserService {
getUser(): IUser {
return {
name: 'Vinicius',
lastname: 'Marcelino',
birthday: '29-09-2000',
skills: [
{
id: 1,
value: 'CSS',
level: 'pleno'
}]
}
}
}
// Component
// profile.ts
import { UserService } from "../service/service";
import { IUser } from "../types/user";
type ISkill = IUser['skills'][0]
interface ISkillTemplate extends ISkill {
checked: boolean;
}
interface IUserTemplate extends IUser {
fullname: string;
skills: ISkillTemplate[];
}
class Profile {
user: IUserTemplate;
userService: UserService;
constructor(userService: UserService){
this.userService = userService;
this.init();
}
init(){
this.fetchUser();
}
fetchUser(){
const user: IUser = this.userService.getUser();
this.user = this.mapUser(user);
}
mapUser(user: IUser): IUserTemplate {
return {...user, fullname: `${user.name} ${user.lastname}`, skills: this.mapSkills(user.skills)}
}
mapSkills(skills: ISkill[]): ISkillTemplate[] {
return skills.map((skill) => ({...skill, checked: false}))
}
}
// type, model, interface
// type.ts
export interface IUser {
name: string;
lastname: string;
fullname?: string;
birthday: string | Date;
skills: ISkill[]
}
export interface ISkill {
id: number;
value: string;
level: string;
checked?: boolean;
}
// service
// service.ts
export class UserService {
getUser() {
return {
name: 'Vinicius',
lastname: 'Marcelino',
birthday: '29-09-2000',
skills: [
{
id: 1,
value: 'CSS',
level: 'pleno'
}]
}
}
}
// Component
// profile.ts
import { UserService } from "../service/service";
import { IUser } from "../types/user";
class Profile {
user: IUser;
userService: UserService;
constructor(userService: UserService){
this.userService = userService;
this.init();
}
init(){
this.fetchUser();
}
fetchUser(){
const user = this.userService.getUser();
this.user = {...user, fullname: `${user.name} ${user.lastname}`}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment