Skip to content
This repository was archived by the owner on Oct 23, 2018. It is now read-only.

get auth for profile #56

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 69 additions & 10 deletions web/src/views/Profile/Component.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,72 @@ import { Props } from './types';

class RawProfile extends React.Component<Props> {
public componentDidMount() {
this.props.loadProfile();
this.loadProfile();
}

public loadProfile = async () => {
const res = await fetch(`${process.env.REACT_APP_API_BASE}/profile`, {
credentials: 'include',
headers: {
Authorization: `Bearer ${this.props.authToken}`,
},
mode: 'cors',
});
if (res.ok) {
const imageBlob = await res.blob();
const displayImage = URL.createObjectURL(imageBlob);
this.props.dispatchLoadProfile(displayImage);
}
};

public handleUpdateImage: React.FormEventHandler = async event => {
event.preventDefault();
const data = new FormData(event.target as HTMLFormElement);

const res = await fetch(
`${process.env.REACT_APP_API_BASE}/update-profile-image`,
{
body: data,
credentials: 'include',
headers: {
Authorization: `Bearer ${this.props.authToken}`,
},
method: 'post',
mode: 'cors',
},
);

if (res.ok) {
const imageBlob = await res.blob();
const displayImage = URL.createObjectURL(imageBlob);
this.props.handleDispatchUpdateImage(displayImage);
}
};

public handleUpdatePassword: React.FormEventHandler = async event => {
event.preventDefault();
const data = new FormData(event.target as HTMLFormElement);

const res = await fetch(
`${process.env.REACT_APP_API_BASE}/update-password`,
{
body: data,
credentials: 'include',
headers: {
Authorization: `Bearer ${this.props.authToken}`,
},
method: 'post',
mode: 'cors',
},
);

if (res.ok) {
this.props.handleDispatchUpdatePassword();
}
};

public render() {
const {
classes,
profileImage,
handleUpdateImage,
handleUpdatePassword,
username,
} = this.props;
const { classes, profileImage, username } = this.props;

return (
<>
Expand All @@ -37,7 +93,10 @@ class RawProfile extends React.Component<Props> {
src={profileImage || defaultProfileImage}
className={classes.avatar}
/>
<form className={classes.form} onSubmit={handleUpdateImage}>
<form
className={classes.form}
onSubmit={this.handleUpdateImage}
>
<FormControl
margin="normal"
required={true}
Expand Down Expand Up @@ -69,7 +128,7 @@ class RawProfile extends React.Component<Props> {
</form>
<form
className={classes.form}
onSubmit={handleUpdatePassword}
onSubmit={this.handleUpdatePassword}
>
<FormControl
margin="normal"
Expand Down
61 changes: 11 additions & 50 deletions web/src/views/Profile/Container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const mapStateToProps: MapStateToProps<
OwnProps,
State
> = state => ({
authToken: state.user.authToken,
profileImage: state.user.displayImage,
username: state.user.displayName,
});
Expand All @@ -19,64 +20,24 @@ const mapDispatchToProps: MapDispatchToProps<
DispatchProps,
OwnProps
> = dispatch => {
const loadProfile = async () => {
const res = await fetch(`${process.env.REACT_APP_API_BASE}/profile`, {
credentials: 'include',
mode: 'cors',
});
if (res.ok) {
const imageBlob = await res.blob();
const displayImage = URL.createObjectURL(imageBlob);
dispatch(setImage({ displayImage }));
}
const dispatchLoadProfile = (displayImage: string) => {
dispatch(setImage({ displayImage }));
};

// TODO: clear flash in react router if user goes to new page
const handleUpdateImage: React.FormEventHandler = async event => {
event.preventDefault();
const data = new FormData(event.target as HTMLFormElement);

const res = await fetch(
`${process.env.REACT_APP_API_BASE}/update-profile-image`,
{
body: data,
credentials: 'include',
method: 'post',
mode: 'cors',
},
);

if (res.ok) {
const imageBlob = await res.blob();
const displayImage = URL.createObjectURL(imageBlob);
dispatch(setImage({ displayImage }));
dispatch(setFlashMessage());
}
const handleDispatchUpdateImage = (displayImage: string) => {
dispatch(setImage({ displayImage }));
dispatch(setFlashMessage());
};

const handleUpdatePassword: React.FormEventHandler = async event => {
event.preventDefault();
const data = new FormData(event.target as HTMLFormElement);

const res = await fetch(
`${process.env.REACT_APP_API_BASE}/update-password`,
{
body: data,
credentials: 'include',
method: 'post',
mode: 'cors',
},
);

if (res.ok) {
dispatch(setFlashMessage());
}
const handleDispatchUpdatePassword = () => {
dispatch(setFlashMessage());
};

return {
handleUpdateImage,
handleUpdatePassword,
loadProfile,
dispatchLoadProfile,
handleDispatchUpdateImage,
handleDispatchUpdatePassword,
};
};

Expand Down
7 changes: 4 additions & 3 deletions web/src/views/Profile/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,13 @@ export interface Props
StateProps {}

export interface DispatchProps {
handleUpdateImage: React.FormEventHandler;
handleUpdatePassword: React.FormEventHandler;
loadProfile: () => void;
handleDispatchUpdateImage: (displayImage: string) => void;
handleDispatchUpdatePassword: () => void;
dispatchLoadProfile: (displayImage: string) => void;
}
export interface OwnProps {}
export interface StateProps {
authToken: string | null;
profileImage: string | null;
username: string | null;
}