Files
portfolio-admin/src/app/admin/state/admin-state.service.ts
T

141 lines
4.5 KiB
TypeScript

import { Injectable, inject } from '@angular/core';
import { tap } from 'rxjs';
import { AdminStore, AdminSection } from './admin.store';
import { AdminService } from '../services/admin.service';
import { IAbout } from '../about/about.model';
import { IHobby } from '../models/hobby.model';
import { IContactModel } from '../contact/contact.model';
import { IProjects } from '../projects/projects.model';
import { IResume } from '../resume/resume.model';
@Injectable({ providedIn: 'root' })
export class AdminStateService {
private store = inject(AdminStore);
private api = inject(AdminService);
// ── About ──────────────────────────────────────────────
loadAbout() {
this.store.setSectionLoading('about', true);
return this.api.getHobbies().pipe(
tap({
next: (about: IAbout) => {
this.store.update({ about });
this.store.setSectionLoading('about', false);
this.store.setSectionError('about', null);
},
error: (err) => {
this.store.setSectionLoading('about', false);
this.store.setSectionError('about', err.message);
}
})
);
}
updateAbout(about: IAbout) {
this.store.update({ about });
}
upsertHobbies(hobbies: IHobby[]) {
this.store.setSectionLoading('about', true);
return this.api.upsertHobbies(hobbies).pipe(
tap({
next: (updatedHobbies: IHobby[]) => {
this.store.update(state => ({
about: state.about ? { ...state.about, hobbies: updatedHobbies } : state.about
}));
this.store.setSectionLoading('about', false);
this.store.setSectionError('about', null);
},
error: (err) => {
this.store.setSectionLoading('about', false);
this.store.setSectionError('about', err.message);
}
})
);
}
// ── Contact ────────────────────────────────────────────
loadContact() {
this.store.setSectionLoading('contact', true);
return this.api.getCandidateWithSocialLinks().pipe(
tap({
next: (contact: IContactModel) => {
this.store.update({ contact });
this.store.setSectionLoading('contact', false);
this.store.setSectionError('contact', null);
},
error: (err) => {
this.store.setSectionLoading('contact', false);
this.store.setSectionError('contact', err.message);
}
})
);
}
updateContact(contact: IContactModel) {
this.store.update({ contact });
}
// ── Projects ───────────────────────────────────────────
loadProjects() {
this.store.setSectionLoading('projects', true);
return this.api.getProjects().pipe(
tap({
next: (projects: IProjects) => {
this.store.update({ projects });
this.store.setSectionLoading('projects', false);
this.store.setSectionError('projects', null);
},
error: (err) => {
this.store.setSectionLoading('projects', false);
this.store.setSectionError('projects', err.message);
}
})
);
}
updateProjects(projects: IProjects) {
this.store.update({ projects });
}
// ── Resume ─────────────────────────────────────────────
loadResume() {
this.store.setSectionLoading('resume', true);
return this.api.getResume().pipe(
tap({
next: (resume: IResume) => {
this.store.update({ resume });
this.store.setSectionLoading('resume', false);
this.store.setSectionError('resume', null);
},
error: (err) => {
this.store.setSectionLoading('resume', false);
this.store.setSectionError('resume', err.message);
}
})
);
}
updateResume(resume: IResume) {
this.store.update({ resume });
}
// ── Reset helpers ──────────────────────────────────────
resetSection(section: AdminSection) {
this.store.update(state => ({
[section]: null,
loading: { ...state.loading, [section]: false },
error: { ...state.error, [section]: null }
}));
}
resetAll() {
this.store.reset();
}
}