Skip to content

Commit

Permalink
fix: tag module
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhammedKpln committed Jan 29, 2024
1 parent 3e83540 commit b68b3e2
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 36 deletions.
5 changes: 4 additions & 1 deletion firebase.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
{
"firestore": {
"rules": "firestore.rules"
},
"emulators": {
"auth": {
"port": 9099,
Expand All @@ -7,7 +10,7 @@
"firestore": {
"port": 8080,
"host": "0.0.0.0",
"rules": "firestore.rules"
"rules": "./firestore.rules"
},
"ui": {
"enabled": true
Expand Down
28 changes: 23 additions & 5 deletions firestore.rules
Original file line number Diff line number Diff line change
@@ -1,14 +1,32 @@
rules_version = '2';



service cloud.firestore {
match /databases/{database}/documents {
match /{document=**} {
allow read, write: if true;
}


match /notes/{note} {
allow read: if request.resource.data.expire_at < request.time;
allow write: if request.auth()

allow read: if true;
allow write: if isUserAuthenticated()
}

match /tags/{tag} {
allow read:if true;
allow write: if isUserAuthenticated()
}

match /profiles/{profile} {
allow read:if true;
allow write: if isUserAuthenticated() && request.path == request.auth.uid
}


/* Functions */

function isUserAuthenticated() {
return request.auth.uid != null;
}
}
}
3 changes: 3 additions & 0 deletions scripts/seed_emulator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,9 @@ const profilesSeeder = async () => {
firstName: faker.person.firstName(),
lastName: faker.person.lastName(),
profileImageRef: faker.image.avatar(),
socialMediaAccounts: {
twitter: faker.internet.userName(),
},
});

console.log("Profile seeded");
Expand Down
49 changes: 34 additions & 15 deletions src/modules/tag/Tag.module.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,20 @@ import styles from "@/pages/Tag/Tag.module.scss";
import { noteService } from "@/services/note.service";
import { profileService } from "@/services/profile.service";
import { tagService } from "@/services/tag.service";
import { IonItem, IonLabel, IonList, IonSpinner, IonText } from "@ionic/react";
import {
IonIcon,
IonItem,
IonLabel,
IonList,
IonSpinner,
IonText,
} from "@ionic/react";
import { useQuery } from "@tanstack/react-query";
import {
logoTwitter,
mailOpenOutline,
phonePortraitOutline,
} from "ionicons/icons";
import { useMemo } from "react";
import { Redirect } from "react-router";

Expand Down Expand Up @@ -84,6 +96,7 @@ export default function TagModule(props: TagDetailPageProps) {

<IonList inset className="w-full">
<IonItem>
<IonIcon icon={phonePortraitOutline} slot="start" />
<IonLabel>
<p>Phone</p>
<h6>
Expand All @@ -92,27 +105,33 @@ export default function TagModule(props: TagDetailPageProps) {
</IonLabel>
</IonItem>
<IonItem>
<IonIcon icon={mailOpenOutline} slot="start" />

<IonLabel>
<p>Email</p>
<h6>
<a href="mailto:email@address.com">email@address.com</a>
</h6>
</IonLabel>
</IonItem>
<IonItem>
<IonLabel>
<p>Website</p>
<h6>
<a
href="http://www.yoursite.com"
target="_blank"
rel="noopener noreferrer"
>
www.yoursite.com
</a>
</h6>
</IonLabel>
</IonItem>

{profileData?.socialMediaAccounts?.twitter && (
<IonItem>
<IonIcon icon={logoTwitter} slot="start" />
<IonLabel>
<p>Twitter</p>
<h6>
<a
href={`https://twitter.com/${profileData.socialMediaAccounts.twitter}`}
target="_blank"
rel="noopener noreferrer"
>
{profileData.socialMediaAccounts.twitter}
</a>
</h6>
</IonLabel>
</IonItem>
)}
</IonList>
</div>
);
Expand Down
3 changes: 1 addition & 2 deletions src/services/base.service.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { FirebaseCollections } from "@/models/firebase_collections.model";
import {
CollectionReference,
DocumentData,
Firestore,
PartialWithFieldValue,
QueryDocumentSnapshot,
Expand All @@ -18,7 +17,7 @@ export class BaseService {
this.collectionRef = collection(this.db, collectionReference);
}

protected converter<T = DocumentData>() {
protected converter<T>() {
return {
toFirestore: (data: PartialWithFieldValue<T>) => data,
fromFirestore: (snap: QueryDocumentSnapshot): T => snap.data() as T,
Expand Down
11 changes: 2 additions & 9 deletions src/services/note.service.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,6 @@
import { FirebaseCollections } from "@/models/firebase_collections.model";
import { INote } from "@/models/note.model";
import {
getDocs,
limit,
orderBy,
query,
startAt,
where,
} from "firebase/firestore";
import { getDocs, limit, orderBy, query, where } from "firebase/firestore";
import { BaseService } from "./base.service";

class NoteService extends BaseService {
Expand All @@ -20,7 +13,7 @@ class NoteService extends BaseService {
this.collectionRef,
orderBy("created_at", "desc"),
where("userUid", "==", userUid),
startAt("expire_at", new Date()),
where("expire_at", "==", new Date()),
limit(1)
).withConverter(this.converter<INote>());

Expand Down
8 changes: 4 additions & 4 deletions src/services/profile.service.ts
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
import { FirebaseCollections } from "@/models/firebase_collections.model";
import { IUser } from "@/models/user.model";
import { doc, getDoc } from "firebase/firestore";
import { DocumentSnapshot, doc, getDoc } from "firebase/firestore";
import { BaseService } from "./base.service";

class ProfileService extends BaseService {
constructor() {
super(FirebaseCollections.Profiles);
}

fetchProfile(userUid: string) {
const docRef = doc(this.collectionRef, userUid).withConverter(
this.converter<IUser>()
fetchProfile(userUid: string): Promise<DocumentSnapshot<IUser>> {
const docRef = doc(this.collectionRef, userUid).withConverter<IUser>(
this.converter()
);

try {
Expand Down

0 comments on commit b68b3e2

Please sign in to comment.