frontend: introduce the sleep records api calls & types
Pedro Lucas Porcellis porcellis@eletrotupi.com · 5 months ago
5e76badbf59459f4d50ea9ca724fedfd03139737 Browse filesParents
04f35e5Changed files
3
Additions
+45
Deletions
-2
frontend/lib/api/client.ts
1 1
import * as SecureStore from 'expo-secure-store';
2 2
import {
3 3
User,
4 4
AuthResponse,
5 5
VerifyTokenResponse,
8 8
ResetPasswordResponse,
9 9
UserUpdatePayload,
10 10
UserUpdateResponse,
11 11
MoodComponentPayload,
12 12
MoodEntryPayload,
13 +
SleepRecordPayload,
13 14
PaginatedResponse
14 15
} from '@/lib/api/types';
15 16
16 17
const API_BASE_URL = process.env.EXPO_PUBLIC_API_BASE_URL;
17 18
const TOKEN_KEY = process.env.EXPO_PUBLIC_TOKEN_KEY;
179 180
return this.request(`/moods/${id}`);
180 181
}
181 182
182 183
async deleteMoodEntry(id: string): Promise<void> {
183 184
return this.request(`/moods/${id}`, { method: 'DELETE' });
185 +
}
186 +
187 +
// Sleep Record
188 +
async createSleepRecord(sleepRecord: CreateSleepRecordPayload): Promise<SleepRecordResponse> {
189 +
return await this.request(`/sleep_records`, {
190 +
method: 'POST',
191 +
body: JSON.stringify({ sleepRecord })
192 +
})
193 +
}
194 +
195 +
async getSleepRecords(params?: {
196 +
from?: string;
197 +
to?: string;
198 +
page?: number;
199 +
limit?: number;
200 +
}): Promise<PaginatedResponse<SleepRecord>> {
201 +
const query = new URLSearchParams();
202 +
if (params?.from) query.set('from', params.from);
203 +
if (params?.to) query.set('to', params.to);
204 +
if (params?.page) query.set('page', String(params.page));
205 +
if (params?.limit) query.set('limit', String(params.limit));
206 +
207 +
const qs = query.toString();
208 +
return this.request(`/sleep_records${qs ? `?${qs}` : ''}`);
184 209
}
185 210
}
186 211
187 212
export const apiClient = new ApiClient();
frontend/lib/api/index.ts
1 1
export {
2 2
apiClient
3 3
} from '@/lib/api/client';
4 4
5 5
export type {
6 6
User,
7 7
AuthResponse,
8 -
MoodEntryPayload,
8 +
CreateMoodEntryPayload,
9 9
MoodComponentPayload,
10 10
PaginatedResponse,
11 -
UserUpdateResponse
11 +
UserUpdateResponse,
12 +
SleepRecordPayload,
12 13
} from '@/lib/api/types';
frontend/lib/api/types.ts
1 1
export interface User {
2 2
id: number;
3 3
email: string;
4 4
firstName: string;
5 5
lastName?: string;
81 81
moodComponents: MoodComponent[];
82 82
createdAt: Date;
83 83
updatedAt: Date;
84 84
}
85 85
86 +
// Sleep Records
87 +
88 +
export interface SleepRecordPayload {
89 +
average: number;
90 +
annotations: string;
91 +
date: Date;
92 +
};
93 +
94 +
export interface SleepRecord {
95 +
id: number;
96 +
annotations: string;
97 +
date: Date;
98 +
average: number;
99 +
createdAt: Date;
100 +
updatedAt: Date;
101 +
}
102 +
86 103
// Generic stuff
87 104
export interface PaginatedResponse<T> {
88 105
entries: T[];
89 106
total: number;
90 107
page: number;
91 108
nextPage: number | null;
92 109
};