fix job update

parent 53209eb3
...@@ -287,7 +287,6 @@ export class DashboardComponent implements OnInit { ...@@ -287,7 +287,6 @@ export class DashboardComponent implements OnInit {
console.log('Jobs loaded successfully from API'); console.log('Jobs loaded successfully from API');
}, },
error: (error) => { error: (error) => {
console.error('Failed to load jobs from API:', error);
this.messageService.error(`Failed to load jobs: ${error.message}`); this.messageService.error(`Failed to load jobs: ${error.message}`);
} }
}); });
...@@ -320,7 +319,6 @@ export class DashboardComponent implements OnInit { ...@@ -320,7 +319,6 @@ export class DashboardComponent implements OnInit {
viewJobDetail(job: Job): void { viewJobDetail(job: Job): void {
this.selectedJob = job; this.selectedJob = job;
this.showDetailModal = true; this.showDetailModal = true;
console.log('Opening job detail for:', job.title);
this.cdr.markForCheck(); this.cdr.markForCheck();
} }
...@@ -371,10 +369,8 @@ export class DashboardComponent implements OnInit { ...@@ -371,10 +369,8 @@ export class DashboardComponent implements OnInit {
} }
closeDetailModal(): void { closeDetailModal(): void {
console.log('Closing detail modal...');
this.showDetailModal = false; this.showDetailModal = false;
this.selectedJob = null; this.selectedJob = null;
console.log('Detail modal closed, state reset');
this.cdr.markForCheck(); this.cdr.markForCheck();
} }
...@@ -391,15 +387,18 @@ export class DashboardComponent implements OnInit { ...@@ -391,15 +387,18 @@ export class DashboardComponent implements OnInit {
this.cdr.markForCheck(); this.cdr.markForCheck();
if (this.isEditMode && this.selectedJob) { if (this.isEditMode && this.selectedJob) {
// Update job
console.log('Updating job:', this.selectedJob.id, 'with data:', jobData);
this.jobApiService.updateJob({ id: this.selectedJob.id, job: jobData }).subscribe({ this.jobApiService.updateJob({ id: this.selectedJob.id, job: jobData }).subscribe({
next: (response) => { next: (response) => {
console.log('Update job response:', response); if (response.success && this.selectedJob) {
if (response.success) { const updatedJob: Job = {
this.jobStateService.updateJob(response.responseData.job); ...this.selectedJob,
this.messageService.success(`Job "${response.responseData.job.title}" updated successfully`); ...response.responseData.job,
id: this.selectedJob.id,
created_at: this.selectedJob.created_at
};
this.jobStateService.updateJob(updatedJob);
this.messageService.success(`Job "${updatedJob.title}" updated successfully`);
this.closeJobFormModal(); this.closeJobFormModal();
this.cdr.markForCheck(); this.cdr.markForCheck();
} else { } else {
...@@ -409,22 +408,18 @@ export class DashboardComponent implements OnInit { ...@@ -409,22 +408,18 @@ export class DashboardComponent implements OnInit {
} }
}, },
error: (error) => { error: (error) => {
console.error('Update job error:', error);
this.messageService.error(`Failed to update job: ${error.message}`); this.messageService.error(`Failed to update job: ${error.message}`);
this.jobFormLoading.set(false); this.jobFormLoading.set(false);
this.cdr.markForCheck(); this.cdr.markForCheck();
} }
}); });
} else { } else {
// Create new job
this.jobApiService.createJob({ job: jobData }).subscribe({ this.jobApiService.createJob({ job: jobData }).subscribe({
next: (response) => { next: (response) => {
if (response.success) { if (response.success) {
console.log('Job created successfully, response:', response);
this.jobStateService.addJob(response.responseData.job); this.jobStateService.addJob(response.responseData.job);
this.messageService.success(`Job "${response.responseData.job.title}" created successfully`); this.messageService.success(`Job "${response.responseData.job.title}" created successfully`);
this.closeJobFormModal(); this.closeJobFormModal();
this.cdr.markForCheck(); this.cdr.markForCheck();
setTimeout(() => { setTimeout(() => {
......
...@@ -9,8 +9,8 @@ export interface Job { ...@@ -9,8 +9,8 @@ export interface Job {
location: string; location: string;
type: JobType; type: JobType;
created_at: string; created_at: string;
how_to_apply?: string; how_to_apply?: string;
company_logo?: string | null; company_logo?: string;
} }
export type JobLocation = 'Viet Nam' | 'Lao' | 'Campuchia'; export type JobLocation = 'Viet Nam' | 'Lao' | 'Campuchia';
...@@ -24,8 +24,8 @@ export interface JobFormData { ...@@ -24,8 +24,8 @@ export interface JobFormData {
company_url: string; company_url: string;
location: JobLocation; location: JobLocation;
type: JobType; type: JobType;
how_to_apply?: string; how_to_apply?: string;
company_logo?: string | null; company_logo?: string;
} }
export interface CreateJobRequest { export interface CreateJobRequest {
...@@ -34,7 +34,7 @@ export interface CreateJobRequest { ...@@ -34,7 +34,7 @@ export interface CreateJobRequest {
export interface UpdateJobRequest { export interface UpdateJobRequest {
id: string; id: string;
job: Partial<JobFormData>; job: JobFormData;
} }
export interface JobResponse { export interface JobResponse {
......
...@@ -33,6 +33,8 @@ interface ApiJob { ...@@ -33,6 +33,8 @@ interface ApiJob {
location: string; location: string;
title: string; title: string;
description: string; description: string;
how_to_apply?: string;
company_logo?: string;
} }
@Injectable({ providedIn: 'root' }) @Injectable({ providedIn: 'root' })
...@@ -71,22 +73,12 @@ export class JobApiService { ...@@ -71,22 +73,12 @@ export class JobApiService {
}; };
private convertApiJobToJob = (apiJob: ApiJob): Job => { private convertApiJobToJob = (apiJob: ApiJob): Job => {
console.log('Converting API job to internal format:', apiJob);
const job: Job = { const job: Job = {
id: apiJob.id || '', ...apiJob,
title: apiJob.title || '',
description: apiJob.description || '',
company: apiJob.company || '',
company_url: apiJob.company_url || '',
location: apiJob.location || '',
type: this.mapApiTypeToJobType(apiJob.type), type: this.mapApiTypeToJobType(apiJob.type),
created_at: apiJob.created_at || new Date().toISOString(), created_at: apiJob.created_at || new Date().toISOString(),
how_to_apply: "",
company_logo: null
}; };
console.log('Converted job:', job);
return job; return job;
}; };
...@@ -127,27 +119,23 @@ export class JobApiService { ...@@ -127,27 +119,23 @@ export class JobApiService {
createJob = (request: CreateJobRequest): Observable<JobResponse> => { createJob = (request: CreateJobRequest): Observable<JobResponse> => {
console.log('Creating job with request:', request);
const apiJobData = { const apiJobData = {
title: request.job.title, title: request.job.title,
description: request.job.description, description: request.job.description,
company: request.job.company, company: request.job.company,
company_url: request.job.company_url, company_url: request.job.company_url,
location: request.job.location, location: request.job.location,
type: request.job.type type: request.job.type,
how_to_apply: request.job.how_to_apply,
company_logo: request.job.company_logo,
}; };
console.log('Sending API data:', apiJobData);
return this.http.post<ApiJob>(this.API_URL, apiJobData, { return this.http.post<ApiJob>(this.API_URL, apiJobData, {
headers: this.getHeaders() headers: this.getHeaders()
}).pipe( }).pipe(
map(apiJob => { map(apiJob => {
console.log('Received API response:', apiJob);
const convertedJob = this.convertApiJobToJob(apiJob); const convertedJob = this.convertApiJobToJob(apiJob);
const result = { return {
message: 'Job created successfully', message: 'Job created successfully',
responseData: { responseData: {
job: convertedJob job: convertedJob
...@@ -155,9 +143,6 @@ export class JobApiService { ...@@ -155,9 +143,6 @@ export class JobApiService {
success: true, success: true,
status: 201 status: 201
}; };
console.log('Final response:', result);
return result;
}), }),
catchError(this.handleError) catchError(this.handleError)
); );
...@@ -173,20 +158,31 @@ export class JobApiService { ...@@ -173,20 +158,31 @@ export class JobApiService {
company: request.job.company, company: request.job.company,
company_url: request.job.company_url, company_url: request.job.company_url,
location: request.job.location, location: request.job.location,
type: request.job.type type: request.job.type,
how_to_apply: request.job.how_to_apply,
company_logo: request.job.company_logo,
}; };
console.log('Sending update API data:', apiJobData);
console.log('Update URL:', `${this.API_URL}/${request.id}`);
return this.http.put<ApiJob>(`${this.API_URL}/${request.id}`, apiJobData, { return this.http.put<ApiJob>(`${this.API_URL}/${request.id}`, apiJobData, {
headers: this.getHeaders() headers: this.getHeaders()
}).pipe( }).pipe(
map(apiJob => { map(apiJob => {
console.log('Received update API response:', apiJob); const apiJobWithId: ApiJob = {
...apiJob,
id: apiJob.id || request.id,
created_at: apiJob.created_at || new Date().toISOString(),
title: apiJob.title || request.job.title,
description: apiJob.description || request.job.description,
company: apiJob.company || request.job.company,
company_url: apiJob.company_url || request.job.company_url,
location: apiJob.location || request.job.location,
type: apiJob.type || request.job.type,
how_to_apply: apiJob.how_to_apply || request.job.how_to_apply,
company_logo: apiJob.company_logo || request.job.company_logo,
};
const convertedJob = this.convertApiJobToJob(apiJob); const convertedJob = this.convertApiJobToJob(apiJobWithId);
const result = { return {
message: 'Job updated successfully', message: 'Job updated successfully',
responseData: { responseData: {
job: convertedJob job: convertedJob
...@@ -194,9 +190,6 @@ export class JobApiService { ...@@ -194,9 +190,6 @@ export class JobApiService {
success: true, success: true,
status: 200 status: 200
}; };
console.log('Final update response:', result);
return result;
}), }),
catchError(this.handleError) catchError(this.handleError)
); );
...@@ -214,7 +207,6 @@ export class JobApiService { ...@@ -214,7 +207,6 @@ export class JobApiService {
); );
getJobStats = (): Observable<{ success: boolean; data: JobStats }> => { getJobStats = (): Observable<{ success: boolean; data: JobStats }> => {
// Since the API doesn't provide stats endpoint, we'll calculate from jobs
return this.getJobs().pipe( return this.getJobs().pipe(
map(response => { map(response => {
const jobs = response.responseData.jobs; const jobs = response.responseData.jobs;
...@@ -228,7 +220,6 @@ export class JobApiService { ...@@ -228,7 +220,6 @@ export class JobApiService {
}; };
}), }),
catchError(error => { catchError(error => {
console.error('Failed to get job stats:', error);
return of({ return of({
success: false, success: false,
data: { data: {
......
...@@ -61,34 +61,37 @@ export class JobStateService { ...@@ -61,34 +61,37 @@ export class JobStateService {
}; };
addJob = (job: Job): void => { addJob = (job: Job): void => {
console.log('Adding new job to state:', job);
const currentJobs = this.jobsSignal(); const currentJobs = this.jobsSignal();
console.log('Current jobs before adding:', currentJobs.length);
const updatedJobs = [job, ...currentJobs]; const updatedJobs = [job, ...currentJobs];
this.setJobs(updatedJobs); this.setJobs(updatedJobs);
this.updateStats(); this.updateStats();
console.log('Jobs after adding:', this.jobsSignal().length);
}; };
updateJob = (updatedJob: Job): void => { updateJob = (updatedJob: Job): void => {
console.log('Updating job in state:', updatedJob); if (!updatedJob.id) {
return;
}
const currentJobs = this.jobsSignal(); const currentJobs = this.jobsSignal();
console.log('Current jobs before update:', currentJobs.length); let jobFound = false;
const updatedJobs = currentJobs.map(job => { const updatedJobs = currentJobs.map(job => {
if (job.id === updatedJob.id) { if (job.id === updatedJob.id) {
console.log('Found job to update:', job.id); jobFound = true;
return updatedJob; return {
...job,
...updatedJob,
id: job.id,
created_at: job.created_at
};
} }
return job; return job;
}); });
this.setJobs(updatedJobs); if (jobFound) {
this.updateStats(); this.setJobs(updatedJobs);
this.updateStats();
console.log('Jobs after update:', this.jobsSignal().length); }
}; };
removeJob = (jobId: string): void => { removeJob = (jobId: string): void => {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment