refactor: REST-API-Abfrage ist in eigenes Modul ausgelagert worden
This commit is contained in:
@@ -0,0 +1,71 @@
|
||||
<script context="module">
|
||||
const MyFetch = {
|
||||
'post': async (rest_url, request_body) => {
|
||||
return MyFetch._fetch(rest_url, {
|
||||
method: 'POST',
|
||||
mode: 'cors',
|
||||
cache: 'no-cache',
|
||||
credentials: 'omit',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: request_body,
|
||||
}, {})
|
||||
},
|
||||
'put': async (rest_url, http_body) => {
|
||||
return MyFetch._fetch(rest_url, {
|
||||
method: 'PUT',
|
||||
mode: 'cors',
|
||||
cache: 'no-cache',
|
||||
credentials: 'omit',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
},
|
||||
body: http_body,
|
||||
}, {})
|
||||
},
|
||||
'get': async (rest_url, query_parameters) => {
|
||||
if (query_parameters !== null) {
|
||||
let filter = []
|
||||
for (let parameter in query_parameters) {
|
||||
filter.push(parameter + '=' + query_parameters[parameter])
|
||||
}
|
||||
if (filter.length > 0) {
|
||||
rest_url += '?' + filter.join('&')
|
||||
}
|
||||
}
|
||||
return MyFetch._fetch(rest_url, {})
|
||||
},
|
||||
'_fetch': async (rest_url, request_options) => {
|
||||
// noinspection JSUnresolvedVariable
|
||||
let response = await fetch(env.API_URL + rest_url, request_options).catch(err => {
|
||||
console.error(err)
|
||||
return null
|
||||
})
|
||||
if (!response.ok) {
|
||||
throw new Error('Fail!')
|
||||
}
|
||||
return response.json()
|
||||
}
|
||||
}
|
||||
|
||||
export class WorkingHoursRepository {
|
||||
static 'browse'() {
|
||||
return MyFetch.get('/working-hours')
|
||||
}
|
||||
|
||||
static 'read'(date) {
|
||||
return MyFetch.get('/working-hours/' + date)
|
||||
}
|
||||
|
||||
static async 'add'(record) {
|
||||
console.debug(record);
|
||||
return MyFetch.post('/working-hours', JSON.stringify(record))
|
||||
}
|
||||
|
||||
static async 'update'(date, record) {
|
||||
console.debug(record);
|
||||
return MyFetch.post('/working-hours/' + date, JSON.stringify(record))
|
||||
}
|
||||
}
|
||||
</script>
|
||||
@@ -7,122 +7,41 @@
|
||||
import xIcon from '@iconify-icons/oi/x';
|
||||
import documentIcon from '@iconify-icons/oi/document';
|
||||
let activeRecord = null;
|
||||
let newRecord = false;
|
||||
params = {
|
||||
title: 'Einträge',
|
||||
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();
|
||||
const Controller = {
|
||||
'listRecords': () => {
|
||||
WorkingHoursRepository.browse().then(data => {
|
||||
activeRecord = null;
|
||||
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();
|
||||
},
|
||||
'newRecord': () => {
|
||||
activeRecord = {workingTime: '00:00:00'};
|
||||
newRecord = true;
|
||||
activeRecord = {workingTime: '00:00:00', workingDay: null};
|
||||
},
|
||||
'saveRecord': () => {
|
||||
if (newRecord === true) {
|
||||
Records.add(activeRecord);
|
||||
if (activeRecord.workingDay === null) {
|
||||
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 => {
|
||||
activeRecord = data;
|
||||
if (activeRecord === null) {
|
||||
Controller.listRecords()
|
||||
}
|
||||
}),
|
||||
'cancelActiveRecord': () => {
|
||||
activeRecord = null;
|
||||
newRecord = false;
|
||||
}
|
||||
};
|
||||
const initSite = () => {
|
||||
|
||||
Reference in New Issue
Block a user