Skip to content

Commit

Permalink
revisi done
Browse files Browse the repository at this point in the history
  • Loading branch information
rizkyilhampra committed Jun 8, 2023
1 parent 3e3f782 commit 2535210
Show file tree
Hide file tree
Showing 8 changed files with 264 additions and 116 deletions.
19 changes: 7 additions & 12 deletions app/Http/Controllers/DiagnosisController.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

class DiagnosisController extends Controller
{
public function Diagnosis(Request $request)
public function diagnosis(Request $request)
{
$allGejala = Gejala::get('id')->count();

Expand Down Expand Up @@ -69,26 +69,21 @@ public function Diagnosis(Request $request)
$modelDiagnosis->penyakit_id = $penyakitId;
$modelDiagnosis->save();
}
$penyakit = Penyakit::where('id', $modelDiagnosis->penyakit_id)->first(['name', 'reason', 'solution', 'image']);
$penyakit = Penyakit::where('id', $modelDiagnosis->penyakit_id)->first('id');
$terdeteksi = true;
}
}


// Tidak ada penyakit yang terdeteksi
if (!$terdeteksi && $request->idgejala == $allGejala) {
return response()->json(['penyakitUndentified' => 'Tidak ada penyakit yang cocok dengan gejala yang anda masukkan.']);
return response()->json([
'penyakitUndentified' => true
]);
}

return response()->json([
$modelDiagnosis->answer_log,
$penyakit ?? null
'idDiagnosis' => $modelDiagnosis->id,
'idPenyakit' => $penyakit ?? null
]);
}

public function getGejala(Request $request)
{
$gejala = Gejala::get(['id', 'name']);
return response()->json($gejala);
}
}
26 changes: 19 additions & 7 deletions app/Http/Controllers/UserController.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,29 +84,41 @@ public function historiDiagnosis(Request $request)
]);
}

public function historiDiagnosisDetail(Request $request)
public function detailDiagnosis(Request $request)
{
$diagnosis = Diagnosis::find($request->id, ['answer_log']);
$penyakit = Penyakit::find(
Diagnosis::find($request->id_diagnosis, ['penyakit_id'])->penyakit_id,
['name', 'reason', 'solution', 'image']
);

$diagnosis = Diagnosis::find($request->id_diagnosis, ['answer_log']);
$answerLog = json_decode($diagnosis->answer_log, true);
foreach ($answerLog as $key => $value) {
$answerLog[$key] = $value == 1 ? 'Ya' : 'Tidak';
}
$gejala = Gejala::whereIn('id', array_keys($answerLog))->get(['id', 'name']);
foreach ($gejala as $item) {

$item->answer = $answerLog[$item->id];
}
$answerLog = $gejala->map(function ($item) use ($request) {
return [
'no' => $request->no,
'id' => $item->id,
'name' => $item->name,
'answer' => $item->answer,
];
});

return response()->json([
'answerLog' => $answerLog,
]);
return response()->json(
[
'penyakit' => $penyakit,
'answerLog' => $answerLog,
]
);
}

public function getGejala()
{
$gejala = Gejala::get(['id', 'name']);
return response()->json($gejala);
}
}
131 changes: 131 additions & 0 deletions public/spesified-assets/user/detail-diagnosis-modal.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
const detailDiagnosisModal = document.querySelector('#detailDiagnosisModal');
const titleDetailDiagnosisModal = detailDiagnosisModal.querySelector('.modal-title');
const instanceDetailDiagnosisModal = bootstrap.Modal.getOrCreateInstance(detailDiagnosisModal);
const headerDetailDiagnosis = document.getElementById('headerDetailDiagnosis');
const subheaderDetailDiagnosis = document.getElementById('subheaderDetailDiagnosis');
const containerImagePenyakitDetailDiagnosisModal = document.getElementById('containerImagePenyakitDetailDiagnosisModal');
const headerPenyakitSolution = document.getElementById('headerPenyakitSolution');
const rowDetailPenyakit = document.getElementById('rowDetailPenyakit');
const detailJawabanDiagnosisTable = document.getElementById('detailJawabanDiagnosisTable');
const tableBody = detailJawabanDiagnosisTable.querySelector('tbody');

let idPenyakit = null;
let idDiagnosis = null;
let noHistoriDiagnosis = null;
let diagnosed = false;
let penyakitUndentified = false;

