refactor [P-47]: упрощение логики, возможность выбора всех сотрудников для событий
This commit is contained in:
1 parent
105217c5e5
commit
6013c4ed0f
3 files changed
+140
-123
No files matched your search
@@ -19,6 +19,7 @@
|
|||||||
<event-create
|
<event-create
|
||||||
@closeDialog="closeEventDialog"
|
@closeDialog="closeEventDialog"
|
||||||
:eventCalendar="eventFromCalendar"
|
:eventCalendar="eventFromCalendar"
|
||||||
|
:myCalendars="calendarStore.myCalendars"
|
||||||
@getCalendar="getCalendar"
|
@getCalendar="getCalendar"
|
||||||
@update:eventCalendar="eventFromCalendar = $event"
|
@update:eventCalendar="eventFromCalendar = $event"
|
||||||
ref="createEvents"
|
ref="createEvents"
|
||||||
@@ -47,21 +48,12 @@ export default {
|
|||||||
const MyMonth = ref(selectdate.value.getMonth() + 1);
|
const MyMonth = ref(selectdate.value.getMonth() + 1);
|
||||||
const calendarForMy = ref([]);
|
const calendarForMy = ref([]);
|
||||||
const eventFromCalendar = ref({});
|
const eventFromCalendar = ref({});
|
||||||
const createEvents = ref(null);
|
|
||||||
|
|
||||||
const calendarStore = useCalendarStore();
|
const calendarStore = useCalendarStore();
|
||||||
const accountStore = useAccountStore();
|
const accountStore = useAccountStore();
|
||||||
|
|
||||||
const { selectedDate, showEventDialog } = storeToRefs(calendarStore); // синхронизируем с date-picker
|
const { selectedDate, showEventDialog } = storeToRefs(calendarStore); // синхронизируем с date-picker
|
||||||
|
|
||||||
const handleSaveEvent = (eventData) => {
|
|
||||||
if (eventData.id) {
|
|
||||||
calendarStore.updatedEvent(eventData);
|
|
||||||
} else {
|
|
||||||
calendarStore.addEvent(eventData);
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
const getMonth = (month) => {
|
const getMonth = (month) => {
|
||||||
MyMonth.value = month + 1;
|
MyMonth.value = month + 1;
|
||||||
};
|
};
|
||||||
@@ -121,9 +113,7 @@ export default {
|
|||||||
getCalendar,
|
getCalendar,
|
||||||
// getEvents,
|
// getEvents,
|
||||||
eventFromCalendar,
|
eventFromCalendar,
|
||||||
createEvents,
|
|
||||||
calendarStore,
|
calendarStore,
|
||||||
handleSaveEvent,
|
|
||||||
accountStore,
|
accountStore,
|
||||||
closeEventDialog,
|
closeEventDialog,
|
||||||
};
|
};
|
||||||
|
|||||||
@@ -40,8 +40,8 @@
|
|||||||
<v-col class="text-body-2 text-text_grey" lg="2"> Календарь</v-col>
|
<v-col class="text-body-2 text-text_grey" lg="2"> Календарь</v-col>
|
||||||
<v-col>
|
<v-col>
|
||||||
<v-autocomplete
|
<v-autocomplete
|
||||||
v-model="calendar"
|
v-model="myCalendar"
|
||||||
:items="propsEvent.mycalendars"
|
:items="safeCalendars"
|
||||||
label="Выберите календарь или найдите по названию"
|
label="Выберите календарь или найдите по названию"
|
||||||
item-title="cl_name"
|
item-title="cl_name"
|
||||||
item-value="id"
|
item-value="id"
|
||||||
@@ -50,6 +50,7 @@
|
|||||||
density="compact"
|
density="compact"
|
||||||
class="py-3"
|
class="py-3"
|
||||||
return-object
|
return-object
|
||||||
|
:rules="rules.calendar"
|
||||||
:disabled="'id' in propsEvent.event"
|
:disabled="'id' in propsEvent.event"
|
||||||
>
|
>
|
||||||
<template v-slot:selection="{ item }">
|
<template v-slot:selection="{ item }">
|
||||||
@@ -63,7 +64,7 @@
|
|||||||
:key="item.title"
|
:key="item.title"
|
||||||
:value="item"
|
:value="item"
|
||||||
color="primary"
|
color="primary"
|
||||||
@click.stop="calendar = item"
|
@click.stop="myCalendar = item.raw"
|
||||||
>
|
>
|
||||||
<template v-slot:prepend>
|
<template v-slot:prepend>
|
||||||
<v-avatar class="cursor-pointer" size="8" :color="item.raw.cl_color" />
|
<v-avatar class="cursor-pointer" size="8" :color="item.raw.cl_color" />
|
||||||
@@ -174,13 +175,14 @@
|
|||||||
</template>
|
</template>
|
||||||
|
|
||||||
<script>
|
<script>
|
||||||
import { computed, ref } from 'vue';
|
import { computed, ref, watch } from 'vue';
|
||||||
import { axiosApiInstance } from '@/utils/axios-api';
|
import { axiosApiInstance } from '@/utils/axios-api';
|
||||||
|
import { useAccountStore } from '@/store/AccountStore';
|
||||||
|
import { useCalendarStore } from '@/store/calendarStore';
|
||||||
import DialogCreate from '@/components/calendar_of_events/UI_calendar/dialogCreate.vue';
|
import DialogCreate from '@/components/calendar_of_events/UI_calendar/dialogCreate.vue';
|
||||||
import calendarCreateSelect from '@/components/calendar_of_events/calendar-create-select.vue';
|
import calendarCreateSelect from '@/components/calendar_of_events/calendar-create-select.vue';
|
||||||
import { formatComa } from '@/utils/universal_functions';
|
import { formatComa } from '@/utils/universal_functions';
|
||||||
import TimeSelect from '@/components/calendar_of_events/UI_calendar/timeSelect.vue';
|
import TimeSelect from '@/components/calendar_of_events/UI_calendar/timeSelect.vue';
|
||||||
import { useAccountStore } from '@/store/AccountStore';
|
|
||||||
import UploadingFile from '@/components/UI/defaultportal/uploading-file.vue';
|
import UploadingFile from '@/components/UI/defaultportal/uploading-file.vue';
|
||||||
import ReminderEvent from '@/components/calendar_of_events/UI_calendar/reminderEvent.vue';
|
import ReminderEvent from '@/components/calendar_of_events/UI_calendar/reminderEvent.vue';
|
||||||
import PeriodEvents from '@/components/calendar_of_events/UI_calendar/periodEvents.vue';
|
import PeriodEvents from '@/components/calendar_of_events/UI_calendar/periodEvents.vue';
|
||||||
@@ -202,9 +204,14 @@ export default {
|
|||||||
type: Object,
|
type: Object,
|
||||||
default: () => {},
|
default: () => {},
|
||||||
},
|
},
|
||||||
|
myCalendars: {
|
||||||
|
type: Array,
|
||||||
|
default: () => [],
|
||||||
|
},
|
||||||
},
|
},
|
||||||
|
|
||||||
setup(props, { emit }) {
|
setup(props, { emit }) {
|
||||||
|
const accountStore = useAccountStore();
|
||||||
const createDialog = ref(null);
|
const createDialog = ref(null);
|
||||||
|
|
||||||
const propsEvent = computed({
|
const propsEvent = computed({
|
||||||
@@ -215,6 +222,19 @@ export default {
|
|||||||
set: (value) => emit('update:eventCalendar', value),
|
set: (value) => emit('update:eventCalendar', value),
|
||||||
});
|
});
|
||||||
|
|
||||||
|
// ВАЛИДАЦИЯ
|
||||||
|
|
||||||
|
const isFormValid = ref(false);
|
||||||
|
|
||||||
|
const rules = {
|
||||||
|
name: [
|
||||||
|
(val) => !!val || 'Поле обязательно для заполнения',
|
||||||
|
(val) => (val && val.length >= 3) || 'Минимум 3 символа',
|
||||||
|
(val) => (val && val.length <= 100) || 'Максимум 100 символов',
|
||||||
|
],
|
||||||
|
calendar: [(val) => !!val || 'Выберите календарь'],
|
||||||
|
};
|
||||||
|
|
||||||
const repeatRules = [
|
const repeatRules = [
|
||||||
{ type: 'Не повторять', rule: {}, id: 1 },
|
{ type: 'Не повторять', rule: {}, id: 1 },
|
||||||
{ type: 'Каждый день', rule: { freq: 3 }, id: 2 },
|
{ type: 'Каждый день', rule: { freq: 3 }, id: 2 },
|
||||||
@@ -293,43 +313,81 @@ export default {
|
|||||||
return !!val || 'Поле обязательно';
|
return !!val || 'Поле обязательно';
|
||||||
};
|
};
|
||||||
|
|
||||||
const rules = {
|
|
||||||
name: [(val) => req(val)],
|
|
||||||
};
|
|
||||||
|
|
||||||
const staff = ref([]);
|
const staff = ref([]);
|
||||||
|
|
||||||
axiosApiInstance.get('api/calendar_of_events/get_employees/').then((res) => {
|
axiosApiInstance.get('api/calendar_of_events/get_employees/').then((res) => {
|
||||||
staff.value = res.data.filter((e) => e.employees.length > 0);
|
staff.value = res.data.filter((e) => e.employees.length > 0);
|
||||||
});
|
});
|
||||||
|
|
||||||
const accountStore = useAccountStore();
|
// const newStaff = computed({
|
||||||
|
// get() {
|
||||||
|
// let ns = [];
|
||||||
|
|
||||||
const newStaff = computed({
|
// let indCal = safeCalendars.value.findIndex((e) => e.id == myCalendar.id);
|
||||||
get() {
|
|
||||||
let ns = [];
|
|
||||||
|
|
||||||
let indCal = propsEvent.value.mycalendars.findIndex((e) => e.id == calendar.value[0].id);
|
// staff.value.forEach((e) => {
|
||||||
|
// let emp = { ...e };
|
||||||
|
// emp.employees = e.employees.filter((n) => {
|
||||||
|
// if (
|
||||||
|
// n.id == accountStore.user.is_idEmployee ||
|
||||||
|
// propsEvent.value.event.participant.some((e) => n.id == e)
|
||||||
|
// ) {
|
||||||
|
// n.selected = true;
|
||||||
|
// }
|
||||||
|
|
||||||
staff.value.forEach((e) => {
|
// return propsEvent.value.mycalendars[indCal].participant.some(
|
||||||
let emp = { ...e };
|
// (part) => part.id === n.id,
|
||||||
emp.employees = e.employees.filter((n) => {
|
// );
|
||||||
if (
|
// });
|
||||||
n.id == accountStore.user.is_idEmployee ||
|
|
||||||
propsEvent.value.event.participant.some((e) => n.id == e)
|
// if (emp.employees.length > 0) ns.push(emp);
|
||||||
) {
|
// });
|
||||||
n.selected = true;
|
// return ns;
|
||||||
|
// },
|
||||||
|
// });
|
||||||
|
|
||||||
|
const newStaff = computed(() => {
|
||||||
|
// Для нового события - все сотрудники, для существующего - фильтр по календарю
|
||||||
|
const isNewEvent = !propsEvent.value.event.id;
|
||||||
|
|
||||||
|
if (isNewEvent) {
|
||||||
|
// НОВОЕ СОБЫТИЕ - все сотрудники
|
||||||
|
const eventParticipantIds = propsEvent.value.event.participant || [];
|
||||||
|
|
||||||
|
return staff.value
|
||||||
|
.map((department) => ({
|
||||||
|
...department,
|
||||||
|
employees: department.employees.map((employee) => ({
|
||||||
|
...employee,
|
||||||
|
selected:
|
||||||
|
employee.id === accountStore.user.is_idEmployee ||
|
||||||
|
eventParticipantIds.includes(employee.id),
|
||||||
|
})),
|
||||||
|
}))
|
||||||
|
.filter((dept) => dept.employees.length > 0);
|
||||||
|
} else {
|
||||||
|
// СУЩЕСТВУЮЩЕЕ СОБЫТИЕ - фильтр по календарю (старая логика)
|
||||||
|
if (!myCalendar.value || !safeCalendars.value.length) return [];
|
||||||
|
|
||||||
|
const currentCalendar = safeCalendars.value.find((cal) => cal.id === myCalendar.value.id);
|
||||||
|
if (!currentCalendar?.participant) return [];
|
||||||
|
|
||||||
|
const calendarParticipantIds = new Set(currentCalendar.participant.map((p) => p.id));
|
||||||
|
const eventParticipantIds = new Set(propsEvent.value.event.participant || []);
|
||||||
|
|
||||||
|
return staff.value
|
||||||
|
.map((dept) => ({
|
||||||
|
...dept,
|
||||||
|
employees: dept.employees
|
||||||
|
.filter((emp) => calendarParticipantIds.has(emp.id))
|
||||||
|
.map((emp) => ({
|
||||||
|
...emp,
|
||||||
|
selected:
|
||||||
|
emp.id === accountStore.user.is_idEmployee || eventParticipantIds.has(emp.id),
|
||||||
|
})),
|
||||||
|
}))
|
||||||
|
.filter((dept) => dept.employees.length > 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
return propsEvent.value.mycalendars[indCal].participant.some(
|
|
||||||
(part) => part.id === n.id,
|
|
||||||
);
|
|
||||||
});
|
|
||||||
|
|
||||||
if (emp.employees.length > 0) ns.push(emp);
|
|
||||||
});
|
|
||||||
return ns;
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const delEvent = (idev) => {
|
const delEvent = (idev) => {
|
||||||
@@ -348,34 +406,48 @@ export default {
|
|||||||
files.value = f;
|
files.value = f;
|
||||||
};
|
};
|
||||||
|
|
||||||
const calendar = computed({
|
// const calendar = computed({
|
||||||
get() {
|
// get() {
|
||||||
let selected = [];
|
// let selected = [];
|
||||||
if (propsEvent.value.event.calendar) {
|
// console.log(propsEvent.value);
|
||||||
let ind = propsEvent.value.mycalendars.findIndex(
|
// if (propsEvent.value.event.calendar) {
|
||||||
(e) => e.id == propsEvent.value.event.calendar,
|
// let ind = propsEvent.value.mycalendars.findIndex(
|
||||||
);
|
// (e) => e.id == propsEvent.value.event.calendar,
|
||||||
selected.push({
|
// );
|
||||||
cl_name: propsEvent.value.mycalendars[ind].cl_name,
|
// selected.push({
|
||||||
cl_color: propsEvent.value.mycalendars[ind].cl_color,
|
// cl_name: propsEvent.value.mycalendars[ind].cl_name,
|
||||||
id: propsEvent.value.mycalendars[ind].id,
|
// cl_color: propsEvent.value.mycalendars[ind].cl_color,
|
||||||
});
|
// id: propsEvent.value.mycalendars[ind].id,
|
||||||
} else {
|
// });
|
||||||
selected.push({
|
// } else {
|
||||||
cl_name: propsEvent.value.mycalendars[0].cl_name,
|
// selected.push({
|
||||||
cl_color: propsEvent.value.mycalendars[0].cl_color,
|
// cl_name: propsEvent.value.mycalendars[0].cl_name,
|
||||||
id: propsEvent.value.mycalendars[0].id,
|
// cl_color: propsEvent.value.mycalendars[0].cl_color,
|
||||||
});
|
// id: propsEvent.value.mycalendars[0].id,
|
||||||
|
// });
|
||||||
|
// }
|
||||||
|
// if (propsEvent.value.event.calendar == null)
|
||||||
|
// propsEvent.value.event.calendar = propsEvent.value.mycalendars[0].id;
|
||||||
|
|
||||||
|
// return selected;
|
||||||
|
// },
|
||||||
|
|
||||||
|
// set(newValue) {
|
||||||
|
// propsEvent.value.event.calendar = newValue.value;
|
||||||
|
// },
|
||||||
|
// });
|
||||||
|
|
||||||
|
const myCalendar = ref(null);
|
||||||
|
|
||||||
|
// Computed property с защитой от undefined
|
||||||
|
const safeCalendars = computed(() => props.myCalendars || []);
|
||||||
|
const isCalendarsReady = computed(() => safeCalendars.value.length > 0);
|
||||||
|
|
||||||
|
// Автовыбор при появлении данных
|
||||||
|
watch(isCalendarsReady, (ready) => {
|
||||||
|
if (ready && !myCalendar.value && safeCalendars.value[0]) {
|
||||||
|
myCalendar.value = safeCalendars.value[0];
|
||||||
}
|
}
|
||||||
if (propsEvent.value.event.calendar == null)
|
|
||||||
propsEvent.value.event.calendar = propsEvent.value.mycalendars[0].id;
|
|
||||||
|
|
||||||
return selected;
|
|
||||||
},
|
|
||||||
|
|
||||||
set(newValue) {
|
|
||||||
propsEvent.value.event.calendar = newValue.value;
|
|
||||||
},
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const titleCard = ref('');
|
const titleCard = ref('');
|
||||||
@@ -436,13 +508,14 @@ export default {
|
|||||||
|
|
||||||
const postData = (validate) => {
|
const postData = (validate) => {
|
||||||
if (validate) {
|
if (validate) {
|
||||||
|
propsEvent.value.event.calendar = myCalendar.value.id;
|
||||||
|
|
||||||
let postEvent = new FormData();
|
let postEvent = new FormData();
|
||||||
postEvent.append('event', JSON.stringify({ ...propsEvent.value.event }));
|
postEvent.append('event', JSON.stringify({ ...propsEvent.value.event }));
|
||||||
if (propsEvent.value.event.files !== null) {
|
if (propsEvent.value.event.files !== null) {
|
||||||
files.value.forEach((e) => postEvent.append('files', e));
|
files.value.forEach((e) => postEvent.append('files', e));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
axiosApiInstance.post('api/calendar_of_events/my_events/', postEvent).then((result) => {
|
axiosApiInstance.post('api/calendar_of_events/my_events/', postEvent).then((result) => {
|
||||||
feedalert.alert = true;
|
feedalert.alert = true;
|
||||||
feedalert.text = result.data.mess;
|
feedalert.text = result.data.mess;
|
||||||
@@ -535,10 +608,6 @@ export default {
|
|||||||
return dates;
|
return dates;
|
||||||
};
|
};
|
||||||
|
|
||||||
const what = (w) => {
|
|
||||||
console.log(w, '?????????????????????????????????');
|
|
||||||
};
|
|
||||||
|
|
||||||
const periodDisplayRule = (idItem) => {
|
const periodDisplayRule = (idItem) => {
|
||||||
if (getNumberOfDays.value >= 7 && idItem == 3) {
|
if (getNumberOfDays.value >= 7 && idItem == 3) {
|
||||||
return true;
|
return true;
|
||||||
@@ -563,7 +632,7 @@ export default {
|
|||||||
newEvent,
|
newEvent,
|
||||||
propsEvent,
|
propsEvent,
|
||||||
dateEvent,
|
dateEvent,
|
||||||
calendar,
|
// calendar,
|
||||||
staff,
|
staff,
|
||||||
newStaff,
|
newStaff,
|
||||||
returnIdParticant,
|
returnIdParticant,
|
||||||
@@ -571,11 +640,15 @@ export default {
|
|||||||
postData,
|
postData,
|
||||||
rules,
|
rules,
|
||||||
delEvent,
|
delEvent,
|
||||||
|
// calendar,
|
||||||
|
myCalendar,
|
||||||
|
safeCalendars,
|
||||||
|
isCalendarsReady,
|
||||||
|
// onCalendarChange,
|
||||||
|
|
||||||
repeatRules,
|
repeatRules,
|
||||||
returnPeriod,
|
returnPeriod,
|
||||||
periodDisplayRule,
|
periodDisplayRule,
|
||||||
what,
|
|
||||||
|
|
||||||
createDialog,
|
createDialog,
|
||||||
getMonthBetweenDates,
|
getMonthBetweenDates,
|
||||||
|
|||||||
@@ -13,7 +13,6 @@ export const useCalendarStore = defineStore('calendarStore', () => {
|
|||||||
const showEventDialog = ref(false);
|
const showEventDialog = ref(false);
|
||||||
const currentView = ref('dayGridMonth'); // Текущий вид календаря
|
const currentView = ref('dayGridMonth'); // Текущий вид календаря
|
||||||
const calendars = ref([]); // Список календарей пользователя
|
const calendars = ref([]); // Список календарей пользователя
|
||||||
const isLoading = ref(false);
|
|
||||||
const lastUpdatedBy = ref(null);
|
const lastUpdatedBy = ref(null);
|
||||||
|
|
||||||
// Массив ID скрытых календарей. Загружаем из localStorage при инициализации.
|
// Массив ID скрытых календарей. Загружаем из localStorage при инициализации.
|
||||||
@@ -113,47 +112,6 @@ export const useCalendarStore = defineStore('calendarStore', () => {
|
|||||||
}));
|
}));
|
||||||
});
|
});
|
||||||
|
|
||||||
// Создание события
|
|
||||||
const createEvent = async (eventData) => {
|
|
||||||
try {
|
|
||||||
const response = await axiosApiInstance.post('api/calendar_of_events/my_events/', eventData);
|
|
||||||
events.value.push(response.data);
|
|
||||||
return response.data;
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Ошибка создания события:', error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Обновление события
|
|
||||||
const updateEvent = async (eventData) => {
|
|
||||||
try {
|
|
||||||
const response = await axiosApiInstance.put(
|
|
||||||
`api/calendar_of_events/my_events/${eventData.id}/`,
|
|
||||||
eventData,
|
|
||||||
);
|
|
||||||
const index = events.value.findIndex((e) => e.id === eventData.id);
|
|
||||||
if (index !== -1) {
|
|
||||||
events.value[index] = response.data;
|
|
||||||
}
|
|
||||||
return response.data;
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Ошибка обновления события:', error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Удаление события
|
|
||||||
const deleteEvent = async (eventId) => {
|
|
||||||
try {
|
|
||||||
await axiosApiInstance.delete(`api/calendar_of_events/my_events/${eventId}/`);
|
|
||||||
events.value = events.value.filter((e) => e.id !== eventId);
|
|
||||||
} catch (error) {
|
|
||||||
console.error('Ошибка удаления события:', error);
|
|
||||||
throw error;
|
|
||||||
}
|
|
||||||
};
|
|
||||||
|
|
||||||
// Открыть диалог создания/редактирования
|
// Открыть диалог создания/редактирования
|
||||||
const openEventDialog = (event = null) => {
|
const openEventDialog = (event = null) => {
|
||||||
selectedEvent.value = event;
|
selectedEvent.value = event;
|
||||||
@@ -174,7 +132,6 @@ export const useCalendarStore = defineStore('calendarStore', () => {
|
|||||||
showEventDialog,
|
showEventDialog,
|
||||||
currentView,
|
currentView,
|
||||||
calendars,
|
calendars,
|
||||||
isLoading,
|
|
||||||
lastUpdatedBy,
|
lastUpdatedBy,
|
||||||
hiddenCalendarIds,
|
hiddenCalendarIds,
|
||||||
|
|
||||||
@@ -191,9 +148,6 @@ export const useCalendarStore = defineStore('calendarStore', () => {
|
|||||||
setCurrentView,
|
setCurrentView,
|
||||||
loadCalendars,
|
loadCalendars,
|
||||||
// loadEvents,
|
// loadEvents,
|
||||||
createEvent,
|
|
||||||
updateEvent,
|
|
||||||
deleteEvent,
|
|
||||||
openEventDialog,
|
openEventDialog,
|
||||||
closeEventDialog,
|
closeEventDialog,
|
||||||
setSelectedDateFromCalendar,
|
setSelectedDateFromCalendar,
|
||||||
|
|||||||
Reference in new issue
Block a user