Skip to content

Commit

Permalink
reorganize file structure based on logical function
Browse files Browse the repository at this point in the history
  • Loading branch information
ksmontville committed Aug 23, 2024
1 parent f4ee059 commit 3bc896f
Showing 1 changed file with 115 additions and 108 deletions.
223 changes: 115 additions & 108 deletions src/components/CreateAdministration.vue
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@
</template>

<script setup>
import { onMounted, reactive, ref, toRaw, computed, watch, onUnmounted } from 'vue';
import { computed, onMounted, onUnmounted, reactive, ref, toRaw, watch } from 'vue';
import { useRouter } from 'vue-router';
import { storeToRefs } from 'pinia';
import { useToast } from 'primevue/usetoast';
Expand All @@ -188,13 +188,19 @@ import { useConfirm } from 'primevue/useconfirm';
import ConsentPicker from './ConsentPicker.vue';
const isLevante = import.meta.env.MODE === 'LEVANTE';
const initialized = ref(false);
const router = useRouter();
const toast = useToast();
const confirm = useConfirm();
const queryClient = useQueryClient();
const authStore = useAuthStore();
const { roarfirekit, administrationQueryKeyIndex } = storeToRefs(authStore);
const props = defineProps({
adminId: { type: String, required: false, default: null },
});
const queryClient = useQueryClient();
const header = computed(() => {
if (props.adminId) {
return 'Edit an administration';
Expand All @@ -218,38 +224,16 @@ const submitLabel = computed(() => {
return 'Create Administration';
});
const preExistingAssessmentInfo = computed(() => {
return _get(preExistingAdminInfo.value, 'assessments', []);
});
const router = useRouter();
const toast = useToast();
const initialized = ref(false);
const confirm = useConfirm();
const authStore = useAuthStore();
const { roarfirekit, administrationQueryKeyIndex } = storeToRefs(authStore);
const queryKeys = [
['administration', props.adminId],
['variants', 'all'],
['districts', props.adminId],
['schools', 'minimalOrgs', props.adminId],
['classes', 'minimal', props.adminId],
['groups', props.adminId],
['families', props.adminId],
];
const resetAllQueries = async () => {
for (const key of queryKeys) {
await queryClient.resetQueries(key);
}
};
const invalidateAllQueries = async () => {
for (const key of queryKeys) {
await queryClient.invalidateQueries(key);
}
// +------------------------------------------+
// -----| Queries for grabbing variants |-----
// +------------------------------------------+
const findVariantWithParams = (variants, params) => {
// TODO: implement tie breakers if found.length > 1
return _find(variants, (variant) => {
const cleanVariantParams = removeNull(variant.variant.params);
const cleanInputParams = removeNull(params);
return _isEqual(cleanInputParams, cleanVariantParams);
});
};
const { data: allVariants } = useQuery({
Expand All @@ -263,15 +247,23 @@ const { data: allVariants } = useQuery({
// +------------------------------------------+
// -----| Queries for grabbing pre-existing admins |-----
// +------------------------------------------+
const queryKeys = [
['administration', props.adminId],
['variants', 'all'],
['districts', props.adminId],
['schools', 'minimalOrgs', props.adminId],
['classes', 'minimal', props.adminId],
['groups', props.adminId],
['families', props.adminId],
];
const shouldGrabAdminInfo = computed(() => {
return initialized.value && Boolean(props.adminId);
});
const { data: preExistingAdminInfo } = useQuery({
queryKey: ['administration', props.adminId],
queryFn: () => fetchDocById('administrations', props.adminId),
keepPreviousData: true,
enabled: shouldGrabAdminInfo,
staleTime: 5 * 60 * 1000, // 5 minutes
const preExistingAssessmentInfo = computed(() => {
return _get(preExistingAdminInfo.value, 'assessments', []);
});
// Grab districts from preExistingAdminInfo.minimalOrgs.districts
Expand All @@ -285,16 +277,10 @@ const districtsToGrab = computed(() => {
};
});
});
const shouldGrabDistricts = computed(() => {
return initialized.value && districtsToGrab.value.length > 0;
});
const { data: preDistricts } = useQuery({
queryKey: ['districts', props.adminId],
queryFn: () => fetchDocsById(districtsToGrab.value),
keepPreviousData: true,
enabled: shouldGrabDistricts,
staleTime: 5 * 60 * 1000, // 5 minutes
});
// grab schools from preExistingAdminInfo.minimalOrgs.schools
const schoolsToGrab = computed(() => {
Expand All @@ -307,18 +293,11 @@ const schoolsToGrab = computed(() => {
};
});
});
const shouldGrabSchools = computed(() => {
return initialized.value && schoolsToGrab.value.length > 0;
});
const { data: preSchools } = useQuery({
queryKey: ['schools', 'minimalOrgs', props.adminId],
queryFn: () => fetchDocsById(schoolsToGrab.value),
keepPreviousData: true,
enabled: shouldGrabSchools,
staleTime: 5 * 60 * 1000, // 5 minutes
});
// Grab classes from preExistingAdminInfo.minimalOrgs.classes
const classesToGrab = computed(() => {
const classIds = _get(preExistingAdminInfo.value, 'minimalOrgs.classes', []);
Expand All @@ -330,18 +309,11 @@ const classesToGrab = computed(() => {
};
});
});
const shouldGrabClasses = computed(() => {
return initialized.value && classesToGrab.value.length > 0;
});
const { data: preClasses } = useQuery({
queryKey: ['classes', 'minimal', props.adminId],
queryFn: () => fetchDocsById(classesToGrab.value),
keepPreviousData: true,
enabled: shouldGrabClasses,
staleTime: 5 * 60 * 1000, // 5 minutes
});
// Grab groups from preExistingAdminInfo.minimalOrgs.groups
const groupsToGrab = computed(() => {
const groupIds = _get(preExistingAdminInfo.value, 'minimalOrgs.groups', []);
Expand All @@ -358,14 +330,6 @@ const shouldGrabGroups = computed(() => {
return initialized.value && groupsToGrab.value.length > 0;
});
const { data: preGroups } = useQuery({
queryKey: ['groups', props.adminId],
queryFn: () => fetchDocsById(groupsToGrab.value),
keepPreviousData: true,
enabled: shouldGrabGroups,
staleTime: 5 * 60 * 1000, // 5 minutes
});
// Grab families from preExistingAdminInfo.families
const familiesToGrab = computed(() => {
const familyIds = _get(preExistingAdminInfo.value, 'minimalOrgs.families', []);
Expand All @@ -382,6 +346,58 @@ const shouldGrabFamilies = computed(() => {
return initialized.value && familiesToGrab.value.length > 0;
});
const resetAllQueries = async () => {
for (const key of queryKeys) {
await queryClient.resetQueries(key);
}
};
const invalidateAllQueries = async () => {
for (const key of queryKeys) {
await queryClient.invalidateQueries(key);
}
};
const { data: preExistingAdminInfo } = useQuery({
queryKey: ['administration', props.adminId],
queryFn: () => fetchDocById('administrations', props.adminId),
keepPreviousData: true,
enabled: shouldGrabAdminInfo,
staleTime: 5 * 60 * 1000, // 5 minutes
});
const { data: preDistricts } = useQuery({
queryKey: ['districts', props.adminId],
queryFn: () => fetchDocsById(districtsToGrab.value),
keepPreviousData: true,
enabled: shouldGrabDistricts,
staleTime: 5 * 60 * 1000, // 5 minutes
});
const { data: preSchools } = useQuery({
queryKey: ['schools', 'minimalOrgs', props.adminId],
queryFn: () => fetchDocsById(schoolsToGrab.value),
keepPreviousData: true,
enabled: shouldGrabSchools,
staleTime: 5 * 60 * 1000, // 5 minutes
});
const { data: preClasses } = useQuery({
queryKey: ['classes', 'minimal', props.adminId],
queryFn: () => fetchDocsById(classesToGrab.value),
keepPreviousData: true,
enabled: shouldGrabClasses,
staleTime: 5 * 60 * 1000, // 5 minutes
});
const { data: preGroups } = useQuery({
queryKey: ['groups', props.adminId],
queryFn: () => fetchDocsById(groupsToGrab.value),
keepPreviousData: true,
enabled: shouldGrabGroups,
staleTime: 5 * 60 * 1000, // 5 minutes
});
const { data: preFamilies } = useQuery({
queryKey: ['families', props.adminId],
queryFn: () => fetchDocsById(familiesToGrab.value),
Expand All @@ -393,6 +409,13 @@ const { data: preFamilies } = useQuery({
// +---------------------------------+
// -----| Form state and validation rules |-----
// +---------------------------------+
let noConsent = ref('');
const pickListError = ref('');
const orgError = ref('');
const submitted = ref(false);
const isTestData = ref(false);
const state = reactive({
administrationName: '',
administrationPublicName: '',
Expand All @@ -411,6 +434,18 @@ const state = reactive({
expectedTime: '',
});
const rules = {
administrationName: { required },
administrationPublicName: { required },
dateStarted: { required },
dateClosed: { required },
sequential: { required },
consent: { requiredIf: requiredIf(!isLevante && noConsent.value === '') },
assent: { requiredIf: requiredIf(!isLevante && noConsent.value === '') },
};
const v$ = useVuelidate(rules, state);
const minStartDate = computed(() => {
if (props.adminId && preExistingAdminInfo.value?.dateOpened) {
return new Date(preExistingAdminInfo.value.dateOpened);
Expand All @@ -425,33 +460,9 @@ const minEndDate = computed(() => {
return new Date();
});
let noConsent = ref('');
const rules = {
administrationName: { required },
administrationPublicName: { required },
dateStarted: { required },
dateClosed: { required },
sequential: { required },
consent: { requiredIf: requiredIf(!isLevante && noConsent.value === '') },
assent: { requiredIf: requiredIf(!isLevante && noConsent.value === '') },
};
const v$ = useVuelidate(rules, state);
const pickListError = ref('');
const orgError = ref('');
const submitted = ref(false);
const isTestData = ref(false);
// +---------------------------------+
// -----| Org Selection |-----
// +---------------------------------+
const selection = (selected) => {
for (const [key, value] of _toPairs(selected)) {
state[key] = value;
}
};
const orgsList = computed(() => {
return {
districts: preDistricts.value,
Expand All @@ -462,11 +473,19 @@ const orgsList = computed(() => {
};
});
const selection = (selected) => {
for (const [key, value] of _toPairs(selected)) {
state[key] = value;
}
};
// +---------------------------------+
// -----| Assessment Selection |-----
// +---------------------------------+
const variants = ref([]);
const preSelectedVariants = ref([]);
const nonUniqueTasks = ref('');
const variantsByTaskId = computed(() => {
return _groupBy(allVariants.value, 'task.id');
});
Expand Down Expand Up @@ -496,7 +515,6 @@ const checkForUniqueTasks = (assignments) => {
return uniqueTasks.length === assignments.length;
};
const nonUniqueTasks = ref('');
const getNonUniqueTasks = (assignments) => {
const grouped = _groupBy(assignments, (assignment) => assignment.taskId);
const taskIds = _values(grouped);
Expand All @@ -512,7 +530,6 @@ const checkForRequiredOrgs = (orgs) => {
// +---------------------------------+
// -----| Form submission |-----
// +---------------------------------+
const removeNull = (obj) => {
// eslint-disable-next-line no-unused-vars
return Object.fromEntries(Object.entries(obj).filter(([_, v]) => v !== null));
Expand Down Expand Up @@ -712,16 +729,6 @@ watch(familiesToGrab, async (updatedValue) => {
state.families = fetchedFamilies ?? [];
}
});
function findVariantWithParams(variants, params) {
const found = _find(variants, (variant) => {
const cleanVariantParams = removeNull(variant.variant.params);
const cleanInputParams = removeNull(params);
return _isEqual(cleanInputParams, cleanVariantParams);
});
// TODO: implement tie breakers if found.length > 1
return found;
}
</script>
<style lang="scss">
Expand Down

0 comments on commit 3bc896f

Please sign in to comment.