function getPenyakitIdFromHistori(data, no) {
idDiagnosis = data;
noHistoriDiagnosis = no;
diagnosed = false;
instanceDetailDiagnosisModal.show();
}

function getPenyakitFromDiagnose(data, wasDiagnosed) {
idPenyakit = data.idPenyakit;
idDiagnosis = data.idDiagnosis;
diagnosed = wasDiagnosed;
instanceDetailDiagnosisModal.show();
}

function getUndentifiedPenyakit(data) {
penyakitUndentified = data;
instanceDetailDiagnosisModal.show();
}

function ajaxRequestDetailDiagnosis() {
return $.ajax({
url: '/detail-diagnosis',
method: 'GET',
data: {
id_penyakit: idPenyakit,
id_diagnosis: idDiagnosis,
},
});
}

detailDiagnosisModal.addEventListener('show.bs.modal', async () => {
try {
let response = await ajaxRequestDetailDiagnosis();
drawDetailDiagnosis(response, diagnosed);
drawDetailJawabanDiagnosis(response.answerLog);
} catch (error) {
swalError(error.responseJSON);
}
});


function drawDetailDiagnosis(response, wasDiagnosed) {
if (wasDiagnosed) {
titleDetailDiagnosisModal.innerText = 'Detail Diagnosis';
headerDetailDiagnosis.innerText = "Penyakit Ditemukan!";
subheaderDetailDiagnosis.innerHTML = "Penyakit yang diderita adalah " + `<u>${response.penyakit.name}</u>`;
headerDetailDiagnosis.classList.remove('d-none');
subheaderDetailDiagnosis.classList.remove('d-none');
}
titleDetailDiagnosisModal.innerText = 'Detail Diagnosis No. ' + noHistoriDiagnosis;
if (response.penyakit == null || penyakitUndentified) {
headerDetailDiagnosis.innerText = "Penyakit Tidak Ditemukan!";
subheaderDetailDiagnosis.innerHTML = 'Tidak ada penyakit yang cocok dengan gejala yang anda masukkan.';
headerDetailDiagnosis.classList.remove('d-none');
subheaderDetailDiagnosis.classList.remove('d-none');
rowDetailPenyakit.classList.add('d-none');
} else {
if (rowDetailPenyakit.classList.contains('d-none')) {
rowDetailPenyakit.classList.remove('d-none');
}
const penyakitName = document.getElementById('penyakitName');
const penyakitReason = document.getElementById('penyakitReason');
penyakitName.innerHTML = response.penyakit.name;
penyakitReason.innerHTML = response.penyakit.reason;

let penyakitSolution = response.penyakit.solution;
let regex = /(\d+\.)\s*(.*?)(?=(\d+\.|$))/gs;
let matches = [...penyakitSolution.matchAll(regex)];
let nomorAsOlTag = '<ol>';
for (let i = 0; i < matches.length; i++) {
nomorAsOlTag += '<li>' + matches[i][2] + '</li>';
}
nomorAsOlTag += '</ol>';
headerPenyakitSolution.insertAdjacentHTML('afterend', nomorAsOlTag);

const imagePenyakit = new Image();
imagePenyakit.src = assetImageUrl + '/' + response.penyakit.image;
imagePenyakit.alt = response.penyakit.name;
imagePenyakit.classList.add('img-fluid');
containerImagePenyakitDetailDiagnosisModal.appendChild(imagePenyakit);
}
}

function drawDetailJawabanDiagnosis(data) {
const response = data;
response.forEach((item, index) => {
const tableRow = document.createElement('tr');
const tableData = document.createElement('td');
const tableData2 = document.createElement('td');
const tableData3 = document.createElement('td');
let number = index + 1;
tableData.innerHTML = number;
tableData2.innerHTML = item.name;
tableData3.innerHTML = item.answer;
tableRow.appendChild(tableData);
tableRow.appendChild(tableData2);
tableRow.appendChild(tableData3);
tableBody.appendChild(tableRow);
});
}

