Skip to content

Commit debce11

Browse files
committed
fix(syndicated article): upgrade to graph upsert
1 parent 844dc52 commit debce11

File tree

2 files changed

+21
-70
lines changed

2 files changed

+21
-70
lines changed

clients/web/src/containers/syndicated-article/syndicated-article.js

+17-8
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ import { Toasts } from 'connectors/toasts/toast-list'
3636
import { ListenLogin as Listen } from 'connectors/listen/listen-login'
3737
import { ShareToMastodon } from 'components/share-modal/share-mastodon'
3838

39+
import { mutationUpsertTransitionalItem } from 'connectors/items/mutation-upsert.state'
40+
import { mutationDeleteTransitionalItem } from 'connectors/items/mutation-delete.state'
41+
3942
// Possible query params passed via url
4043
const validParams = {
4144
mobile_web_view: false, // hide unneeded elements when rendered by native apps
@@ -52,7 +55,6 @@ export function SyndicatedArticle({ queryParams = validParams, locale }) {
5255

5356
const topics = useSelector((state) => state.topicList?.topicsByName)
5457
const articleData = useSelector((state) => state.syndicatedArticle.articleData)
55-
const saveStatus = useSelector((state) => state.syndicatedArticle.saveStatus)
5658

5759
const [isMastodonOpen, setIsMastodonOpen] = useState(false)
5860

@@ -73,6 +75,9 @@ export function SyndicatedArticle({ queryParams = validParams, locale }) {
7375
showAds
7476
} = articleData || {}
7577

78+
const saveItemId = useSelector((state) => state.itemsTransitions[slug])
79+
const saveStatus = saveItemId ? 'saved' : 'unsaved'
80+
7681
// modify rendered elements when query params are passed in
7782
const { mobile_web_view, premium_user } = queryParams
7883
const isMobileWebView = mobile_web_view === 'true'
@@ -112,15 +117,19 @@ export function SyndicatedArticle({ queryParams = validParams, locale }) {
112117
const syndicatedFrom = publisherUrl ? publisherUrl : false
113118
const ArticleLayout = isMobileWebView ? MobileLayout : Layout
114119

115-
const saveAction = (savedUrl, value) => {
116-
if (saveStatus === 'saved') dispatch(unSaveArticleItem(itemId))
117-
if (saveStatus !== 'saved') {
118-
const analyticsData = { url: savedUrl, value }
119-
dispatch(sendSnowplowEvent('syndicated.article.save', analyticsData))
120-
dispatch(saveArticleItem(savedUrl))
121-
}
120+
// Prep save action
121+
const onSave = (url, value) => {
122+
dispatch(sendSnowplowEvent('syndicated.article.save', { url, value }))
123+
dispatch(mutationUpsertTransitionalItem(url, slug))
122124
}
123125

126+
const onUnSave = (url, value) => {
127+
dispatch(sendSnowplowEvent('syndicated.article.unsave', { url, value }))
128+
dispatch(mutationDeleteTransitionalItem(saveItemId, slug))
129+
}
130+
131+
const saveAction = saveItemId ? onUnSave : onSave
132+
124133
const shareAction = (platform) => {
125134
const analyticsData = { url }
126135
dispatch(sendSnowplowEvent(`syndicated.share.${platform}`, analyticsData))

clients/web/src/containers/syndicated-article/syndicated-article.state.js

+4-62
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,12 @@
1-
import { put, takeEvery } from 'redux-saga/effects'
2-
import {
3-
ARTICLE_HYDRATE,
4-
ARTICLE_SAVE_REQUEST,
5-
ARTICLE_SAVE_SUCCESS,
6-
ARTICLE_SAVE_FAILURE,
7-
ARTICLE_UNSAVE_REQUEST,
8-
ARTICLE_UNSAVE_SUCCESS,
9-
ARTICLE_UNSAVE_FAILURE,
10-
ARTICLE_GET_PONG
11-
} from 'actions'
1+
import { takeEvery } from 'redux-saga/effects'
2+
import { ARTICLE_HYDRATE, ARTICLE_GET_PONG } from 'actions'
123
import { getSyndicatedArticle } from 'common/api/queries/get-syndicated-article'
13-
import { saveItem } from 'common/api/_legacy/saveItem'
14-
import { removeItem } from 'common/api/_legacy/removeItem'
4+
155
import { HYDRATE } from 'actions'
166

177
/** ACTIONS
188
--------------------------------------------------------------- */
199
export const hydrateArticle = (hydrated) => ({ type: ARTICLE_HYDRATE, hydrated }) //prettier-ignore
20-
export const saveArticleItem = (url) => ({ type: ARTICLE_SAVE_REQUEST, url })
21-
export const unSaveArticleItem = (id) => ({ type: ARTICLE_UNSAVE_REQUEST, id })
2210
export const callGetPong = () => ({ type: ARTICLE_GET_PONG })
2311

2412
/** REDUCERS
@@ -35,26 +23,6 @@ export const syndicatedArticleReducers = (state = initialState, action) => {
3523
return { ...state, ...hydrated }
3624
}
3725

38-
case ARTICLE_SAVE_REQUEST: {
39-
return { ...state, saveStatus: 'saving' }
40-
}
41-
42-
case ARTICLE_SAVE_SUCCESS: {
43-
return { ...state, saveStatus: 'saved' }
44-
}
45-
46-
case ARTICLE_SAVE_FAILURE: {
47-
return { ...state, saveStatus: 'unsaved' }
48-
}
49-
50-
case ARTICLE_UNSAVE_SUCCESS: {
51-
return { ...state, saveStatus: 'unsaved' }
52-
}
53-
54-
case ARTICLE_UNSAVE_FAILURE: {
55-
return { ...state, saveStatus: 'saved' }
56-
}
57-
5826
// SPECIAL HYDRATE: This is sent from the next-redux wrapper and
5927
// it represents the state used to build the page on the server.
6028
case HYDRATE: {
@@ -69,36 +37,10 @@ export const syndicatedArticleReducers = (state = initialState, action) => {
6937

7038
/** SAGAS :: WATCHERS
7139
--------------------------------------------------------------- */
72-
export const syndicatedArticleSagas = [
73-
takeEvery(ARTICLE_SAVE_REQUEST, articleSaveRequest),
74-
takeEvery(ARTICLE_UNSAVE_REQUEST, articleUnSaveRequest),
75-
takeEvery(ARTICLE_GET_PONG, articleGetPong)
76-
]
40+
export const syndicatedArticleSagas = [takeEvery(ARTICLE_GET_PONG, articleGetPong)]
7741

7842
/** SAGA :: RESPONDERS
7943
--------------------------------------------------------------- */
80-
function* articleSaveRequest({ url }) {
81-
try {
82-
const response = yield saveItem(url)
83-
if (response?.status !== 1) throw new Error('Unable to save')
84-
85-
yield put({ type: ARTICLE_SAVE_SUCCESS })
86-
} catch (error) {
87-
yield put({ type: ARTICLE_SAVE_FAILURE, error })
88-
}
89-
}
90-
91-
function* articleUnSaveRequest({ id }) {
92-
try {
93-
const response = yield removeItem(id)
94-
if (response?.status !== 1) throw new Error('Unable to remove item')
95-
96-
yield put({ type: ARTICLE_UNSAVE_SUCCESS })
97-
} catch (error) {
98-
yield put({ type: ARTICLE_UNSAVE_FAILURE, error })
99-
}
100-
}
101-
10244
function* articleGetPong() {
10345
yield fetch('/api/pong/get', {
10446
method: 'post',

0 commit comments

Comments
 (0)