File size: 9,055 Bytes
919f56a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
983b3f6
919f56a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
const dictionary = {
    1: 'Basic Template',
    2: 'Link Template',
    3: 'Image Template'
    };

const carousel = document.getElementById('carouselExampleCaptions');
const carouselText = document.getElementById('carouselText');
var activeIndex=0;
carousel.addEventListener('slid.bs.carousel', function (event) {
 activeIndex = event.to;
captionDiv = document.querySelector('.carousel-item.active .carousel-caption');
carouselText.innerHTML = dictionary[activeIndex+1];
carouselText.classList.add('highlight');
});
carousel.addEventListener('slide.bs.carousel', function () {
carouselText.classList.remove('highlight');
});







// Function to create an input field with a placeholder
function createInputField(name, id, required, placeholder) {
const input = document.createElement('input');
input.type = 'text';
input.name = name;
input.id = id;
input.placeholder = placeholder; // Set the placeholder value
if (required) {
input.required = true;
}
return input;
}

// Function to create a textarea with a placeholder
function createTextArea(name, id, required, placeholder) {
const textarea = document.createElement('textarea');
textarea.name = name;
textarea.id = id;
textarea.placeholder = placeholder; // Set the placeholder value
if (required) {
textarea.required = true;
}
return textarea;
}

// Add placeholders to dynamically generated fields
function addSkill() {
const skillsContainer = document.getElementById('skillsContainer');
const skillInput = createInputField('skills[]', '', true, 'Enter a skill'); // Add placeholder
const deleteButton = createDeleteButton(skillInput);
skillsContainer.appendChild(skillInput);
skillsContainer.appendChild(deleteButton);
}

function addEducation() {
const educationContainer = document.getElementById('educationContainer');
const schoolNameInput = createInputField('education[][school_name]', '', true, 'Enter school name'); // Add placeholder
const passingYearInput = createInputField('education[][passing_year]', '', true, 'Enter your passing year 2018-2020'); // Add placeholder
const descriptionTextarea = createTextArea('education[][description]', '', true, 'Enter a description'); // Add placeholder
const deleteButton = createDeleteButton(schoolNameInput, passingYearInput, descriptionTextarea);
educationContainer.appendChild(schoolNameInput);
educationContainer.appendChild(passingYearInput);
educationContainer.appendChild(descriptionTextarea);
educationContainer.appendChild(deleteButton);
}

function addExperience() {
const experienceContainer = document.getElementById('experienceContainer');
const companyNameInput = createInputField('experience[][company_name]', '', true, 'Enter company name'); // Add placeholder
const positionInput = createInputField('experience[][passing_year]', '', true, 'Enter your passing year 2020-2021');  // Add placeholder
const responsibilitiesTextarea = createTextArea('experience[][responsibilities]', '', true, 'Enter responsibilities'); // Add placeholder
const deleteButton = createDeleteButton(companyNameInput, positionInput, responsibilitiesTextarea);
experienceContainer.appendChild(companyNameInput);
experienceContainer.appendChild(positionInput);
experienceContainer.appendChild(responsibilitiesTextarea);
experienceContainer.appendChild(deleteButton);
}

function addAchievement() {
const achievementsContainer = document.getElementById('achievementsContainer');
const fieldInput = createInputField('achievements[][field]', '', true, 'Enter field'); // Add placeholder
const awardsInput = createInputField('achievements[][awards]', '', true, 'Enter awards'); // Add placeholder
const deleteButton = createDeleteButton(fieldInput, awardsInput);
achievementsContainer.appendChild(fieldInput);
achievementsContainer.appendChild(awardsInput);
achievementsContainer.appendChild(deleteButton);
}

// Add placeholders to form inputs
const nameInput = document.getElementById('name');
nameInput.placeholder = 'Enter your Name';

const lastNameInput = document.getElementById('last_name');
lastNameInput.placeholder = 'Enter your Last Name';

const emailInput = document.getElementById('email_address');
emailInput.placeholder = 'Enter your email address';

const phoneNumberInput = document.getElementById('phone_number');
phoneNumberInput.placeholder = 'Ex +91 7013...';

const linkedInInput = document.getElementById('linkedin_url');
linkedInInput.placeholder = 'Enter your LinkedIn URL';

