Untitled
unknown
plain_text
a year ago
8.8 kB
5
Indexable
// Customizable Area Start import { IBlock } from '../../../framework/src/IBlock'; import { Message } from '../../../framework/src/Message'; import { BlockComponent } from '../../../framework/src/BlockComponent'; import MessageEnum, { getName, } from '../../../framework/src/Messages/MessageEnum'; import { runEngine } from '../../../framework/src/RunEngine'; export const configJSON = require('./config'); import { handleResponseMessage } from '../../utilities/src/handle-response-message'; import { getStorageData, removeStorageData, } from '../../../framework/src/Utilities'; import createRequestMessage from '../../utilities/src/create-request-message'; interface ErrorType2 { error: string; } interface ErrorType extends ErrorType2 { errors: [{ token: string }]; } interface ReviewType { profilePicture: string; fullName: string; pastDays: string | number; comment: string; } interface ResponseType { account: { data: { attributes: { full_name: string; joined: string; about: string; profile_image_url: { url: string; }; verified: { email: boolean; phone: boolean; identity_verified: boolean; }; }; }; }; reviews: { total_average_rating: number; reviews_count: number; all_rating_stars: { star_1: number; star_2: number; star_3: number; star_4: number; star_5: number; }; }; render_trip_completed: number; } // Customizable Area End export interface Props { // Customizable Area Start navigation: any; // Customizable Area End } interface S { // Customizable Area Start token: string; loader: boolean; showVerifiedInformation: boolean; showTravelHistory: boolean; profileId: string | number; ratingsData: { averageRating: string | number; totalRatings: string | number; '1': number; '2': number; '3': number; '4': number; '5': number; }; userData: { profilePicture: string; fullName: string; joinedYear: string | number; totalTrips: string | number; aboutMe: string; }; verifiedInformationData: { email: boolean; phone: boolean; identity: boolean; }; travelHistoryData: { totalTrips: string | number; destinations: string | string[]; }; reviews: ReviewType[]; // Customizable Area End } interface SS { // Customizable Area Start // Customizable Area End } export default class HostProfileController extends BlockComponent< Props, S, SS > { // Customizable Area Start getUserDetailsApiCallId: string = ''; unsubscribe: { addListener: (e: Function) => void; remove: () => void; } = { remove: () => {}, addListener: () => {} }; // Customizable Area End constructor(props: Props) { super(props); this.receive = this.receive.bind(this); // Customizable Area Start this.subScribedMessages = [ getName(MessageEnum.RestAPIResponceMessage), getName(MessageEnum.NavigationPayLoadMessage), ]; this.state = { token: '', loader: true, showVerifiedInformation: true, showTravelHistory: true, profileId: '', ratingsData: { averageRating: 0, totalRatings: 0, '1': 0, '2': 0, '3': 0, '4': 0, '5': 0, }, userData: { profilePicture: '', fullName: '', joinedYear: 0, totalTrips: 0, aboutMe: '', }, verifiedInformationData: { email: false, phone: false, identity: false, }, travelHistoryData: { totalTrips: 0, destinations: '', }, reviews: [], }; // Customizable Area End runEngine.attachBuildingBlock(this as IBlock, this.subScribedMessages); } // Customizable Area Start async componentDidMount() { super.componentDidMount(); this.getToken(); this.unsubscribe = this.props.navigation.addListener( 'didFocus', async () => { this.getToken(); } ); } async componentWillUnmount() { this.unsubscribe.remove(); } async receive(from: string, message: Message) { runEngine.debugLog('Message Recived', message); let apiRequestCallId = message.getData( getName(MessageEnum.RestAPIResponceDataMessage) ); let errorReponse = message.getData( getName(MessageEnum.RestAPIResponceErrorMessage) ); let responseJson = message.getData( getName(MessageEnum.RestAPIResponceSuccessMessage) ); if (getName(MessageEnum.NavigationPayLoadMessage) === message.id) { const userProfileId = message.getData( getName(MessageEnum.SessionResponseData) ); this.setState({ profileId: userProfileId }); } if (getName(MessageEnum.RestAPIResponceMessage) === message.id) { if (apiRequestCallId === this.getUserDetailsApiCallId) { handleResponseMessage({ responseJson, errorJson: errorReponse, onSuccess: () => { this.handleProfileResponse(responseJson); }, onFail: () => { this.handleErrorResponse(responseJson); }, }); } } } getToken = async () => { let token = await getStorageData('usertoken'); const authToken = JSON.parse(token); this.setState({ token: authToken }); if (!token) { this.showAlert('Please Login', 'Please Login to view wishlist'); this.goToLoginPage(); } else { this.getRenterProfileDetails(); } }; handleErrorResponse = (responseJson: ErrorType) => { this.setState({ loader: false }); if (responseJson?.errors?.[0]?.token === 'Invalid token') { this.showAlert('Please Login', 'Please login again to continue.'); this.goToLoginPage(); } else this.showAlert( 'Error', 'Error occured when processing your request, try again later' ); }; goToLoginPage = async () => { await removeStorageData('usertoken'); const message: Message = new Message( getName(MessageEnum.NavigationEmailLogInMessage) ); message.addData(getName(MessageEnum.NavigationPropsMessage), this.props); this.send(message); }; getRenterProfileDetails = () => { this.setState({ loader: true }); const requestMessage = new Message( getName(MessageEnum.RestAPIRequestMessage) ); this.getUserDetailsApiCallId = requestMessage.messageId; const endPoint = `${configJSON.getHostProfile}?id=${this.state.profileId}`; createRequestMessage({ requestMessage: requestMessage, endPoint: endPoint, method: configJSON.validationApiMethodType, token: this.state.token, }); }; handleProfileResponse = (response: ResponseType) => { const { account: { data: { attributes: { full_name, joined, about, profile_image_url: { url }, verified: { email, phone, identity_verified }, }, }, }, reviews: { all_rating_stars: { star_1, star_2, star_3, star_4, star_5 }, reviews_count, total_average_rating, }, render_trip_completed, } = response; const joinedYear = joined.split(',')[1]; this.setState({ ratingsData: { averageRating: total_average_rating ?? 0, totalRatings: reviews_count ?? 0, '1': star_1, '2': star_2, '3': star_3, '4': star_4, '5': star_5, }, userData: { profilePicture: url ?? '', fullName: full_name, joinedYear: joinedYear, totalTrips: render_trip_completed, aboutMe: about ?? 'NA', }, verifiedInformationData: { email: email, phone: phone, identity: identity_verified, }, travelHistoryData: { totalTrips: render_trip_completed, destinations: 'NA', }, reviews: [], }); }; toggleShowVerifiedInformation = () => { this.setState((prev) => ({ ...prev, showVerifiedInformation: !prev.showVerifiedInformation, })); }; toggleshowTravelHistoryInformation = () => { this.setState((prev) => ({ ...prev, showTravelHistory: !prev.showTravelHistory, })); }; returnRatingProgress = (ratingNumber: number) => { return ratingNumber / 100; }; goBack = () => { const message: Message = new Message( getName(MessageEnum.NavigationMessage) ); message.addData( getName(MessageEnum.NavigationTargetMessage), 'UserProfileBasicBlock' ); message.addData(getName(MessageEnum.NavigationPropsMessage), this.props); this.send(message); }; // Customizable Area End }
Editor is loading...
Leave a Comment