MovieInfo.tsx 10.9 KB
Newer Older
1
import { useNavigation } from '@react-navigation/native';
2
3
import * as React from 'react';
import { useEffect, useState } from 'react';
4
import {
5
  ActivityIndicator,
6
7
8
9
  Image,
  ScrollView,
  StyleSheet,
  useWindowDimensions,
10
} from 'react-native';
11
import AwesomeAlert from 'react-native-awesome-alerts';
12
13
14
import { Button, Icon } from 'react-native-elements';
import { AirbnbRating } from 'react-native-ratings';
import { default as AwesomeIcon } from 'react-native-vector-icons/FontAwesome';
15
import { useDispatch, useSelector } from 'react-redux';
16
import Profile from '../screens/Profile';
17
import { rateMovie } from '../store/ducks/auth/actions';
18
19
import { clearMovie, fetchMovieById } from '../store/ducks/movies/actions';
import { ApplicationState } from '../store/interface';
20
import Comment from './Comment';
21
22
23
import Seperator from './common/Seperator';
import { Text, View } from './common/Themed';
import RatingModal from './RatingModal';
24
25
26
27
28

interface MovieInfoProps {
  movieID: string;
}

29
30
31
32
33
34
const noAlert = {
  title: '',
  message: '',
  error: false,
};

