chargeStationService.tsx 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. import axios from 'axios';
  2. import { Alert } from 'react-native';
  3. import * as SecureStore from 'expo-secure-store';
  4. import { EXPO_PUBLIC_API_URL } from '@env';
  5. import { forgetPasswordFormData } from '../types/signup';
  6. import { CustomerData } from '../types/signUpFormData';
  7. class ChargeStationService {
  8. private apiUrl: string;
  9. constructor() {
  10. this.apiUrl = EXPO_PUBLIC_API_URL;
  11. if (!this.apiUrl) {
  12. throw new Error('API URL is not defined in environment variables');
  13. }
  14. }
  15. async fetchCarBrand() {
  16. try {
  17. const response = await axios.get(
  18. `${this.apiUrl}/public/client/car/brand`
  19. );
  20. if (response.status === 200 || response.status === 201) {
  21. // console.log(response.data);
  22. return response.data;
  23. } else {
  24. console.log('invalid response');
  25. }
  26. } catch (error) {
  27. if (axios.isAxiosError(error)) {
  28. console.error(
  29. 'error:',
  30. error.response?.data?.message || error.message
  31. );
  32. } else {
  33. console.error('An unexpected error occurred:', error);
  34. }
  35. return false;
  36. }
  37. }
  38. async getUserCars() {
  39. try {
  40. const response = await axios.get(
  41. `${this.apiUrl}/clients/customer/car/all`,
  42. {
  43. headers: {
  44. Authorization: `Bearer ${await SecureStore.getItemAsync(
  45. 'accessToken'
  46. )}`
  47. }
  48. }
  49. );
  50. if (response.status === 200 || response.status === 201) {
  51. // console.log(response.data.data);
  52. return response.data;
  53. } else {
  54. console.log('invalid response');
  55. }
  56. } catch (error) {
  57. console.log(error);
  58. }
  59. }
  60. async addCar(
  61. licensePlate: string,
  62. carBrandFk: string,
  63. carTypeFk: string,
  64. isDefault: boolean
  65. ) {
  66. try {
  67. const response = await axios.post(
  68. `${this.apiUrl}/clients/customer/car`,
  69. {
  70. licensePlate: licensePlate,
  71. carBrandFk: carBrandFk,
  72. carTypeFk: carTypeFk,
  73. isDefault: isDefault
  74. },
  75. {
  76. headers: {
  77. Authorization: `Bearer ${await SecureStore.getItemAsync(
  78. 'accessToken'
  79. )}`
  80. }
  81. }
  82. );
  83. if (response.status === 200 || response.status === 201) {
  84. console.log('add car successful');
  85. return true;
  86. } else {
  87. console.log('invalid response');
  88. }
  89. } catch (error) {
  90. if (axios.isAxiosError(error)) {
  91. console.error(
  92. 'error:',
  93. error.response?.data?.message || error.message
  94. );
  95. } else {
  96. console.error('An unexpected error occurred:', error);
  97. }
  98. return false;
  99. }
  100. }
  101. async deleteCar(carID) {
  102. try {
  103. // console.log('i receive this carID', carID);
  104. const response = await axios.delete(
  105. `${this.apiUrl}/clients/customer/car?carId=${carID}`,
  106. {
  107. headers: {
  108. Authorization: `Bearer ${await SecureStore.getItemAsync(
  109. 'accessToken'
  110. )}`
  111. }
  112. }
  113. );
  114. // console.log('Full response:', JSON.stringify(response, null, 2));
  115. if (response.status === 200 || response.status === 201) {
  116. console.log('delete car successful');
  117. return true;
  118. } else {
  119. console.log('invalid response');
  120. }
  121. } catch (error) {
  122. console.log(error);
  123. }
  124. }
  125. async setDefaultCar(carID) {
  126. try {
  127. const response = await axios.put(
  128. `${this.apiUrl}/clients/customer/car/default?carId=${carID}`,
  129. {},
  130. {
  131. headers: {
  132. Authorization: `Bearer ${await SecureStore.getItemAsync(
  133. 'accessToken'
  134. )}`
  135. }
  136. }
  137. );
  138. if (response.status === 200 || response.status === 201) {
  139. console.log('set default car successful');
  140. return true;
  141. } else {
  142. console.log('invalid response');
  143. }
  144. } catch (error) {
  145. console.log(error);
  146. }
  147. }
  148. async fetchChargeStations() {
  149. try {
  150. const response = await axios.get(
  151. `${this.apiUrl}/clients/chargestations/resources/info`,
  152. {
  153. headers: {
  154. Authorization: `Bearer ${await SecureStore.getItemAsync(
  155. 'accessToken'
  156. )}`
  157. }
  158. }
  159. );
  160. if (response.status === 200 || response.status === 201) {
  161. return response.data.data.map((station, index) => {
  162. const {
  163. Address,
  164. StationName,
  165. StationID,
  166. StationLng,
  167. StationLat
  168. } = station.snapshot;
  169. return {
  170. Address,
  171. StationName,
  172. StationID,
  173. StationLng,
  174. StationLat
  175. };
  176. });
  177. } else {
  178. console.log('invalid response');
  179. }
  180. } catch (error) {
  181. if (axios.isAxiosError(error)) {
  182. console.error(
  183. 'Login error:',
  184. error.response?.data?.message || error.message
  185. );
  186. } else {
  187. console.error('An unexpected error occurred:', error);
  188. }
  189. return false;
  190. }
  191. }
  192. async fetchChargeStationPrice(stationID: string) {
  193. try {
  194. const response = await axios.get(
  195. `${this.apiUrl}/clients/promotion/price?id=${stationID}`
  196. );
  197. if (response.status === 200 || response.status === 201) {
  198. return response.data.price;
  199. } else {
  200. console.log('invalid response');
  201. }
  202. } catch (error) {
  203. if (axios.isAxiosError(error)) {
  204. console.error(
  205. 'Login error:',
  206. error.response?.data?.message || error.message
  207. );
  208. } else {
  209. console.error('An unexpected error occurred:', error);
  210. }
  211. return false;
  212. }
  213. }
  214. async fetchAvailableDates(stationID: string) {
  215. try {
  216. const response = await axios.get(
  217. `${this.apiUrl}/clients/reservation/connectors/${stationID}`,
  218. {
  219. headers: {
  220. Authorization: `Bearer ${await SecureStore.getItemAsync(
  221. 'accessToken'
  222. )}`
  223. }
  224. }
  225. );
  226. if (response.status === 200 || response.status === 201) {
  227. const dates = response.data.map((i) => i.date);
  228. return dates;
  229. } else {
  230. console.log('invalid response');
  231. }
  232. } catch (error) {
  233. if (axios.isAxiosError(error)) {
  234. console.error(
  235. 'Login error:',
  236. error.response?.data?.message || error.message
  237. );
  238. } else {
  239. console.error('An unexpected error occurred:', error);
  240. }
  241. return false;
  242. }
  243. }
  244. async fetchAvailableTimeSlots(stationID: string, targetDate: string) {
  245. try {
  246. const response = await axios.get(
  247. `${this.apiUrl}/clients/reservation/connectors/${stationID}`,
  248. {
  249. headers: {
  250. Authorization: `Bearer ${await SecureStore.getItemAsync(
  251. 'accessToken'
  252. )}`
  253. }
  254. }
  255. );
  256. if (response.status === 200 || response.status === 201) {
  257. const times = response.data.find((i) => i.date === targetDate);
  258. if (times) {
  259. const availableTimeSlots = times.range.map((i) => i.start);
  260. return availableTimeSlots;
  261. }
  262. } else {
  263. console.log('invalid response');
  264. }
  265. } catch (error) {
  266. if (axios.isAxiosError(error)) {
  267. console.error(
  268. 'Login error:',
  269. error.response?.data?.message || error.message
  270. );
  271. } else {
  272. console.error('An unexpected error occurred:', error);
  273. }
  274. return false;
  275. }
  276. }
  277. }
  278. export const chargeStationService = new ChargeStationService();