refactor: Abfrage in ein eigenes Modul ausgelagert.

This commit is contained in:
2021-12-15 17:47:02 +01:00
parent 3dd0f451fc
commit 45650a0f9a
5 changed files with 117 additions and 140 deletions
+19 -95
View File
@@ -1,5 +1,5 @@
<script>
export let params;
import {WorkingHoursRepository} from "../components/Repositories.svelte"
let title = "Bearbeitung Einträge"
import IconifyIcon from '@iconify/svelte';
import wrenchIcon from '@iconify-icons/oi/wrench';
@@ -13,100 +13,14 @@
records: [],
isLoading: true
}
const Records = {
'browse': () => {
// noinspection JSUnresolvedVariable
fetch(env.API_URL + '/working-hours')
.then(res => {
if (!res.ok) {
throw new Error('Fail!');
}
return res.json();
})
.then(data => {
Controller.cancelActiveRecord();
params.records = data;
params.isLoading = false;
})
.catch(err => {
params.isLoading = false;
console.log(err);
});
},
'read': date => {
// noinspection JSUnresolvedVariable
fetch(env.API_URL + '/working-hours/' + date)
.then(res => {
if (!res.ok) {
throw new Error('Fail!');
}
return res.json();
})
.then(data => {
activeRecord = data;
if (date === null) {
Controller.listRecords();
}
})
.catch(err => {
console.log(err);
});
},
'add': async record => {
console.log(record);
// noinspection JSUnresolvedVariable
await fetch(env.API_URL + '/working-hours', {
method: 'POST',
mode: 'cors',
cache: 'no-cache',
credentials: 'omit',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(record)
})
.then(res => {
if (!res.ok) {
throw new Error('Fail!');
}
})
.then(() => {
Controller.listRecords();
})
.catch(err => {
console.log(err);
});
},
'update': async (date, record) => {
console.log(record);
// noinspection JSUnresolvedVariable
await fetch(env.API_URL + '/working-hours/' + date, {
method: 'PUT',
mode: 'cors',
cache: 'no-cache',
credentials: 'omit',
headers: {
'Content-Type': 'application/json'
},
body: JSON.stringify(record)
})
.then(res => {
if (!res.ok) {
throw new Error('Fail!');
}
})
.then(() => {
Controller.listRecords();
})
.catch(err => {
console.log(err);
});
}
};
const Controller = {
'listRecords': () => {
Controller.cancelActiveRecord();
Records.browse();
WorkingHoursRepository.browse().then(data => {
Controller.cancelActiveRecord()
params.records = data
params.isLoading = false
});
},
'newRecord': () => {
activeRecord = {workingTime: '00:00:00'};
@@ -114,12 +28,22 @@
},
'saveRecord': () => {
if (newRecord === true) {
Records.add(activeRecord);
WorkingHoursRepository.add(activeRecord).then(() => {
Controller.listRecords()
});
} else {
Records.update(activeRecord.workingDay, activeRecord);
WorkingHoursRepository.update(activeRecord.workingDay, activeRecord).then(() => {
Controller.listRecords()
});
}
},
'selectActiveRecord': record => Records.read(record.workingDay),
'selectActiveRecord': record => WorkingHoursRepository.read(record.workingDay).then(data => {
if (data === null) {
Controller.listRecords()
return
}
activeRecord = data;
}),
'cancelActiveRecord': () => {
activeRecord = null;
newRecord = false;