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