Skip to content

Commit e1598a4

Browse files
committed
Prepopulate logs for admin user correctly
1 parent 65955a3 commit e1598a4

File tree

2 files changed

+21
-11
lines changed

2 files changed

+21
-11
lines changed

src/service/log.service.js

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const createLog = async ({
2020
description,
2121
date,
2222
}) => {
23-
const currentLogs = getLogsFromStoreByUserId(userId);
23+
const currentLogs = await getLogsByUserId(userId);
2424
const newLog = {
2525
id: currentLogs.length + 1,
2626
title,
@@ -30,16 +30,10 @@ const createLog = async ({
3030
date,
3131
};
3232
const newLogs = [...currentLogs, newLog];
33-
localStorageStore.saveToStore(
34-
prefixLogStoreKey(userId),
35-
JSON.stringify(newLogs),
36-
);
33+
saveLogsToStoreByUserId(userId, newLogs);
3734
};
3835

3936
const getLogsByUserId = async (userId) => {
40-
if (userId === 'admin') {
41-
return mockData.LOG_ENTRIES;
42-
}
4337
return getLogsFromStoreByUserId(userId);
4438
};
4539

@@ -65,8 +59,24 @@ const getLogsFromStoreByUserId = (userId) => {
6559
return rawLogs ? JSON.parse(rawLogs) : [];
6660
};
6761

62+
const saveLogsToStoreByUserId = (userId, logs) => {
63+
localStorageStore.saveToStore(
64+
prefixLogStoreKey(userId),
65+
JSON.stringify(logs),
66+
);
67+
};
68+
6869
const prefixLogStoreKey = (userId) => `${CREATED_LOG_STORE_KEY}-${userId}`;
6970

71+
const loadAdminLogs = async () => {
72+
const userId = 'admin';
73+
const adminLogs = await getLogsByUserId('admin');
74+
if (adminLogs.length === 0) {
75+
saveLogsToStoreByUserId(userId, mockData.LOG_ENTRIES);
76+
}
77+
};
78+
loadAdminLogs();
79+
7080
export const logService = {
7181
getMoodOptions,
7282
getPriorityOptions,

src/store/user.store.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ import { makeAutoObservable, runInAction } from 'mobx';
22
import { logService } from '../service/log.service';
33

44
export class UserStore {
5-
id = null
6-
username = null
7-
logs = []
5+
id = null;
6+
username = null;
7+
logs = [];
88

99
constructor(userId) {
1010
this.id = userId;

0 commit comments

Comments
 (0)