refactor/js-repositories #20

Merged
TorstenHettstedt merged 4 commits from refactor/js-repositories into master 2021-12-15 20:41:24 +01:00
5 changed files with 117 additions and 140 deletions
Showing only changes of commit 45650a0f9a - Show all commits
+83
View File
@@ -0,0 +1,83 @@
<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 let WorkingHoursRepository = {
'browse': () => {
return MyFetch.get('/working-hours')
},
'read': date => {
return MyFetch.get('/working-hours/' + date)
},
'add': async (record) => {
console.debug(record);
return MyFetch.post('/working-hours', JSON.stringify(record))
},
'update': async (date, record) => {
console.debug(record);
return MyFetch.put('/working-hours/' + date, JSON.stringify(record))
}
}
export let WorkingHoursViewsRepository = {
'weekly': async () => {
return MyFetch.get('/views/working-hours/weekly')
},
'monthly': async () => {
return MyFetch.get('/views/working-hours/monthly')
},
'yearly': async () => {
return MyFetch.get('/views/working-hours/yearly')
},
}
</script>
+5 -15
View File
@@ -1,5 +1,6 @@
<script>
import Views from "./Views.svelte";
import {WorkingHoursViewsRepository} from "../components/Repositories.svelte";
export let params;
params = {
title: 'Monatsansicht',
@@ -8,21 +9,10 @@
}
const View = {
'browse': () => {
fetch(env.API_URL + '/views/working-hours/monthly')
.then(res => {
if (!res.ok) {
throw new Error('Fail!');
}
return res.json();
})
.then(data => {
params.records = data;
params.isLoading = false;
})
.catch(err => {
params.isLoading = false;
console.log(err);
});
WorkingHoursViewsRepository.monthly().then(data => {
params.records = data;
params.isLoading = false;
})
}
};
View.browse();
+5 -15
View File
@@ -1,5 +1,6 @@
<script>
import Views from "./Views.svelte";
import {WorkingHoursViewsRepository} from "../components/Repositories.svelte";
export let params;
params = {
title: 'Wochenansicht',
@@ -8,21 +9,10 @@
}
const View = {
'browse': () => {
fetch(env.API_URL + '/views/working-hours/weekly')
.then(res => {
if (!res.ok) {
throw new Error('Fail!');
}
return res.json();
})
.then(data => {
params.records = data;
params.isLoading = false;
})
.catch(err => {
params.isLoading = false;
console.log(err);
});
WorkingHoursViewsRepository.weekly().then(data => {
params.records = data;
params.isLoading = false;
})
}
};
View.browse();
+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;
+5 -15
View File
@@ -1,5 +1,6 @@
<script>
import Views from "./Views.svelte";
import {WorkingHoursViewsRepository} from "../components/Repositories.svelte";
export let params;
params = {
title: 'Jahresansicht',
@@ -8,21 +9,10 @@
}
const View = {
'browse': () => {
fetch(env.API_URL + '/views/working-hours/yearly')
.then(res => {
if (!res.ok) {
throw new Error('Fail!');
}
return res.json();
})
.then(data => {
params.records = data;
params.isLoading = false;
})
.catch(err => {
params.isLoading = false;
console.log(err);
});
WorkingHoursViewsRepository.yearly().then(data => {
params.records = data;
params.isLoading = false;
})
}
};
View.browse();