Ott 424 subscription (#186)
* fix(ott-424): not finished yet * feat(ott-424): rendered user subscriptions list * fix(ott-424): deleted comments * fix(ott-424): deleted background * fix(ott-424): fixed imports * fix(ott-424): fixed comments * fix(ott-424): поправил комментарий * fix(ott-424): edited according to Mirlan's comments * fix(ott-424): deleted suffix from get subscriptions * fix(ott-424): donekeep-around/af30b88d367751c9e05a735e4a0467a96238ef47
parent
d8941fc016
commit
563219b741
|
Before Width: | Height: | Size: 353 B |
|
After Width: | Height: | Size: 878 B |
@ -0,0 +1,41 @@ |
||||
import { |
||||
useEffect, |
||||
useState, |
||||
useCallback, |
||||
} from 'react' |
||||
|
||||
import map from 'lodash/map' |
||||
|
||||
import { getUserSubscriptions } from 'requests/getUserSubscriptions' |
||||
|
||||
import { useLexicsStore } from 'features/LexicsStore' |
||||
|
||||
export type Team = { |
||||
id: number, |
||||
name_eng: string, |
||||
name_rus: string, |
||||
} |
||||
|
||||
export const useUserSubscriptions = () => { |
||||
const [subscriptions, setSubscriptions] = useState<Array<Team>>([]) |
||||
const { suffix } = useLexicsStore() |
||||
|
||||
type Names = 'name_eng' | 'name_rus' |
||||
|
||||
useEffect(() => { |
||||
getUserSubscriptions().then((res) => { |
||||
setSubscriptions(res.custom[0].teams) |
||||
}) |
||||
}, []) |
||||
|
||||
const normalizeSubscriptions = useCallback(() => { |
||||
const nameKey = `name_${suffix}` as Names |
||||
|
||||
return map(subscriptions, (team) => ({ |
||||
...team, |
||||
name: team[nameKey], |
||||
})) |
||||
}, [subscriptions, suffix]) |
||||
|
||||
return normalizeSubscriptions() |
||||
} |
||||
@ -0,0 +1,38 @@ |
||||
import { |
||||
DATA_URL, |
||||
PROCEDURES, |
||||
} from 'config' |
||||
import { callApi, getResponseData } from 'helpers' |
||||
|
||||
const proc = PROCEDURES.get_user_subscriptions |
||||
|
||||
type Team = { |
||||
id: number, |
||||
name_eng: string, |
||||
name_rus: string, |
||||
} |
||||
|
||||
type CustomSubscription = { |
||||
sport: number, |
||||
teams: Array<Team>, |
||||
} |
||||
|
||||
type UserSubscription = { |
||||
custom: Array<CustomSubscription>, |
||||
id: number, |
||||
lexic: number, |
||||
} |
||||
|
||||
export const getUserSubscriptions = (): Promise<UserSubscription> => { |
||||
const config = { |
||||
body: { |
||||
params: {}, |
||||
proc, |
||||
}, |
||||
} |
||||
|
||||
return callApi({ |
||||
config, |
||||
url: DATA_URL, |
||||
}).then(getResponseData(proc)) |
||||
} |
||||
@ -0,0 +1,43 @@ |
||||
import { |
||||
DATA_URL, |
||||
PROCEDURES, |
||||
} from 'config' |
||||
import { callApi } from 'helpers' |
||||
|
||||
const proc = PROCEDURES.save_user_custom_subscription |
||||
|
||||
type Response = { |
||||
_p_error: string | null, |
||||
_p_status: 1 | 2, |
||||
} |
||||
|
||||
const responseStatus = { |
||||
FAILURE: 2, |
||||
SUCCESS: 1, |
||||
} |
||||
|
||||
export const saveUserCustomSubscription = async () => { |
||||
const config = { |
||||
body: { |
||||
params: { |
||||
_p_action: 1, |
||||
_p_id: 1, |
||||
_p_sport: 1, |
||||
_p_status: 1, |
||||
_p_type: 1, |
||||
_p_user_id: 1, |
||||
}, |
||||
proc, |
||||
}, |
||||
} |
||||
|
||||
const response: Response = await callApi({ |
||||
config, |
||||
url: DATA_URL, |
||||
}) |
||||
|
||||
if (response._p_status === responseStatus.SUCCESS) { |
||||
return Promise.resolve(response) |
||||
} |
||||
return Promise.reject(response._p_error) |
||||
} |
||||
@ -0,0 +1,42 @@ |
||||
import { |
||||
DATA_URL, |
||||
PROCEDURES, |
||||
} from 'config' |
||||
import { callApi } from 'helpers' |
||||
|
||||
const proc = PROCEDURES.save_user_subscription |
||||
|
||||
type Type = { |
||||
subscriptionId: number, |
||||
} |
||||
|
||||
type Response = { |
||||
_p_error: string | null, |
||||
_p_status: 1 | 2, |
||||
} |
||||
|
||||
const responseStatus = { |
||||
FAILURE: 2, |
||||
SUCCESS: 1, |
||||
} |
||||
|
||||
export const saveUserSubscription = async ({ subscriptionId }: Type) => { |
||||
const config = { |
||||
body: { |
||||
params: { |
||||
p_subscription_id: subscriptionId, |
||||
}, |
||||
proc, |
||||
}, |
||||
} |
||||
|
||||
const response: Response = await callApi({ |
||||
config, |
||||
url: DATA_URL, |
||||
}) |
||||
|
||||
if (response._p_status === responseStatus.SUCCESS) { |
||||
return Promise.resolve(response) |
||||
} |
||||
return Promise.reject(response._p_error) |
||||
} |
||||
Loading…
Reference in new issue