@umituz/react-native-gamification 1.2.0 → 1.2.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@umituz/react-native-gamification",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "description": "Comprehensive gamification system for React Native apps with achievements, points, levels, streaks, leaderboards, rewards, and progress tracking",
5
5
  "main": "./src/index.ts",
6
6
  "types": "./src/index.ts",
@@ -41,7 +41,7 @@ interface GamificationStore {
41
41
 
42
42
  // Actions - Achievements
43
43
  loadAchievements: () => Promise<void>;
44
- updateAchievementProgress: (achievementId: string, progress: number) => Promise<void>;
44
+ updateAchievementProgress: (achievementId: string, progressIncrement: number) => Promise<void>;
45
45
  unlockAchievement: (achievementId: string) => Promise<void>;
46
46
 
47
47
  // Actions - Points
@@ -140,17 +140,30 @@ export const useGamificationStore = create<GamificationStore>((set, get) => ({
140
140
  }
141
141
  },
142
142
 
143
- updateAchievementProgress: async (achievementId: string, progress: number) => {
143
+ updateAchievementProgress: async (achievementId: string, progressIncrement: number) => {
144
144
  const { userId, repository } = get();
145
145
  if (!userId) return;
146
- const result = await repository.updateAchievementProgress(userId, achievementId, progress);
146
+
147
+ // Find existing achievement to get current progress
148
+ const existingAchievement = get().achievements.find((a) => a.id === achievementId);
149
+ if (!existingAchievement) {
150
+ /* eslint-disable-next-line no-console */
151
+ if (__DEV__) console.warn(`Achievement ${achievementId} not found, cannot update progress`);
152
+ return;
153
+ }
154
+
155
+ // Calculate new progress (add increment to current progress)
156
+ const currentProgress = existingAchievement.progress || 0;
157
+ const newProgress = Math.min(currentProgress + progressIncrement, existingAchievement.requirement);
158
+
159
+ const result = await repository.updateAchievementProgress(userId, achievementId, newProgress);
147
160
  if (result.success) {
148
161
  const achievements = get().achievements.map((a) =>
149
162
  a.id === achievementId ? result.data : a,
150
163
  );
151
164
  set({ achievements });
152
165
  // Check if achievement should be unlocked
153
- if (progress >= result.data.requirement && !result.data.unlocked) {
166
+ if (newProgress >= result.data.requirement && !result.data.unlocked) {
154
167
  await get().unlockAchievement(achievementId);
155
168
  }
156
169
  }