const jobTitleInput = document.getElementById('job_title');
jobTitleInput.placeholder = 'Enter your job title';

const careerObjectiveTextarea = document.getElementById('career_objective');
careerObjectiveTextarea.placeholder = 'Enter your career objective';


function createDeleteButton(...deleteElements) {
    const deleteButton = document.createElement('button');
    deleteButton.type = 'button';
    deleteButton.textContent = 'Delete';
    deleteButton.addEventListener('click', () => {
    deleteElements.forEach((element) => {
        element.parentNode.removeChild(element);
    });
    deleteButton.parentNode.removeChild(deleteButton);
    });
    return deleteButton;
}

const form = document.getElementById('dynamicForm');
form.addEventListener('submit', (event) => {
    event.preventDefault();

    const formData = new FormData(form);
    const jsonData = {
    template_id:-1,
    personal_information: {},
    job_title:"",
    career_objective:"",
    skills: [],
    education: [],
    experience: [],
    achievements: []
    };

    educationIndex=0;
    experienceIndex=0;
    achievementIndex=0;
    for (const [key, value] of formData.entries()) {
        
    const keys = key.split(/\[|\]\[|\]/).filter((entry) => entry !== '');
    const category = keys.shift();
    //   console.log(category,key,keys,value);
    if (category === 'personal_information') {
        const fieldName = keys[0];
        jsonData.personal_information[fieldName] = value;
    } else if(category==='job_title')
    {
        jsonData.job_title=value;
    }
    else if(category==='career_objective')
    {
       jsonData.career_objective=value;
    }
    else if (category === 'skills') {
        jsonData.skills.push(value);
    } 

    //correct code

    else if (category === 'education') {
        const fieldName = keys[0];
        const fieldValue = formData.get(key);
        const educationItem = jsonData.education[educationIndex] || {};
        educationItem[fieldName] = fieldValue;
        jsonData.education[educationIndex] = educationItem;
        if( Object.keys(educationItem).length===3)
        {
            educationIndex+=1;
        
        }
    } else if (category === 'experience') {
        const fieldName = keys[0];
        const fieldValue = formData.get(key);
        const experienceItem = jsonData.experience[experienceIndex] || {};
        experienceItem[fieldName] = fieldValue;
        jsonData.experience[experienceIndex] = experienceItem;
        if( Object.keys(experienceItem).length===3)
        {
        experienceIndex+=1;
        }
    } else if (category === 'achievements') {
        const fieldName = keys[0];
        const fieldValue = formData.get(key);
        const achievementItem = jsonData.achievements[achievementIndex] || {};
        achievementItem[fieldName] = fieldValue;
        jsonData.achievements[achievementIndex] = achievementItem;
        if( Object.keys(achievementItem).length===2)
        {
        achievementIndex+=1;
        }
    }
    }
    // Select all buttons in the form
    const buttons = form.querySelectorAll('button');

    buttons.forEach(button => {
        button.disabled = true;
      });
      const loadingIcon = document.getElementById('loadingIcon');
      loadingIcon.style.display = 'block';
    jsonData.template_id=(activeIndex+1).toString();
    console.log('Form Data:', jsonData);
    fetch('http://localhost:7860/', {
        method: 'POST',
        headers: {
          'Accept':'application/pdf',
          'Content-Type': 'application/json'
        },
        body: JSON.stringify(jsonData)
      })
      // Disable all buttons
      .then(response => {
        if (response.ok) {
            return response.blob();
        } else {
            throw new Error('Invalid data');
        }
    })
    .then(blob => {
        // Handle the successful response
        const url = URL.createObjectURL(blob);
        window.open(url, '_blank');
        console.log(url);
        console.log(blob);
        buttons.forEach(button => {
            button.disabled = false;
        });
        loadingIcon.style.display = 'none';
        // Alternatively, you can set the URL as the source of an iframe to display it within the page
        // const iframe = document.createElement('iframe');
        // iframe.src = url;
        // document.body.appendChild(iframe);
    })
    .catch(error => {
        // Handle the error response
        console.error('Error:', error);
        buttons.forEach(button => {
            button.disabled = false;
          });
        // Display an error message to the user
        alert(error);
    });
  
});