detailDiagnosisModal.addEventListener('hide.bs.modal', function (event) {
containerImagePenyakitDetailDiagnosisModal.innerHTML = '';
if (headerPenyakitSolution.nextElementSibling) {
headerPenyakitSolution.nextElementSibling.remove();
}
headerDetailDiagnosis.classList.add('d-none');
subheaderDetailDiagnosis.classList.add('d-none');

//remove all child element in table body
while (tableBody.firstChild) {
tableBody.removeChild(tableBody.firstChild);
}
});
74 changes: 8 additions & 66 deletions public/spesified-assets/user/script.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ async function drawHistoriDiagnosisTable() {
{
data: 'id',
render: function (data, type, row, meta) {
return `<button class="btn btn-outline-primary me-1" onclick="detailHistoriDiagnosis(${data}, ${row.no})">
return `<button class="btn btn-outline-primary me-1" onclick="getPenyakitIdFromHistori(${data}, ${row.no})">
<i class="fa-solid fa-eye"></i>
<button class="btn btn-outline-danger" onclick="deleteHistoriDiagnosis(${data})">
<i class="fa-solid fa-trash"></i>
Expand Down Expand Up @@ -107,61 +107,6 @@ function deleteHistoriDiagnosis(id) {
});
}

async function detailHistoriDiagnosis(id, no) {
const detailHistoriDiagnosis = document.getElementById('detailHistoriDiagnosis');
const table = detailHistoriDiagnosis.querySelector('table');
const tableBody = table.querySelector('tbody');
const sectionHeading = detailHistoriDiagnosis.querySelector('h2');
//remove all child element in table body
while (tableBody.firstChild) {
tableBody.removeChild(tableBody.firstChild);
}
sectionHeading.innerHTML = "";

function ajaxRequestToHistoriDiagnosisDetail() {
return $.ajax({
url: "/histori-diagnosis-user/detail",
method: "GET",
data: {
id: id,
no: no
}
});
}
try {
const response = await ajaxRequestToHistoriDiagnosisDetail();
response.answerLog.forEach((item, index) => {
sectionHeading.innerHTML = `Detail Diagnosis No.${item.no}`;
const tableRow = document.createElement('tr');
const tableData = document.createElement('td');
const tableData2 = document.createElement('td');
const tableData3 = document.createElement('td');
let number = index + 1;
tableData.innerHTML = number;
tableData2.innerHTML = item.name;
tableData3.innerHTML = item.answer;
tableRow.appendChild(tableData);
tableRow.appendChild(tableData2);
tableRow.appendChild(tableData3);
tableBody.appendChild(tableRow);
});

detailHistoriDiagnosis.classList.remove('d-none');
detailHistoriDiagnosis.scrollIntoView({
behavior: 'smooth'
});
} catch (error) {
swalError(error.responseJSON);
}
}

function ajaxRequestToHistoriDiagnosis() {
return $.ajax({
url: "/histori-diagnosis-user",
method: "GET"
});
}

function ajaxRequestEditProfile() {
return $.ajax({
url: "/edit-profile",
Expand Down Expand Up @@ -451,15 +396,14 @@ document.addEventListener('DOMContentLoaded', async () => {
swalLoading.close();

//looping Swal sebanyak jumlah gejala
let isClosed = false;
var isClosed = false;
for (let i = 0; i < countGejala; i++) {
const element = gejala[i];
const {
value: jawaban,
dismiss: dismissReason
} = await Swal.fire({
title: 'Pertanyaan ' + (i + 1) + ' dari ' +
countGejala,
title: 'Pertanyaan ' + (i + 1),
text: 'Apakah ' + element.name +
'?',
icon: 'question',
Expand All @@ -480,16 +424,14 @@ document.addEventListener('DOMContentLoaded', async () => {
}
try {
const response = await ajaxRequestToDiagnosis(element.id, jawaban);
if (response[1] != null) {
console.log(response);
if (response.idPenyakit != null) {
await Swal.close();
modalResult(response[1], true, 'Penyakit ditemukan!',
'Penyakit yang diderita adalah ' + response[1].name,
'success');
getPenyakitFromDiagnose(response, true);
break;
} else if (response.penyakitUndentified) {
modalResult(response.penyakitUndentified, false,
'Penyakit tidak ditemukan!',
'Mohon maaf, penyakit tidak dapat ditemukan', 'error');
getUndentifiedPenyakit(response.penyakitUndentified);
break;
}
} catch (error) {
swalError(error.responseJSON);
Expand Down
Loading

0 comments on commit 2535210

Please sign in to comment.