-
Notifications
You must be signed in to change notification settings - Fork 0
/
AddTvSeriesPage.tsx
46 lines (41 loc) · 1.58 KB
/
AddTvSeriesPage.tsx
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
import React from 'react';
import { useMutation } from '@apollo/client';
import {
MUTATION_ADD_TV_SERIES,
QUERY_TV_SERIESES,
insertIntoSortedTvSerieses,
TvSeriesPropsInterface,
TvSeriesInterface
} from '../../Utils/Utils';
import TvSeriesForm from '../common/TvSeriesForm';
function AddTvSeriesPage() {
// Apollo insertion:
const [addTvSeries, { loading, error }] = useMutation(MUTATION_ADD_TV_SERIES, {
// Update can be used to update the client's cache:
update: (cache, { data: { addSeries } }) => {
const queryRes = cache.readQuery({ query: QUERY_TV_SERIESES }) as { serieses: TvSeriesInterface[] };
cache.writeQuery({
query: QUERY_TV_SERIESES, data: {
serieses: insertIntoSortedTvSerieses(queryRes?.serieses, addSeries) // keep sorted cache
}
});
}
});
if (loading) return <p>Loading...</p>;
if (error) return <p>Error. Try to refresh.</p>;
const addTvSeriesOnSubmit = async ({ title, yearBegin, yearEnd, popularity }: TvSeriesPropsInterface): Promise<void> => {
// Add tvSeries to the db:
await addTvSeries({
variables: { title, yearBegin, yearEnd, popularity }
});
}
return (
<TvSeriesForm
submitButtonString={"Add"}
shouldRedirectAfterSubmit={true}
tvSeriesProps={{ title: '', yearBegin: -1, yearEnd: -1, popularity: -1 }} // pass "false" values for the form
onSubmitDbAction={addTvSeriesOnSubmit}
/>
);
}
export default AddTvSeriesPage;