| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291 |
- import axios from 'axios';
- import { Alert } from 'react-native';
- import * as SecureStore from 'expo-secure-store';
- import { EXPO_PUBLIC_API_URL } from '@env';
- import { forgetPasswordFormData } from '../types/signup';
- import { CustomerData } from '../types/signUpFormData';
- class ChargeStationService {
- private apiUrl: string;
- constructor() {
- this.apiUrl = EXPO_PUBLIC_API_URL;
- if (!this.apiUrl) {
- throw new Error('API URL is not defined in environment variables');
- }
- }
- async fetchCarBrand() {
- try {
- const response = await axios.get(
- `${this.apiUrl}/public/client/car/brand`
- );
- if (response.status === 200 || response.status === 201) {
- // console.log(response.data);
- return response.data;
- } else {
- console.log('invalid response');
- }
- } catch (error) {
- if (axios.isAxiosError(error)) {
- console.error(
- 'error:',
- error.response?.data?.message || error.message
- );
- } else {
- console.error('An unexpected error occurred:', error);
- }
- return false;
- }
- }
- async getUserCars() {
- try {
- const response = await axios.get(
- `${this.apiUrl}/clients/customer/car/all`,
- {
- headers: {
- Authorization: `Bearer ${await SecureStore.getItemAsync(
- 'accessToken'
- )}`
- }
- }
- );
- if (response.status === 200 || response.status === 201) {
- // console.log(response.data.data);
- return response.data;
- } else {
- console.log('invalid response');
- }
- } catch (error) {
- console.log(error);
- }
- }
- async addCar(
- licensePlate: string,
- carBrandFk: string,
- carTypeFk: string,
- isDefault: boolean
- ) {
- try {
- const response = await axios.post(
- `${this.apiUrl}/clients/customer/car`,
- {
- licensePlate: licensePlate,
- carBrandFk: carBrandFk,
- carTypeFk: carTypeFk,
- isDefault: isDefault
- },
- {
- headers: {
- Authorization: `Bearer ${await SecureStore.getItemAsync(
- 'accessToken'
- )}`
- }
- }
- );
- if (response.status === 200 || response.status === 201) {
- console.log('add car successful');
- return true;
- } else {
- console.log('invalid response');
- }
- } catch (error) {
- if (axios.isAxiosError(error)) {
- console.error(
- 'error:',
- error.response?.data?.message || error.message
- );
- } else {
- console.error('An unexpected error occurred:', error);
- }
- return false;
- }
- }
- async deleteCar(carID) {
- try {
- // console.log('i receive this carID', carID);
- const response = await axios.delete(
- `${this.apiUrl}/clients/customer/car?carId=${carID}`,
- {
- headers: {
- Authorization: `Bearer ${await SecureStore.getItemAsync(
- 'accessToken'
- )}`
- }
- }
- );
- // console.log('Full response:', JSON.stringify(response, null, 2));
- if (response.status === 200 || response.status === 201) {
- console.log('delete car successful');
- return true;
- } else {
- console.log('invalid response');
- }
- } catch (error) {
- console.log(error);
- }
- }
- async setDefaultCar(carID) {
- try {
- const response = await axios.put(
- `${this.apiUrl}/clients/customer/car/default?carId=${carID}`,
- {},
- {
- headers: {
- Authorization: `Bearer ${await SecureStore.getItemAsync(
- 'accessToken'
- )}`
- }
- }
- );
- if (response.status === 200 || response.status === 201) {
- console.log('set default car successful');
- return true;
- } else {
- console.log('invalid response');
- }
- } catch (error) {
- console.log(error);
- }
- }
- async fetchChargeStations() {
- try {
- const response = await axios.get(
- `${this.apiUrl}/clients/chargestations/resources/info`,
- {
- headers: {
- Authorization: `Bearer ${await SecureStore.getItemAsync(
- 'accessToken'
- )}`
- }
- }
- );
- if (response.status === 200 || response.status === 201) {
- return response.data.data.map((station, index) => {
- const {
- Address,
- StationName,
- StationID,
- StationLng,
- StationLat
- } = station.snapshot;
- return {
- Address,
- StationName,
- StationID,
- StationLng,
- StationLat
- };
- });
- } else {
- console.log('invalid response');
- }
- } catch (error) {
- if (axios.isAxiosError(error)) {
- console.error(
- 'Login error:',
- error.response?.data?.message || error.message
- );
- } else {
- console.error('An unexpected error occurred:', error);
- }
- return false;
- }
- }
- async fetchChargeStationPrice(stationID: string) {
- try {
- const response = await axios.get(
- `${this.apiUrl}/clients/promotion/price?id=${stationID}`
- );
- if (response.status === 200 || response.status === 201) {
- return response.data.price;
- } else {
- console.log('invalid response');
- }
- } catch (error) {
- if (axios.isAxiosError(error)) {
- console.error(
- 'Login error:',
- error.response?.data?.message || error.message
- );
- } else {
- console.error('An unexpected error occurred:', error);
- }
- return false;
- }
- }
- async fetchAvailableDates(stationID: string) {
- try {
- const response = await axios.get(
- `${this.apiUrl}/clients/reservation/connectors/${stationID}`,
- {
- headers: {
- Authorization: `Bearer ${await SecureStore.getItemAsync(
- 'accessToken'
- )}`
- }
- }
- );
- if (response.status === 200 || response.status === 201) {
- const dates = response.data.map((i) => i.date);
- return dates;
- } else {
- console.log('invalid response');
- }
- } catch (error) {
- if (axios.isAxiosError(error)) {
- console.error(
- 'Login error:',
- error.response?.data?.message || error.message
- );
- } else {
- console.error('An unexpected error occurred:', error);
- }
- return false;
- }
- }
- async fetchAvailableTimeSlots(stationID: string, targetDate: string) {
- try {
- const response = await axios.get(
- `${this.apiUrl}/clients/reservation/connectors/${stationID}`,
- {
- headers: {
- Authorization: `Bearer ${await SecureStore.getItemAsync(
- 'accessToken'
- )}`
- }
- }
- );
- if (response.status === 200 || response.status === 201) {
- const times = response.data.find((i) => i.date === targetDate);
- if (times) {
- const availableTimeSlots = times.range.map((i) => i.start);
- return availableTimeSlots;
- }
- } else {
- console.log('invalid response');
- }
- } catch (error) {
- if (axios.isAxiosError(error)) {
- console.error(
- 'Login error:',
- error.response?.data?.message || error.message
- );
- } else {
- console.error('An unexpected error occurred:', error);
- }
- return false;
- }
- }
- }
- export const chargeStationService = new ChargeStationService();
|