import * as _ from "lodash"; import * as React from "react"; import { Avatar, AvatarSize } from "../../components/Avatar"; import { Button, ButtonType } from "../../components/Button"; import { IImageProps } from "../../components/Image"; import { Loading, LoadingType } from "../../components/Loading"; import { Modal, IModalProps } from "../../components/Modal"; import { Pin, PinType } from "../../components/Pin"; import { Reveal, RevealType } from "../../components/Reveal"; import { Upload, IUploadEvent } from "../../components/Upload"; import { SpriteType } from "../../components/Sprite"; interface IFindPhotosOfMeProps { referencePhotoSrc?: string; modal?: IModalProps; findPhotos: (upload: IUploadEvent) => Promise; onClose?: () => void; onFoundImages?: (images: any) => void; } interface IFindPhotosOfMeState { navigate: "upload" | "loading" | "edit-photo" | "fail"; targetImage?: IUploadEvent; foundImages?: any[]; } const defaultFindPhotosOfMeState: IFindPhotosOfMeState = { navigate: "upload", targetImage: null, foundImages: null, }; export class FindPhotosOfMe extends React.PureComponent { public static getDerivedStateFromProps(props: IFindPhotosOfMeProps, state: IFindPhotosOfMeState) { const { image, ...targetImage } = state.targetImage || { image: {} }; return { targetImage: { ...targetImage, image: { src: image.src || props.referencePhotoSrc, }, }, }; } public state: IFindPhotosOfMeState = defaultFindPhotosOfMeState; public componentDidMount() { if (!_.isEmpty(this.props.referencePhotoSrc)) { this.setState({ navigate: "edit-photo", }); } } public componentDidUpdate(prevProps: IFindPhotosOfMeProps, prevState: IFindPhotosOfMeState) { if (_.isEmpty(prevState.foundImages) && !_.isEmpty(this.state.foundImages)) { this.props.onFoundImages(this.state.foundImages); } if (!_.isEqual(prevProps.referencePhotoSrc, this.props.referencePhotoSrc)) { if (_.isEmpty(this.props.referencePhotoSrc)) { this.setState({ navigate: "upload", }); } else { this.setState({ navigate: "edit-photo", }); } } } public render() { return ( (
{this.renderLoader(this.state.navigate === "loading")} {this.renderError(this.state.navigate === "fail")} {this.renderUploadPhoto(this.state.navigate === "upload")} {this.renderEditPhoto(this.state.navigate === "edit-photo")}
) ]}>
); } // TODO: This `immediate` flag doesn't look great when going from a populated // `referencePhotoSrc` to a null one. private renderUploadPhoto = (render: boolean) => { return (

Find photos of me

Take a selfie and we will look through all the photos to find the ones you are in.

{ this.setState({ targetImage: upload, }, () => this.findPhotos()); }} />
); } private renderEditPhoto = (render: boolean) => { return (
{ this.setState({ targetImage: upload, }, () => this.findPhotos()); }, }} />

{(_.isArray(this.state.foundImages) && _.isEmpty(this.state.foundImages)) ? "We couldn't find any photos" : "Edit your selfie."}

Make sure that your face is visible and centered on the picture.

); } private renderLoader = (render: boolean) => { return ( ); } private renderError = (active: boolean) => { return (
Something went wrong!
Please try again shortly.
); } private findPhotos = () => { this.setState({ navigate: "loading", foundImages: [], }, async () => { try { const images = await this.props.findPhotos(this.state.targetImage); this.setState({ navigate: "edit-photo", foundImages: images, }); } catch { this.setState({ navigate: "fail", }); } }); } }