add module & status

This commit is contained in:
ArifHilmi 2024-11-27 10:34:32 +08:00
parent f9666d341c
commit 1484cfa45e
4 changed files with 80 additions and 31 deletions

View File

@ -132,5 +132,32 @@ namespace PSTW_CentralSystem.Controllers.API
return BadRequest(ex.Message);
}
}
[HttpPost("DeleteModule")]
public async Task<IActionResult> DeleteModule([FromBody] int? id)
{
try
{
var deleteList = await _authDbContext.ModuleSettings
.Where(x => x.SettingId == id)
.FirstOrDefaultAsync();
if (deleteList != null)
{
_authDbContext.ModuleSettings.Remove(deleteList);
await _authDbContext.SaveChangesAsync();
}
var updatedList = await _authDbContext.ModuleSettings.ToListAsync();
return Json(updatedList);
}
catch(Exception ex)
{
return BadRequest(ex.Message);
}
}
}
}

View File

@ -130,7 +130,9 @@
if (!response.ok) {
throw new Error('Name module already exists');
}
location.reload();
alert('Module information saved successfully');
window.location.href = '/Admin/ModuleAdmin';
})
.catch(error => {
console.error('There was a problem with the update operation:', error);

View File

@ -81,28 +81,28 @@
</div>
</div>
</div>
</div>
<!-- MODAL -->
<div class="modal fade" id="confirm-dialog" tabindex="-1" role="dialog" aria-labelledby="confirm-dialog-title" aria-hidden="true">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="confirm-dialog-title">Confirmation</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div v-if="selectedModule">
<div class="modal-body">
<p>Are you sure you want to delete module {{ selectedModule.moduleName }}?</p>
<!-- MODAL -->
<div class="modal fade" id="confirm-dialog" v-show="selectedModule">
<div class="modal-dialog modal-dialog-centered" role="document">
<div class="modal-content">
<div class="modal-header">
<h5 class="modal-title" id="confirm-dialog-title">Confirmation</h5>
<button type="button" class="close" data-dismiss="modal" aria-label="Close">
<span aria-hidden="true">&times;</span>
</button>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<input type="hidden" id="delete-id">
<a id="confirmButton" href="#" class="btn btn-danger" v-on:click="confirmDelete(selectedModule)">Confirm</a>
<div v-if="selectedModule">
<div class="modal-body">
<p>Are you sure you want to delete module {{ selectedModule.moduleName }}?</p>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-secondary" data-dismiss="modal">Cancel</button>
<input type="hidden" id="delete-id">
<a id="confirmButton" class="btn btn-danger" v-on:click="confirmDelete(selectedModule)">Confirm</a>
</div>
</div>
<div v-else><p>Loading...</p></div>
</div>
<div v-else><p>Loading...</p></div>
</div>
</div>
</div>
@ -147,24 +147,31 @@
}
},
deleteModule(module) {
this.selectedModule = module; // Set selected user
$('#confirm-dialog').modal('show'); // Show the modal
this.selectedModule = module;
$('#confirm-dialog').modal('show');
},
confirmDelete(module) {
fetch('/ModuleAPI/DeleteModule/' + module.settingId, {
method: 'POST'
if (!module || !module.settingId) {
console.error('Invalid module data');
return;
}
$('#confirm-dialog').modal('hide');
fetch('/ModuleAPI/DeleteModule', {
method: 'POST',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(module.settingId)
})
.then(response => {
if (!response.ok) {
throw new Error('Failed to delete module');
}
// Remove the deleted user from the userData array
const index = this.moduleData.findIndex(u => u.settingId === module.settingId);
if (index !== -1) {
alert("Module deleted successfully");
this.moduleData.splice(index, 1);
}
$('#confirm-dialog').modal('hide'); // Hide the modal after deletion
alert("Module deleted successfully");
location.reload();
})
.catch(error => {
console.error('Failed to delete module with status:', error);

View File

@ -81,6 +81,17 @@
</div>
</div>
</div>
<div class="form-group row">
<label class="col-md-3 mt-3">Module Status</label>
<div class="col-md-6">
<select class="form-select shadow-none mt-3" name="moduleStatus" v-model="moduleData.moduleStatus" style="height: 36px; width: 100%">
<optgroup label="General">
<option value="1">Enabled</option>
<option value="0">Disabled</option>
</optgroup>
</select>
</div>
</div>
</div>
<div class="border-top">
<div class="card-body" v-on:click="saveData">
@ -248,7 +259,8 @@
.then(data => {
console.log('Method added successfully:', data);
alert('Method added successfully');
this.fetchXModule(); // Refresh the module data
location.reload();
this.fetchXModule();
})
.catch(error => {
console.error('There was a problem when adding the module operation:', error);
@ -268,6 +280,7 @@
throw new Error('Failed to save module information');
}
alert('Module information saved successfully');
location.reload();
})
.catch(error => {
console.error('There was a problem with the update operation:', error);