Skip to content

Commit

Permalink
user-jobs-ui
Browse files Browse the repository at this point in the history
  • Loading branch information
ArslanYousaf12 committed Oct 31, 2024
1 parent 6f3e51e commit 13b5924
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 13 deletions.
40 changes: 32 additions & 8 deletions lib/src/features/user_job_post/data/firestore_repo.dart
Original file line number Diff line number Diff line change
@@ -1,28 +1,48 @@
// lib/data/repositories/firestore_repository.dart
import 'package:cloud_firestore/cloud_firestore.dart';
import 'package:firebase_auth/firebase_auth.dart';
import 'package:home_front_pk/src/features/user_job_post/domain/job_post_model.dart';

class FirestoreRepository {
final FirebaseFirestore _firestore = FirebaseFirestore.instance;
final FirebaseAuth _auth = FirebaseAuth.instance;

Future<String> createJobPost(JobPost jobPost) async {
String? get currentUserId => _auth.currentUser?.uid;

Future<void> createJobPost(JobPost jobPost) async {
try {
final docRef = await _firestore.collection('jobs').add(jobPost.toJson());
return docRef.id;
if (currentUserId == null) {
throw Exception('No user logged in');
}

// Create a new document reference
final docRef = _firestore.collection('jobs').doc();

// Update the job post with the document ID and current user ID
final updatedJobPost = jobPost.copyWith(
id: docRef.id,
userId: currentUserId,
);

// Set the document data
await docRef.set(updatedJobPost.toJson());
} catch (e) {
throw Exception('Failed to create job post: $e');
}
}

// Add this new method to get user's jobs
Stream<List<JobPost>> getUserJobs(String userId) {
return _firestore
.collection('jobs')
.where('userId', isEqualTo: userId)
.orderBy('createdAt', descending: true)
.snapshots()
.map((snapshot) =>
snapshot.docs.map((doc) => JobPost.fromJson(doc.data())).toList());
.map((snapshot) => snapshot.docs
.map((doc) => JobPost.fromJson({
...doc.data(),
'id': doc.id, // Include the document ID
}))
.toList());
}

Stream<List<JobPost>> getJobPosts(String category) {
Expand All @@ -31,7 +51,11 @@ class FirestoreRepository {
.where('category', isEqualTo: category)
.orderBy('createdAt', descending: true)
.snapshots()
.map((snapshot) =>
snapshot.docs.map((doc) => JobPost.fromJson(doc.data())).toList());
.map((snapshot) => snapshot.docs
.map((doc) => JobPost.fromJson({
...doc.data(),
'id': doc.id, // Include the document ID
}))
.toList());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -168,9 +168,9 @@ class _CreateJobPostScreenState extends ConsumerState<CreateJobPostScreen> {
estimatedCost: _estimatedCost,
budget: _budget,
);
if (mounted && !jobPostState.hasError) {
context.goNamed(AppRoute.userJobs.name);
}

// ignore: use_build_context_synchronously
context.goNamed(AppRoute.userJobs.name);
}
},
child: jobPostState.isLoading
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,25 @@ final userJobsControllerProvider =
class UserJobsController extends StateNotifier<AsyncValue<List<JobPost>>> {
final Ref _ref;
StreamSubscription<List<JobPost>>? _subscription;
StreamSubscription<User?>? _authSubscription;

UserJobsController(this._ref) : super(const AsyncValue.loading()) {
loadUserJobs();
// Listen to auth state changes
_authSubscription = FirebaseAuth.instance.authStateChanges().listen((user) {
if (user != null) {
loadUserJobs();
} else {
state = const AsyncValue.data([]);
}
});
}

void loadUserJobs() {
final userId = FirebaseAuth.instance.currentUser!.uid;
final userId = FirebaseAuth.instance.currentUser?.uid;
if (userId == null) {
state = const AsyncValue.error('No user logged in', StackTrace.empty);
return;
}

state = const AsyncValue.loading();
_subscription?.cancel();
Expand All @@ -34,6 +46,7 @@ class UserJobsController extends StateNotifier<AsyncValue<List<JobPost>>> {
@override
void dispose() {
_subscription?.cancel();
_authSubscription?.cancel();
super.dispose();
}
}

0 comments on commit 13b5924

Please sign in to comment.