Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 6 require an explicit save before attaching file; which causes my page to reload endlessly with this package. #41

Open
andrew-oko-odion opened this issue Oct 10, 2019 · 1 comment

Comments

@andrew-oko-odion
Copy link

The My File upload handler

  async uploadAvatar(upload) {
    const URI = `${process.env.API}/${process.env.ACCOUNT_UPDATE_URI}`;

    const res = await fetch(URI, {
      method: "PUT",
      headers: {
        "Content-Type": "application/json",
        Accept: "application/json",
        Authorization: this.props.token
      },
      body: JSON.stringify({ avatar: upload.file.name })
    });

      try {
	  if (res.ok) {
	      const data = await res.json();
	      console.log(data);
	      console.log("Avatar Uploaded, try to render it");
	      this.props.dispatch(userSuccess({ ...data }));
	      // message.success("update successfully");
	      this.setState({avatarStatus: "upload successfull"})
	  } else {
	      const data = res.statusText();
	      console.log(data);
	  }
      } catch {
	  console.log("Some network error occored");
      }
  }

The File Upload Componenet

			<Col sm={2} xs={2}> 
			    <ActiveStorage
				endpoint={{
				    path: "users",
				    host: process.env.UPLOAD_URI,
				    model: "User",
				    attribute: "avatar",
				    method: "PUT",
				    protocol: "https"
				}}
				headers={{ Authorization: this.props.token }}
				directUploadsPath="/rails/active_storage/direct_uploads"
				onSubmit={user => this.setState({ avatar: user.avatar })}
				render={({ handleUpload, uploads, ready }) => (
				    <div>
					<label>
					    <div className="avatar-item">
						{!isEmpty(this.props.userData.avatar_url) ? (
						    <img
						    className="thumbnail-image"
							       src={
							       process.env.API + this.props.userData.avatar_url
							       }
						    />
						) : (
						    <span>
							<Icon type="plus" style={{ fontSize: "20px" }} />
							<div style={{ fontSize: "13px" }}>
							    Upload Avatar
							</div>
						    </span>
						)}
						
						<input
						type="file"
						      disabled={!ready}
						      onChange={e => handleUpload(e.currentTarget.files)}
						/>
						{uploads.map(upload => {
						    switch (upload.state) {
							case "waiting":
							    return (
								<span key={upload.id}>
								    <Spin indicator={loadingIcon} />
								</span>
							    );
							case "uploading":
							    return (
								<span key={upload.id}>
								    <Spin indicator={loadingIcon} />
								</span>
							    );
							case "error":
							    return (
								<span key={upload.id}>
								    Error uploading {upload.file.name}:{" "}
								    {upload.error}
								</span>
							    );
							case "finished":
							    return (
								<React.Fragment>
								    <span key={upload.id}>
									<Icon type="check" />
									{ this.uploadAvatar(upload) }
									{this.state.avatarStatus && <p> done </p>}
								    </span>
								</React.Fragment>
							    );
						    }
						})}
					    </div>
					</label>
				    </div>
				)}
			    />
			</Col>

I am using nextjs ^9.0.5-canary.1
yarn 1.152
and node 12.2.0

@andrew-oko-odion andrew-oko-odion changed the title Rails 6 require file save case an upload error Rails 6 require an explicit save before attaching file; which causes my page to reload endlessly with this package. Oct 10, 2019
@andrew-oko-odion
Copy link
Author

@cbothner how can I fix this problem

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant