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

View File

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

View File

@ -81,6 +81,17 @@
</div> </div>
</div> </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>
<div class="border-top"> <div class="border-top">
<div class="card-body" v-on:click="saveData"> <div class="card-body" v-on:click="saveData">
@ -248,7 +259,8 @@
.then(data => { .then(data => {
console.log('Method added successfully:', data); console.log('Method added successfully:', data);
alert('Method added successfully'); alert('Method added successfully');
this.fetchXModule(); // Refresh the module data location.reload();
this.fetchXModule();
}) })
.catch(error => { .catch(error => {
console.error('There was a problem when adding the module operation:', 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'); throw new Error('Failed to save module information');
} }
alert('Module information saved successfully'); alert('Module information saved successfully');
location.reload();
}) })
.catch(error => { .catch(error => {
console.error('There was a problem with the update operation:', error); console.error('There was a problem with the update operation:', error);