UserProfile.tsx 5.38 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import * as React from 'react';
import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
import { faImage, faUser } from '@fortawesome/free-solid-svg-icons';

import P from '../../components/typography/P/P';
import H6 from '../../components/typography/H6/H6';
import styled, { StyledThemeInterface } from '../../themes/styled';
import User from '../../types/User';

const FileInput = styled.input`
  cursor: pointer;
  height: calc(100% + 100px);
  width: 100%;
  position: absolute;
  top: -100px;
  left: 0;

  &:focus {
    outline: 0;
  }
`;

//TODO media queries/responsivity
export const UserProfile = styled.div`
  overflow: auto;
  width: 75%;
  height: 100%;
  margin-left: 1%;
  background-color: ${(props: StyledThemeInterface) =>
    props.theme.styles.colour.base5};
`;

//TODO don't use any props type
const UserProfileImage = styled.div<any>`
  position: relative;
  width: 100%;
  height: 400px;
  background-color: ${props => props.theme.styles.colour.base4};
  background-image: ${props =>
    props.backgroundImage
      ? `linear-gradient(transparent, rgba(0, 0, 0, .75)), url(${
          props.backgroundImage
        })`
      : ''};
  background-position: center;
  background-size: 100% auto;
  color: ${props => props.theme.styles.colour.base3};
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 6rem;
`;

const liftUserAvatar = -75;

const AvatarPlaceholder = styled.div`
  font-size: 12px;
  position: absolute;
  top: 150px;
  text-align: center;
  text-shadow: 1px 1px 0 #00000069;
  transition: all 0.2s linear;
`;

//TODO don't use the `any` props type
const UserAvatar = styled.div<any>`
  overflow: hidden;
  height: 150px;
  width: 150px;
  border-radius: 75px;
  background-color: ${props => props.theme.styles.colour.base3};
  background-image: ${props => `url(${props.backgroundImage})`};
  background-position: center;
  background-size: cover;
  background-repeat: no-repeat;
  color: ${props => props.theme.styles.colour.base5};
  position: relative;
  top: ${liftUserAvatar}px;
  margin: 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 4rem;

  // avatar placeholder
  > div {
    font-size: 6px;
    pointer-events: none;
  }

  &:hover > div {
    top: 50%;
    font-size: 12px;
  }
  // end avatar placeholder

  // avatar font awesome icon
  svg {
    top: 0;
    position: relative;
    transition: all 0.2s linear;
    pointer-events: none;
  }

  &:hover svg {
    font-size: 30px;
    top: -25px;
  }
  // end avatar font awesome icon
`;

const UserDetails = styled.div`
  position: relative;
  top: ${liftUserAvatar}px;
  text-align: center;
`;

const FadedFallbackText = ({ value, fallback }) =>
  value ? (
    <span>{value}</span>
  ) : (
    <span style={{ color: 'lightgrey' }}>{fallback}</span>
  );

// the body of UserProfile is customisable through a render prop
// the `body` render prop will be passed this style object which
// should be passed to the container returned from the render prop
const bodyProps = {
  style: {
    position: 'relative',
    top: `${liftUserAvatar}px`
  }
};

type UserProfileProps = {
  innerRef: (instance: any) => void;
  user: User;
  setUserImage: Function; // TODO make this optional
  body?: ({ containerProps: object }) => JSX.Element;
};

export default function(props: UserProfileProps) {
  return (
    <UserProfile innerRef={props.innerRef}>
      <UserProfileImage
        title="Click to select a profile background"
        backgroundImage={props.user.profileImage}
      >
        <FileInput onChange={props.setUserImage('profileImage')} type="file" />
        {!props.user.profileImage ? (
          <div
            style={{
              pointerEvents: 'none',
              position: 'relative',
              top: '-20px',
              textAlign: 'center'
            }}
          >
            <FontAwesomeIcon icon={faImage} />
            <P
              style={{
                pointerEvents: 'none',
                marginTop: '-12px',
                fontSize: '.8rem',
                textShadow: '1px 1px 0 lightgrey'
              }}
            >
              Click to select
              <br />a profile background
            </P>
          </div>
        ) : null}
      </UserProfileImage>
      <UserAvatar
        title="Click to select your profile image"
        backgroundImage={props.user.avatarImage}
      >
        <FileInput onChange={props.setUserImage('avatarImage')} type="file" />
        {!props.user.avatarImage ? (
          <>
            <FontAwesomeIcon icon={faUser} />
            <AvatarPlaceholder>
              Click to select
              <br />a profile picture
            </AvatarPlaceholder>
          </>
        ) : null}
      </UserAvatar>
      <UserDetails>
        <H6>{props.user.emojiId}</H6>
        <H6>
          <FadedFallbackText
            value={props.user.username}
            fallback="joebloggs84"
          />
        </H6>
        <H6>
          <FadedFallbackText value={props.user.role} fallback="Head Teacher" />
          <span style={{ color: 'lightgrey' }}>&nbsp;&nbsp;</span>
          <FadedFallbackText
            value={props.user.location}
            fallback="London, UK"
          />
        </H6>
      </UserDetails>
      {props.body ? props.body({ containerProps: bodyProps }) : null}
      {/*padding forces profile to be scrollable*/}
      <div style={{ height: '75%', width: '100%' }} />
    </UserProfile>
  );
}