| 15 | import { PageTitle, Box, Grid } from "styles/common"; |
| 16 | |
| 17 | class Profile extends React.Component<ProfileSpecs.Props, ProfileSpecs.State> { |
| 18 | constructor(props: ProfileSpecs.Props) { |
| 19 | super(props); |
| 20 | this.state = { |
| 21 | loading: true, |
| 22 | packs: [] |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | componentDidMount() { |
| 27 | this.fetchPacks(); |
| 28 | } |
| 29 | |
| 30 | fetchPacks = () => { |
| 31 | const { getPacks, user: { id } } = this.props; |
| 32 | this.setState({ loading: true }); |
| 33 | getPacks(id) |
| 34 | .then(packs => { |
| 35 | this.setState({ packs, loading: false }) |
| 36 | }) |
| 37 | .catch(() => { |
| 38 | alertError({ message: 'Unable to retrieve profile.' }); |
| 39 | this.setState({ loading: false }); |
| 40 | }); |
| 41 | }; |
| 42 | |
| 43 | deletePack = (id: number) => { |
| 44 | this.props.deletePack(id) |
| 45 | .then(() => this.fetchPacks()) |
| 46 | .catch(() => alertError({ message: 'Error while deleting pack.' })) |
| 47 | }; |
| 48 | |
| 49 | render() { |
| 50 | const { user: { username, id, default_weight_unit }, updateUser, fetchUser } = this.props; |
| 51 | const { loading, packs } = this.state; |
| 52 | return ( |
| 53 | <DocumentTitle title={`Packstack | My Packs`}> |
| 54 | <> |
| 55 | <PageTitle> |
| 56 | <h1>My Packs</h1> |
| 57 | </PageTitle> |
| 58 | <Grid> |
| 59 | <div className="two-thirds"> |
| 60 | <h3>Packs</h3> |
| 61 | <PackList loading={loading} packs={packs} currentUserId={id} deletePack={this.deletePack}/> |
| 62 | </div> |
| 63 | <div className="third"> |
| 64 | <h3>Settings</h3> |
| 65 | <Box> |
| 66 | <Formik |
| 67 | initialValues={{ username, default_weight_unit }} |
| 68 | validationSchema={Yup.object().shape({ |
| 69 | username: Yup.string().required("Username cannot be empty.") |
| 70 | })} |
| 71 | onSubmit={(values) => { |
| 72 | updateUser(values.username, values.default_weight_unit) |
| 73 | .then(() => { |
| 74 | fetchUser(); |
nothing calls this directly
no test coverage detected