import React, { Component } from 'react'; import Button from '../components/Button'; import UploadButton from '../components/UploadButton'; import { compose } from '../../bridge/lib/lib'; import { PROFILE_STATES } from '../lib/constants'; const NEXT_STEP_NUM = 3; const validateBase64 = ({ data, pass, error }) => { if (pass !== true) return { data, pass, error }; try { atob(data); return { data: atob(data), pass: true, error: '' }; } catch (e) { console.error(e); return { data: data, pass: false, error: 'File is not valid base64.' }; } }; const validateJSON = ({ data, pass, error }) => { if (pass !== true) return { data, pass, error }; try { JSON.parse(data); return { data: JSON.parse(data), pass: true, error: '' }; } catch (e) { console.error(e); return { data: data, pass: false, error: 'File does not decode into valid JSON.', }; } }; const validateFile = ({ data, pass, error }) => { if (pass !== true) return { data, pass, error }; if (data.file.type !== 'text/plain') { return { data: data, pass: false, error: 'File must be a .txt file' }; } if (data.file.name.includes('urbit-ships') === false) { return { data: data, pass: false, error: 'This does not appear to be the correct file.', }; } return { data: data.event.target.result, pass, error }; }; const validateObject = ({ data, pass, error }) => { if (pass !== true) return { data, pass, error }; const checkStringField = (obj, field) => field in obj && typeof obj[field] === 'string'; const checkShipField = (obj, field) => field in obj || Array.isArray(obj[field]); const meetsRequirements = obj => checkStringField(obj, 'idCode') && checkShipField(obj, 'planets') && checkShipField(obj, 'stars') && checkShipField(obj, 'galaxies'); const didPass = meetsRequirements(data) ? true : false; const err = didPass === false ? 'File is missing data, or the data is of incorrect data type.' : ''; return { data: data, pass: didPass, error: err }; }; // const preview = ship => { // const name = ob.patp(ship) // const sig = sigil.pour({ // patp: name, // colorway: ['black', 'white'], // renderer: ReactSVGComponents, // size: 64 // }) // // return ( //
//
//
// { sig } //
//
//

// { name } //

//
//
//
//
// { sig } //
// { name } //
//
// ) // } // const shipList = profile => { // const ships = Object.keys(profile) // return( //
//

Total Ships: { ships.length }

// //
// { ships.map(preview) } //
//
// ) // } class Upload extends Component { constructor(props) { super(props); this.handleTXTUpload = this.handleTXTUpload.bind(this); } handleTXTUpload = event => { const { setGlobalState } = this.props; const file = event.files.item(0); const reader = new FileReader(); reader.onload = event => { const validated = compose( validateObject, validateJSON, validateBase64, validateFile )({ data: { event: event, file: file }, pass: true, error: '' }); if (validated.pass === true) { const { galaxies, stars, planets } = validated.data; setGlobalState({ 'profile.error': validated.error, 'profile.state': PROFILE_STATES.UPLOAD_SUCCESS, 'profile.value': validated.data, 'profile.shipCount': galaxies.length + stars.length + planets.length, }); } else { setGlobalState({ 'profile.error': validated.error, 'profile.state': PROFILE_STATES.UPLOAD_ERROR, 'profile.value': null, }); } }; reader.readAsText(file); }; render() { const { setGlobalState } = this.props; const { props } = this; const success = props['profile.state'] === PROFILE_STATES.UPLOAD_SUCCESS || props['profile.state'] === PROFILE_STATES.INPUT_SUCCESS; let uploadButtonClass = props['profile.state'] === PROFILE_STATES.UPLOAD_ERROR ? 'btn shape-orange' : props['profile.state'] === PROFILE_STATES.UPLOAD_SUCCESS ? 'btn btn-success' : 'btn btn-primary'; let uploadButtonText = props['profile.state'] === PROFILE_STATES.UPLOAD_ERROR ? 'Choose file again' : props['profile.state'] === PROFILE_STATES.UPLOAD_SUCCESS ? 'urbit-ships.txt' : 'Choose file'; return (

{'Upload your '} {'urbit-ships.txt'}

{ 'This file contains your email and tells us what Urbit address space you own. Don’t have it? Return to Registration to download it.' }

{uploadButtonText} {props['profil.error'] !== '' ? (

{props['profile.error']}

) : (
)} { // { success // ? // :
// } // // { success // ? // :
// } // // { success // ? // :
// } }
{success ? (
{ //

Missing ships? Email {'registration@urbit.org'}

}
) : (
)}
); } } // const ShipList = (ships, title) => { // if (ships.length > 0) { // return ( //
//

{ title }

// { ships.map(ship => preview(ship)) } //
// ) // } else { // return
// } // } export default Upload;