80 lines
2.2 KiB
JavaScript
80 lines
2.2 KiB
JavaScript
import {ref, computed} from 'vue';
|
|
import useToastify from './useToastify.js';
|
|
import debounce from 'lodash/debounce';
|
|
|
|
const {success, errorFromResponse} = useToastify();
|
|
|
|
export default function useAdremaLogin() {
|
|
const loginData = ref({
|
|
mglnr: null,
|
|
password: null,
|
|
});
|
|
|
|
const defaultSearchData = {
|
|
vorname: null,
|
|
nachname: null,
|
|
untergliederungId: null,
|
|
};
|
|
|
|
const searchData = ref(JSON.parse(JSON.stringify(defaultSearchData)));
|
|
|
|
function resetSearchData() {
|
|
searchData.value = JSON.parse(JSON.stringify(defaultSearchData));
|
|
}
|
|
|
|
const searchResults = ref([]);
|
|
|
|
const loginToken = ref(window.localStorage.getItem('adrema_login_key') ? JSON.parse(window.localStorage.getItem('adrema_login_key')) : null);
|
|
|
|
async function login() {
|
|
try {
|
|
const response = await axios.post('/remote/nami/token', loginData.value);
|
|
const payload = {user: loginData.value.mglnr, token: response.data.access_token};
|
|
window.localStorage.setItem('adrema_login_key', JSON.stringify(payload));
|
|
loginToken.value = payload;
|
|
success('Login erfolgreich');
|
|
} catch (e) {
|
|
errorFromResponse(e);
|
|
}
|
|
}
|
|
|
|
const user = computed(() => {
|
|
if (loginToken.value === null) {
|
|
return null;
|
|
}
|
|
|
|
return loginToken.value.user;
|
|
});
|
|
|
|
function logout() {
|
|
window.localStorage.removeItem('adrema_login_key');
|
|
loginToken.value = null;
|
|
}
|
|
|
|
const searchForMember = debounce(async function () {
|
|
const response = await axios.post(
|
|
'/remote/nami/search',
|
|
{
|
|
...searchData.value,
|
|
untergliederungId: searchData.value.untergliederungId ? [searchData.value.untergliederungId] : [],
|
|
},
|
|
{
|
|
headers: {'X-Adrema-Token': loginToken.value.token},
|
|
},
|
|
);
|
|
|
|
searchResults.value = response.data.data;
|
|
}, 1000);
|
|
|
|
return {
|
|
searchResults,
|
|
login,
|
|
logout,
|
|
user,
|
|
loginData,
|
|
searchData,
|
|
searchForMember,
|
|
resetSearchData,
|
|
};
|
|
}
|