Skip to content

Commit

Permalink
Save weekTarget in firebase
Browse files Browse the repository at this point in the history
  • Loading branch information
Sébastien LeBlanc committed Oct 11, 2023
1 parent c9d1c64 commit ff1c056
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 26 deletions.
18 changes: 11 additions & 7 deletions src/components/WeekSummary.vue
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@
<div>
<div class="text-xs font-bold uppercase opacity-80">{{ $t('Mon objectif') }}</div>
<div v-if="!is_editing" class="mt-1 block text-3xl font-bold tabular-nums">
{{ weekObjective }}
{{ weekTarget }}
</div>
<TimeInput v-else class="mt-1" v-model="objective" mask="99:99" />
<TimeInput v-else class="mt-1" v-model="target" mask="99:99" />
</div>
<button
v-if="!is_editing"
type="button"
class="inline-flex h-10 w-10 flex-shrink-0 items-center justify-center rounded bg-primary-500 font-bold text-white shadow ring-primary-200 transition hover:bg-primary-400 focus:outline-none focus:ring active:bg-primary-600 dark:text-gray-800 dark:ring-gray-600"
@click="is_editing = true"
@click="onEdit"
>
<IEdit class="h-5" />
</button>
Expand Down Expand Up @@ -79,10 +79,10 @@ const store = useIndexStore();
const route = useRoute();
const { weekSummaryColors } = store;
const { weekSummary, weekObjective, weekTotal } = storeToRefs(store);
const { weekSummary, weekTarget, weekTotal } = storeToRefs(store);
const is_editing = ref(false);
const objective = ref(weekObjective.value);
const target = ref('');
const props = withDefaults(
defineProps<{
Expand All @@ -100,9 +100,13 @@ watch(
emit('update:is_open', false);
},
);
function onSave() {
function onEdit() {
is_editing.value = true;
target.value = weekTarget.value;
}
async function onSave() {
await store.updateWeekTarget(target.value);
is_editing.value = false;
weekObjective.value = objective.value;
}
function onClickOutside() {
emit('update:is_open', false);
Expand Down
17 changes: 10 additions & 7 deletions src/stores/auth.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { defineStore } from 'pinia';
import { GoogleAuthProvider } from 'firebase/auth';
export const googleAuthProvider = new GoogleAuthProvider();
import { signInWithPopup, signOut } from 'firebase/auth';
import { signInWithPopup } from 'firebase/auth';
import { useFirebaseAuth } from 'vuefire';
import { useIndexStore } from '@/stores/index';

Expand All @@ -10,12 +10,15 @@ export const useAuthStore = defineStore('auth', () => {
const auth = useFirebaseAuth()!;
const localePath = useLocalePath();

await signInWithPopup(auth, googleAuthProvider);
return navigateTo(
localePath({
name: 'index',
}),
);
return signInWithPopup(auth, googleAuthProvider).then(async (result) => {
const store = useIndexStore();
await store.createUserInfo(result);
navigateTo(
localePath({
name: 'index',
}),
);
});
}
async function logout() {
const localePath = useLocalePath();
Expand Down
63 changes: 51 additions & 12 deletions src/stores/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,19 @@
import { defineStore } from 'pinia';
import { collection, addDoc, updateDoc, deleteDoc, doc, query, where, writeBatch } from 'firebase/firestore';
import {
collection,
addDoc,
updateDoc,
deleteDoc,
doc,
query,
where,
writeBatch,
getDoc,
setDoc,
} from 'firebase/firestore';
import type { QueryDocumentSnapshot, SnapshotOptions, DocumentData } from 'firebase/firestore';
import { firestoreDefaultConverter } from 'vuefire';
import type { UserCredential } from 'firebase/auth';

export const useIndexStore = defineStore('store', () => {
const db = useFirestore();
Expand Down Expand Up @@ -29,8 +41,11 @@ export const useIndexStore = defineStore('store', () => {
const filter = useLocalStorage('filter', ref('daily'));
const selectedTabIndex = useLocalStorage('selectedTabIndex', ref(0));
const sort = useLocalStorage('sort', ref('name'));
const weekObjective = useLocalStorage('weekObjective', ref('40:00'));
const userInfo = useDocument(
computed(() => (user.value?.uid ? doc(collection(db, 'users'), user.value.uid) : null)),
);
const menuOpened = ref(false);
const weekTarget = computed(() => userInfo.value?.weekTarget || '40:00');

const weekStart = computed(() => {
return $moment(selectedDay.value).startOf('week').toDate();
Expand All @@ -39,14 +54,16 @@ export const useIndexStore = defineStore('store', () => {
return $moment(selectedDay.value).endOf('week').toDate();
});
const projects = useCollection<Project>(
computed(() => (user.value ? query(collection(db, 'projects'), where('user', '==', user.value?.uid)) : null)),
computed(() => (user.value ? query(collection(db, 'projects'), where('user', '==', user.value.uid)) : null)),
{
wait: true,
ssrKey: 'projects',
},
);
const priorities = useCollection<Priority>(
computed(() => (user.value ? query(collection(db, 'priorities'), where('user', '==', user.value?.uid)) : null)),
computed(() => (user.value ? query(collection(db, 'priorities'), where('user', '==', user.value.uid)) : null)),
{
wait: true,
ssrKey: 'priorities',
},
);
Expand All @@ -55,13 +72,14 @@ export const useIndexStore = defineStore('store', () => {
user.value
? query(
collection(db, 'entries').withConverter(dateConverter),
where('user', '==', user.value?.uid),
where('user', '==', user.value.uid),
where('date', '>=', weekStart.value),
where('date', '<=', weekEnd.value),
)
: null,
),
{
wait: true,
ssrKey: 'entries',
},
);
Expand All @@ -85,7 +103,7 @@ export const useIndexStore = defineStore('store', () => {
});
});
const weekRemaining = computed((): string => {
return $moment.duration(weekObjective.value).subtract($moment.duration(weekTotal.value)).format('HH:mm', {
return $moment.duration(weekTarget.value).subtract($moment.duration(weekTotal.value)).format('HH:mm', {
trim: false,
});
});
Expand Down Expand Up @@ -175,12 +193,15 @@ export const useIndexStore = defineStore('store', () => {

const weekSummaryColors = (time: string): string => {
const isZero = $moment.duration(time).asHours() === 0;
const isOvertime = $moment.duration(time).asHours() >= $moment.duration(weekObjective.value).asHours() / 5;
const isBelow = $moment.duration(time).asHours() >= $moment.duration(weekObjective.value).asHours() / 5 - 0.5;
const isGoal = $moment.duration(time).asHours() === $moment.duration(weekTarget.value).asHours() / 5;
const isOvertime = $moment.duration(time).asHours() > $moment.duration(weekTarget.value).asHours() / 5;
const isBelow = $moment.duration(time).asHours() >= $moment.duration(weekTarget.value).asHours() / 5 - 0.5;

if (isZero) {
return 'text-gray-400 dark:text-gray-600';
} else if (isOvertime) {
return 'text-lime-500';
} else if (isGoal) {
return 'text-green-500';
} else if (isBelow) {
return 'text-yellow-500';
Expand All @@ -195,7 +216,7 @@ export const useIndexStore = defineStore('store', () => {
async function addEntry(entry: Entry) {
await addDoc(collection(db, 'entries').withConverter(dateConverter), {
...entry,
user: user.value?.uid,
user: user.value!.uid,
project: entry.project?.id ? doc(db, 'projects', entry.project.id) : null,
});
}
Expand All @@ -219,7 +240,7 @@ export const useIndexStore = defineStore('store', () => {
async function addProject(option: Project) {
const project = await addDoc(collection(db, 'projects'), {
name: option.name,
user: user.value?.uid,
user: user.value!.uid,
});
return { id: project.id, name: option.name };
}
Expand Down Expand Up @@ -260,7 +281,7 @@ export const useIndexStore = defineStore('store', () => {
await addDoc(collection(db, 'priorities'), {
name,
completed: false,
user: user.value?.uid,
user: user.value!.uid,
});
}
async function deletePriority(priority: Priority, force = false) {
Expand All @@ -279,10 +300,25 @@ export const useIndexStore = defineStore('store', () => {
alert(t('Aucune priorité complétée à supprimer'));
}
}
async function createUserInfo(result: UserCredential) {
const docRef = doc(db, 'users', result.user.uid);
const docSnap = await getDoc(docRef);
if (!docSnap.exists()) {
await setDoc(docRef, {
weekTarget: '40:00',
});
}
}
async function updateWeekTarget(weekTarget: string) {
await updateDoc(doc(db, 'users', user.value!.uid), {
weekTarget,
});
}
function $reset() {
projects.value = [];
priorities.value = [];
entries.value = [];
userInfo.value = {};
}

return {
Expand All @@ -293,11 +329,12 @@ export const useIndexStore = defineStore('store', () => {
filter,
selectedTabIndex,
sort,
weekObjective,
userInfo,
projects,
entries,
priorities,
// getters
weekTarget,
weekStart,
weekEnd,
todaysEntries,
Expand All @@ -322,6 +359,8 @@ export const useIndexStore = defineStore('store', () => {
addPriority,
deletePriority,
deleteCompletedPriorities,
createUserInfo,
updateWeekTarget,
$reset,
};
});

0 comments on commit ff1c056

Please sign in to comment.