Skip to content

Commit

Permalink
Delete projects
Browse files Browse the repository at this point in the history
  • Loading branch information
Sébastien LeBlanc committed Oct 7, 2023
1 parent cb96e06 commit 3c5ac59
Show file tree
Hide file tree
Showing 6 changed files with 121 additions and 165 deletions.
3 changes: 0 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
"@types/file-saver": "^2.0.5",
"@types/inputmask": "^5.0.3",
"@types/moment-duration-format": "^2.2.3",
"@types/uuid": "^9.0.0",
"@vueuse/components": "^10.3.0",
"@vueuse/core": "^10.3.0",
"@vueuse/nuxt": "^10.3.0",
Expand All @@ -32,15 +31,13 @@
"@tailwindcss/forms": "^0.5.3",
"@types/node": "^20.5.0",
"@vueform/multiselect": "^2.5.8",
"file-saver": "^2.0.5",
"firebase": "^10.4.0",
"inputmask": "^5.0.7",
"linkify-string": "^4.1.0",
"linkifyjs": "^4.1.0",
"moment": "^2.29.4",
"moment-duration-format": "^2.3.2",
"nuxt-vuefire": "^0.4.0",
"uuid": "^9.0.0",
"vue-draggable-next": "^2.1.1",
"vuefire": "^3.1.17"
}
Expand Down
16 changes: 1 addition & 15 deletions src/components/WeekSummary.vue
Original file line number Diff line number Diff line change
Expand Up @@ -61,23 +61,13 @@
</div>
</div>
</div>
<button
class="inline-flex h-9 cursor-pointer items-center justify-center gap-2 whitespace-nowrap rounded bg-primary-500 px-3 py-2 text-sm font-bold text-white shadow ring-primary-200 transition hover:bg-primary-400 focus:outline-none focus:ring disabled:pointer-events-none disabled:cursor-not-allowed disabled:opacity-50 dark:text-gray-900 dark:ring-gray-600"
type="button"
@click="onDownload"
>
{{ $t('Télécharger') }}
<IDownload class="w-5 shrink-0" />
</button>
</div>
</Transition>
</template>

<script lang="ts" setup>
import ISave from '@/assets/svg/save.svg?component';
import IEdit from '@/assets/svg/edit.svg?component';
import IDownload from '@/assets/svg/download.svg?component';
import ISignOut from '@/assets/svg/signout.svg?component';
import { useIndexStore } from '@/stores/index';
import { storeToRefs } from 'pinia';
Expand All @@ -90,7 +80,7 @@ const user = useCurrentUser();
const store = useIndexStore();
const route = useRoute();
const { weekSummaryColors, downloadAndReset } = store;
const { weekSummaryColors } = store;
const { weekSummary, weekObjective, weekTotal } = storeToRefs(store);
const is_editing = ref(false);
Expand All @@ -116,10 +106,6 @@ function onSave() {
is_editing.value = false;
weekObjective.value = objective.value;
}
function onDownload() {
emit('update:is_open', false);
downloadAndReset();
}
function onClickOutside() {
emit('update:is_open', false);
}
Expand Down
4 changes: 1 addition & 3 deletions src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,7 @@
"Arrêter": "Stop",
"Ajouter": "Add",
"Annuler": "Cancel",
"Interne": "Internal",
"Êtes vous certain de vouloir supprimer cette entrée ?": "Are you sure you want to delete this entry?",
"Voulez-vous réinitialisé votre feuille de temps ? Ceci remettera à zero votre entrées de temps, mais conservera vos projets.": "Do you want to reset your timesheet ? This will reset all your entries, but will preserve all your projects.",
"Résumé": "Summary",
"Projets": "Projects",
"Priorités": "Priorities",
Expand All @@ -25,7 +23,7 @@
"Aucune donnée pour l'instant": "Nothing to display for now",
"Aucune entrée | {n} entrée | {n} entrées": "No entries | {n} entry | {n} entries",
"Êtes vous certain de vouloir supprimer ce projet ?": "Are you sure you want to delete this project?",
"Voulez-vous supprimer l'entrée reliée ? | Voulez-vous supprimer les {n} entrées reliées ?": "Do you wish to delete the linked entry? | Do you wish to delete the {n} linked entries?",
"Cela entrainera la supression de l'entrée liée, êtes-vous certain de vouloir continuer ? | Cela entrainera la supression des {n} entrées liées, êtes-vous certain de vouloir continuer ?": "This will delete the linked entry, do you wish to continue? | This will delete the {n} linked entries, do you wish to continue?",
"Cleanup": "Cleanup",
"Priorité": "Priority",
"Êtes vous certain de vouloir supprimer cette priorité ?": "Are you sure you want to delete this priority?",
Expand Down
5 changes: 2 additions & 3 deletions src/pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,14 @@
<TimeEntriesHeader />
<div class="mx-auto flex w-full flex-col items-center gap-2">
<TimeEntry v-for="(entry, index) in todaysEntries" :key="entry.id" :entry="entry" />
<TimeEntry v-if="canCreateEntry" :key="key" @add="key = uuidv4()" />
<TimeEntry v-if="canCreateEntry" :key="key" @add="key = Date.now()" />
</div>
</div>
<SummarySidebar />
</div>
</template>

