Skip to content

Commit 7e48ad4

Browse files
committed
jobofferform: allow to edit job offers
Signed-off-by: Patrizio Bekerle <patrizio@bekerle.com>
1 parent 207e2b9 commit 7e48ad4

1 file changed

Lines changed: 46 additions & 6 deletions

File tree

src/modules/jobOfferForm.js

Lines changed: 46 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ import {
1111
} from '@dbp-toolkit/common';
1212
import * as commonStyles from '@dbp-toolkit/common/styles';
1313
import DBPLitElement from '@dbp-toolkit/common/dbp-lit-element';
14-
import {apiCreateForm} from '../../vendor/formalize/src/manage-forms-api.js';
14+
import {apiCreateForm, apiUpdateForm} from '../../vendor/formalize/src/manage-forms-api.js';
1515
import {createInstance} from '../i18n.js';
1616

1717
const i18n = createInstance();
@@ -97,6 +97,13 @@ class JobOfferCreateFormElement extends ScopedElementsMixin(DBPLitElement) {
9797
this.auth = {};
9898
this.entryPointUrl = '';
9999

100+
/**
101+
* When set by the parent dialog, the component operates in edit mode.
102+
* Shape: { formId, formSlug, formName, moduleInstance, additionalData, localizedNames }
103+
* @type {object|null}
104+
*/
105+
this.existingForm = null;
106+
100107
// Mandatory job detail fields
101108
this._title = '';
102109
this._description = '';
@@ -131,6 +138,7 @@ class JobOfferCreateFormElement extends ScopedElementsMixin(DBPLitElement) {
131138
lang: {type: String},
132139
auth: {type: Object},
133140
entryPointUrl: {type: String, attribute: 'entry-point-url'},
141+
existingForm: {type: Object, attribute: false},
134142
_title: {state: true},
135143
_description: {state: true},
136144
_publishedAt: {state: true},
@@ -157,6 +165,32 @@ class JobOfferCreateFormElement extends ScopedElementsMixin(DBPLitElement) {
157165
if (propName === 'lang') {
158166
this._i18n.changeLanguage(this.lang);
159167
}
168+
169+
// Pre-populate form fields when an existing form is provided for editing
170+
if (propName === 'existingForm' && this.existingForm) {
171+
const d = this.existingForm.additionalData || {};
172+
this._title = d.title || '';
173+
this._description = d.description || '';
174+
this._publishedAt = d.publishedAt || '';
175+
this._deadline = d.deadline || '';
176+
this._organization = d.organization || '';
177+
this._startDate = d.startDate || '';
178+
this._weeklyHours = d.weeklyHours || '';
179+
this._salary = d.salary || '';
180+
this._jobType = d.jobType || '';
181+
this._areaOfInterest = d.areaOfInterest || '';
182+
this._linkName = d.linkName || '';
183+
this._linkUrl = d.linkUrl || '';
184+
this._requirementsText = Array.isArray(d.requirements)
185+
? d.requirements.join('\n')
186+
: '';
187+
this._titleEn = d.titleEn || '';
188+
this._descriptionEn = d.descriptionEn || '';
189+
this._organizationEn = d.organizationEn || '';
190+
this._requirementsTextEn = Array.isArray(d.requirementsEn)
191+
? d.requirementsEn.join('\n')
192+
: '';
193+
}
160194
});
161195
super.update(changedProperties);
162196
}
@@ -220,11 +254,12 @@ class JobOfferCreateFormElement extends ScopedElementsMixin(DBPLitElement) {
220254
}
221255

222256
/**
223-
* Builds the form payload and calls apiCreateForm.
224-
* On success dispatches `dbp-create-form-created`, on failure shows an error notification.
257+
* Builds the form payload and calls apiCreateForm or apiUpdateForm depending on mode.
258+
* On success dispatches a form event, on failure shows an error notification.
225259
*/
226260
async submit() {
227261
const t = (key, opts) => this._i18n.t(key, opts);
262+
const isEditMode = this.existingForm !== null && this.existingForm !== undefined;
228263

229264
if (!this._isFormValid) {
230265
sendNotification({
@@ -313,15 +348,20 @@ class JobOfferCreateFormElement extends ScopedElementsMixin(DBPLitElement) {
313348
dataFeedSchema,
314349
};
315350

316-
// Build a minimal host object that apiCreateForm expects
351+
// Build a minimal host object that apiCreateForm / apiUpdateForm expect
317352
const host = {
318353
auth: this.auth,
319354
entryPointUrl: this.entryPointUrl,
320355
_i18n: this._i18n,
321356
};
322357

323358
try {
324-
const result = await apiCreateForm(host, formData);
359+
let result;
360+
if (isEditMode) {
361+
result = await apiUpdateForm(host, this.existingForm.formId, formData);
362+
} else {
363+
result = await apiCreateForm(host, formData);
364+
}
325365

326366
if (result) {
327367
this.dispatchEvent(
@@ -334,7 +374,7 @@ class JobOfferCreateFormElement extends ScopedElementsMixin(DBPLitElement) {
334374
return result;
335375
}
336376
} catch (error) {
337-
console.error('Error creating job offer form:', error);
377+
console.error('Error saving job offer form:', error);
338378
sendNotification({
339379
summary: t('create-job-offer.error-title'),
340380
body: error.message,

0 commit comments

Comments
 (0)