35
export default function MovieInfo({ movieID }: MovieInfoProps) {
36
  const { height, width } = useWindowDimensions();
37
38

  const styles = StyleSheet.create({
39
40
41
42
    scrollView: {
      flexGrow: 1,
      width: '90%',
    },
43
    root: {
44
      flex: 1,
45
      alignItems: 'center',
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
    infoView: {
      marginVertical: 10,
      flexDirection: 'row',
      alignItems: 'flex-start',
      alignContent: 'space-between',
    },
    infoRow: {
      flexGrow: 1,
      height: '100%',
      alignItems: 'center',
      textAlign: 'center',
    },
    poster: {
      borderRadius: 5,
    },
    titleText: {
      flexGrow: 1,
      fontSize: 18,
      fontWeight: 'bold',
    },
    infoText: {
      fontSize: 16,
      fontWeight: 'bold',
    },
    icon: { fontSize: 22, marginRight: 10 },
    maxRow: {
      flexGrow: 1,
      flexDirection: 'row',
      alignItems: 'center',
76
77
    },
    container: {
78
      width: '100%',
79
80
      padding: 15,
      marginVertical: 5,
81
      alignItems: 'center',
82
83
84
      borderRadius: 5,
    },
    subText: {
85
      color: '#2e78b7',
86
87
88
89
90
91
92
    },
    button: {
      marginTop: 10,
    },
    key_number_container: {
      marginTop: 20,
      flex: 1,
93
94
95
      flexDirection: 'row',
      flexWrap: 'wrap',
      alignItems: 'flex-start',
96
97
98
    },
    genres: {
      flex: 1,
99
100
      flexWrap: 'wrap',
      flexDirection: 'row',
101
      marginTop: 15,
102
      paddingBottom: '1rem',
103
    },
104
    comments: {
105
      width: '100%',
106
107
108
109
110
111
112
113
114
115
116
      alignItems: 'center',
      justifyContent: 'center',
      textAlign: 'center',
      marginBottom: 32,
    },
    alert: {
      width: 400,
    },
    alertError: {
      color: 'red',
    },
117
118
119
120
121
  });

  const dispatch = useDispatch();

  const data = useSelector(({ movies }: ApplicationState) => movies.byId);
122
123
  const { ratings, token, loading, error, loggedIn } = useSelector((state: ApplicationState) => state.auth);
  const navigation = useNavigation();
124
125
126
127

  const [rateModalVisible, setRateModalVisible] = useState(false);
  const [awaitingRating, setAwaitingRating] = useState(false);
  const [alert, setAlert] = useState({ ...noAlert });
128

129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
  /**
   * Callback for rating modal
   * @param rating number of stars
   * @param comment optional comment
   */
  const dispatchRating = (rating: number, comment: string) => {
    if (token) {
      // If user is logged in, dispatch rateMovie
      setAwaitingRating(true);
      dispatch(rateMovie({ token: token, movieId: movieID, rating, comment }));
    } else {
      // If user is not logged in, dispatch error alert
      setAwaitingRating(false);
      setAlert({
        title: 'Error',
        message: 'User not logged in',
        error: true,
      });
    }
  };

  // Fetch movies on load
151
152
153
154
155
156
157
  useEffect(() => {
    dispatch(fetchMovieById({ id: movieID }));
    return () => {
      dispatch(clearMovie());
    };
  }, []);

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
  useEffect(() => {
    // If we're not loading and awaitingRating is true, we sucessfully rated movie => Show sucess alert
    if (!loading && awaitingRating && ratings) {
      dispatch(fetchMovieById({ id: movieID }));
      setAwaitingRating(false);
      setAlert({
        title: 'Rating succesful',
        message: 'You have succesfully rated this movie',
        error: false,
      });
    }
  }, [ratings]);

  useEffect(() => {
    // If an error occurs, show an error alert with the error message returned from the server
    if (error) {
      setAwaitingRating(false);
      setAlert({
        title: 'Error',
        message: 'Server responded with: ' + error,
        error: true,
      });
    }
  }, [error]);

183
184
185
186
187
188
189
  // Function that formats minutes ot hours and minutes
  const formatTime = (time: number) => {
    const hours = Math.floor(time / 60);
    const minutes = time % 60;
    return hours + 'h ' + minutes + 'm';
  };

190
191
192
193
194
195
196
  if (loading || awaitingRating) {
    return (
      <View style={{ flex: 1, alignContent: 'center', alignItems: 'center', height: '100%' }}>
        <ActivityIndicator size='large' color='#00ff00' />
      </View>
    );
  } else if (data) {
197
    return (
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
      <View style={styles.root}>
        <ScrollView style={styles.scrollView} showsVerticalScrollIndicator={false}>
          <View style={{ flexGrow: 1, alignItems: 'center' }}>
            <View style={styles.infoView}>
              <View style={{ flexGrow: 1 }}>
                <Image
                  source={{
                    uri: data.movie.poster,
                  }}
                  style={[styles.poster, { width: width * 0.45, height: (width * 0.45 * 89) / 60 }]}
                  resizeMode='contain'
                />
              </View>

              <View style={styles.infoRow}>
                <Text
                  style={[styles.titleText, { textAlign: 'center', maxWidth: 150, fontSize: 16 }]}
                >
                  {data.movie.title}
                </Text>
                <View style={{ flexGrow: 1 }}>
                  <AirbnbRating
                    count={5}
                    defaultRating={data.movie.rating}
                    showRating={false}
                    isDisabled={true}
                    size={25}
                  />
                  <Text style={[styles.infoText, { textAlign: 'center' }]}>
                    {data.movie.rating.toFixed(2)} / 5
                  </Text>
                </View>
230

231
232
233
234
235
236
237
238
239
240
                <View style={styles.maxRow}>
                  <Icon
                    name='clock'
                    type='fontisto'
                    color='#517fa4'
                    style={styles.icon}
                    tvParallaxProperties={undefined}
                  />
                  <Text style={styles.infoText}>{formatTime(data.movie.runtime)}</Text>
                </View>
241

242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
                <View style={styles.maxRow}>
                  <Icon
                    name='calendar'
                    type='antdesign'
                    color='#517fa4'
                    style={styles.icon}
                    tvParallaxProperties={undefined}
                  />
                  <Text style={styles.infoText}>{new Date(data.movie.released).getFullYear()}</Text>
                </View>

                <View style={styles.maxRow}>
                  <Text style={{ fontSize: 16 }}>PG-RATED: </Text>
                  <Text style={styles.infoText}>{data.movie.rated}</Text>
                </View>

                <View style={styles.maxRow}>
                  <Text style={{ fontSize: 16 }}>IMDB RATING: </Text>
                  <Text style={styles.infoText}>{data.movie.imdbRating}</Text>
                </View>
              </View>
            </View>

265
266
            <View style={{ maxWidth: 250, marginBottom: 10 }}>
              {loggedIn ?  <Button
267
268
269
270
271
272
                onPress={() => setRateModalVisible(true)}
                title='  Give Rating'
                disabled={(() => {
                  return ratings.find((rating) => rating.movie.id === movieID) ? true : false;
                })()}
                icon={<AwesomeIcon name='star-o' size={24} color='white' />}
273
274
              /> : <Button title="Log in to rate this movie" onPress={() => navigation.navigate("Profile")}/> }
             
275
276
277
            </View>

            <Text style={[styles.titleText, { flexGrow: 0 }]}>Plot</Text>
278
            <Seperator marginVertical={10} width='75%' curved /> 
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323

            <Text style={{ fontSize: 16, fontStyle: 'italic', textAlign: 'center' }}>
              {data.movie.plot}
            </Text>
            <Seperator marginVertical={10} width={'15%'} curved />

            <Text style={[styles.titleText, { flexGrow: 0 }]}>Cast</Text>
            <Seperator marginVertical={10} curved />

            <View style={{ width: '100%' }}>
              {LabelAndText('DIRECTORS', 'Joshua King')}
              <Seperator marginVertical={10} curved />
              {LabelAndText('PRODUCTION', 'Paramount Pictures, W365')}
              <Seperator marginVertical={10} curved />
              {LabelAndText('WRITERS', 'Phil Hay, Matt Manfredi, Peter Chung')}
              <Seperator marginVertical={10} curved />
              {LabelAndText(
                'STARRING ACTORS',
                'Charlize Theron, Frances McDormand, Sophie Okonedo',
              )}
              <Seperator marginVertical={10} curved />
            </View>

            <Text style={[styles.titleText, { flexGrow: 0 }]}>Reviews</Text>
            <Seperator marginVertical={10} width='75%' curved />

            <RatingModal
              modalVisible={rateModalVisible}
              onClose={() => setRateModalVisible(false)}
              onSubmit={dispatchRating}
            ></RatingModal>

            <View style={styles.comments}>
              {data.ratings.map((rating) => {
                return (
                  <Comment
                    date={rating.date}
                    comment={rating.comment}
                    username={rating.user.username}
                    rating={rating.rating}
                    key={rating.user.id}
                  ></Comment>
                );
              })}
            </View>
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
          </View>
        </ScrollView>

        <AwesomeAlert
          contentContainerStyle={styles.alert}
          titleStyle={alert.error ? styles.alertError : {}}
          show={alert.title.length > 0}
          title={alert.title}
          message={alert.message}
          closeOnTouchOutside={true}
          showConfirmButton={true}
          confirmText='Ok'
          onConfirmPressed={() => {
            setAlert({ ...noAlert });
          }}
        />
      </View>
341
    );
342
  } else return <ActivityIndicator size='large' color='rgba(255,100,255,0.5)' />;
343
344
345
346
347
}

function LabelAndText(label: string, text: string) {
  const styles = StyleSheet.create({
    label: {
348
349
      textTransform: 'capitalize',
      fontWeight: 'bold',
350
      marginRight: 5,
351
      textAlign: 'left',
352
353
    },
    container: {
354
355
      flexWrap: 'wrap',
      flexDirection: 'row',
356
357
358
359
360
361
362
363
364
365
    },
  });

  return (
    <View style={styles.container}>
      <Text style={styles.label}>{label}:</Text>
      <Text>{text}</Text>
    </View>
  );
}