<script lang="ts" setup>
import { v4 as uuidv4 } from 'uuid';
import { useIndexStore } from '@/stores/index';
import { storeToRefs } from 'pinia';
import { usePendingPromises } from 'vuefire';
Expand All @@ -22,5 +21,5 @@ await usePendingPromises();
const { todaysEntries, canCreateEntry } = storeToRefs(store);
const key = ref(uuidv4());
const key = ref(Date.now());
</script>
131 changes: 61 additions & 70 deletions src/stores/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import { defineStore } from 'pinia';
import fileSaver from 'file-saver';
const { saveAs } = fileSaver;
import { collection, addDoc, updateDoc, deleteDoc, doc, query, where, Timestamp } from 'firebase/firestore';
import { collection, addDoc, updateDoc, deleteDoc, doc, query, where, writeBatch } from 'firebase/firestore';
import type { QueryDocumentSnapshot, SnapshotOptions, DocumentData } from 'firebase/firestore';

export const useIndexStore = defineStore('store', () => {
Expand All @@ -15,7 +13,7 @@ export const useIndexStore = defineStore('store', () => {
toFirestore(entry: Entry): DocumentData {
return {
...entry,
date: $moment(entry.date, 'YYYY-MM-DD').toDate(),
date: $moment(entry.date).startOf('day').toDate(),
};
},
fromFirestore(snapshot: QueryDocumentSnapshot, options: SnapshotOptions): DocumentData {
Expand All @@ -24,8 +22,15 @@ export const useIndexStore = defineStore('store', () => {
},
};

const menuOpened = ref(false);
const selectedDay = ref(new Date().toLocaleDateString('en-CA'));
const weekStart = computed(() => {
return $moment(selectedDay.value).startOf('week').toDate();
});
const weekEnd = computed(() => {
return $moment(selectedDay.value).endOf('week').toDate();
});

const menuOpened = ref(false);
const filter = ref('daily');
const selectedTabIndex = ref(1);
const sort = ref('entries');
Expand All @@ -40,46 +45,42 @@ export const useIndexStore = defineStore('store', () => {
ssrKey: 'priorities',
},
);
// const entries = useCollection<Entry>(
// () =>
// query(
// collection(db, 'entries').withConverter(dateConverter),
// where('user', '==', user.value?.uid),
// where('date', '>', weekStart.value),
// where('date', '<', weekEnd.value),
// ),
// {
// ssrKey: 'entries',
// },
// );
const entries = useCollection<Entry>(
query(
collection(db, 'entries').withConverter(dateConverter),
where('user', '==', user.value?.uid),
where('date', '>', $moment(selectedDay.value).startOf('week').toDate()),
where('date', '<', $moment(selectedDay.value).endOf('week').toDate()),
where('date', '>', weekStart.value),
where('date', '<', weekEnd.value),
),
{
ssrKey: 'entries',
},
);
watch(
() => selectedDay.value,
() => {
entries.value = useCollection<Entry>(
query(
collection(db, 'entries').withConverter(dateConverter),
where('user', '==', user.value?.uid),
where('date', '>', $moment(selectedDay.value).startOf('week').toDate()),
where('date', '<', $moment(selectedDay.value).endOf('week').toDate()),
),
{
ssrKey: 'entries',
},
).value;
},
);
// const entries = computed(() => {
// return useCollection<Entry>(
// query(
// collection(db, 'entries').withConverter(dateConverter),
// where('user', '==', user.value?.uid),
// where('date', '>', $moment(selectedDay.value).startOf('week').toDate()),
// where('date', '<', $moment(selectedDay.value).endOf('week').toDate()),
// ),
// {
// ssrKey: 'entries',
// },
// ).value;
// });
watch(selectedDay, () => {
entries.value = useCollection<Entry>(
query(
collection(db, 'entries').withConverter(dateConverter),
where('user', '==', user.value?.uid),
where('date', '>', weekStart.value),
where('date', '<', weekEnd.value),
),
{
ssrKey: 'entries',
},
).value;
});

const todaysEntries = computed((): Entry[] => {
return [...entries.value]
Expand Down Expand Up @@ -224,11 +225,10 @@ export const useIndexStore = defineStore('store', () => {
};

async function addEntry(entry: Entry) {
await addDoc(collection(db, 'entries'), {
await addDoc(collection(db, 'entries').withConverter(dateConverter), {
...entry,
user: user.value?.uid,
project: doc(db, 'projects', entry.project.id),
date: $moment(entry.date).startOf('day').toDate(),
});
}
async function updateEntry(entry: Entry) {
Expand All @@ -255,20 +255,34 @@ export const useIndexStore = defineStore('store', () => {
}
async function deleteProject(project: Project) {
if (confirm(t('Êtes vous certain de vouloir supprimer ce projet ?'))) {
await deleteDoc(doc(db, 'projects', project.id));
const linkedEntries = projectEntriesTotal(project);

if (linkedEntries > 0) {
const projectRef = doc(db, 'projects', project.id);
const { promise: linkedEntriesPromise } = await useCollection<Entry>(
query(collection(db, 'entries'), where('project', '==', projectRef)),
{
once: true,
wait: true,
ssrKey: 'projectEntries',
},
);
const batch = writeBatch(db);
const linkedEntries = await linkedEntriesPromise.value;
if (linkedEntries.length > 0) {
if (
confirm(
t(
"Voulez-vous supprimer l'entrée reliée ? | Voulez-vous supprimer les {n} entrées reliées ?",
linkedEntries,
"Cela entrainera la supression de l'entrée liée, êtes-vous certain de vouloir continuer ? | Cela entrainera la supression des {n} entrées liées, êtes-vous certain de vouloir continuer ?",
linkedEntries.length,
),
)
) {
entries.value.filter((e) => e.project.id === project.id).forEach((e) => deleteEntry(e, true));
linkedEntries.forEach((entry) => {
batch.delete(doc(db, 'entries', entry.id));
});
await batch.commit();
await deleteDoc(projectRef);
}
} else {
await deleteDoc(projectRef);
}
}
}
Expand All @@ -295,30 +309,6 @@ export const useIndexStore = defineStore('store', () => {
alert(t('Aucune priorité complétée à supprimer'));
}
}
// TODO
function downloadAndReset() {
const data = {
entries: entries.value,
weekSummary: weekSummary.value,
weekTotal: weekTotal.value,
};

var blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
saveAs(blob, `semaine-${$moment(selectedDay.value).week()}.json`);

setTimeout(() => {
if (
confirm(
t(
'Voulez-vous réinitialisé votre feuille de temps ? Ceci remettera à zero votre entrées de temps, mais conservera vos projets.',
),
)
) {
entries.value = [];
}
priorities.value = [];
}, 100);
}

return {
menuOpened,
Expand All @@ -331,6 +321,8 @@ export const useIndexStore = defineStore('store', () => {
entries,
priorities,
//
weekStart,
weekEnd,
todaysEntries,
weekTotal,
weekRemaining,
Expand All @@ -353,6 +345,5 @@ export const useIndexStore = defineStore('store', () => {
addPriority,
deletePriority,
deleteCompletedPriorities,
downloadAndReset,
};
});
Loading

0 comments on commit 3c5ac59

Please sign in to comment.