Skip to content

Commit

Permalink
SOROKA-124: Подключение api интервалов и строк к ui (#36)
Browse files Browse the repository at this point in the history
SOROKA-124: connection api for dates

* SOROKA-124: fix bug this organization

* SOROKA-124: remove unused imports

* SOROKA-124: initial loading fix

* SOROKA-124: fix after review (useEffect)

Co-authored-by: Alexander Botenkov <[email protected]>
  • Loading branch information
AlexRbkv and boatincow authored Sep 30, 2022
1 parent eb1a2db commit 08b460d
Show file tree
Hide file tree
Showing 8 changed files with 61 additions and 36 deletions.
2 changes: 0 additions & 2 deletions src/components/common/Header.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ import { WUNDERKAMMER } from '../../utils/urls'

import { useStore } from '../../stores/rootStore'

import API from '../../api/config'

const Header = observer(() => {
const navigate = useNavigate()
const { baseStore, authStore } = useStore()
Expand Down
2 changes: 1 addition & 1 deletion src/components/dashboard/ListOfCards.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ const ListOfCards = () => {
// todo: add page query
const [cards, setCards] = useState({ results: [], total: 0 })
const [page, setPage] = useState(0)
const { authStore, baseStore, cardStore } = useStore()
const { authStore, baseStore } = useStore()
const [currentOrganization, setCurrentOrganization] = useState<string | number>(DEFAULT_ORGANIZATION_FILTER_VALUE)

useEffect(() => {
Expand Down
54 changes: 32 additions & 22 deletions src/components/properties/DateProperty/DateItem.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -47,24 +47,24 @@ const DateItem = ({
}) => {
const intl = useIntl()

const { calendar, jd: startDateJd, startContext, endJD, endContext = null } = value
const { calendar, startJD: startDateJd, startContext, endJD, endContext } = value

const parser = CALENDARS[calendar]
const placeholder = parser.getDateFormatPlaceholder()

const [startError, setStartError] = useState('')
const [endError, setEndError] = useState('')
const [dirty, setDirty] = useState({
start: parser.fromJD(startDateJd),
startContext,

end: parser.fromJD(endJD),
endContext,

calendar
})

const [isRange, setIsRange] = useState(false)
const placeholder = parser.getDateFormatPlaceholder()

const [startError, setStartError] = useState('')
const [endError, setEndError] = useState('')

const [isRange, setIsRange] = useState(!!dirty.end)

// using state to skip initial mount useEffect calls
const [isMounted, setIsMounted] = useState(false)
Expand All @@ -76,7 +76,8 @@ const DateItem = ({
return
}

const { start, end, calendar } = dirty
const { start, end, calendar, startContext: startDateString, endContext: endDateString } = dirty

const currentParser = CALENDARS[calendar]

// validation
Expand All @@ -99,29 +100,38 @@ const DateItem = ({

onChange(
{
jd: newStartDate,
endJD: newEndDate,
startJD: newStartDate,
startContext: calendar === CALENDAR_STRING_ID ? startDateString : '',
endJD: isRange ? newEndDate : '',
endContext: calendar === CALENDAR_STRING_ID && isRange ? endDateString : '',
calendar
},
isValid,
''
)
}, [dirty])
}, [dirty, isRange])

const onCalendarChange = (newCalendar: number) => {
setDirty((prev) => ({ ...prev, calendar: newCalendar }))
setDirty((prev) => {
if (newCalendar !== CALENDAR_STRING_ID) {
return { ...prev, startContext: '', endContext: '', calendar: newCalendar }
}
return { ...prev, calendar: newCalendar }
})
}

const fromInput = useMemo(
() => (
<DateInputUpd
defaultValue={parser.fromJD(startDateJd)}
onChange={(newValue, newStringValue) => {
setDirty((prev) => ({
...prev,
start: newValue || '',
startContext: newStringValue || ''
}))
setDirty((prev) => {
return {
...prev,
start: newValue || prev.start,
startContext: newStringValue || prev.startContext
}
})
}}
errorMessage={startError}
placeholder={placeholder}
Expand All @@ -131,7 +141,7 @@ const DateItem = ({
: undefined
}
stringForm={calendar === CALENDAR_STRING_ID}
stringFormDefaultValue=""
stringFormDefaultValue={startContext}
/>
),
[calendar, placeholder, parser, startDateJd, startError, setDirty]
Expand All @@ -140,18 +150,18 @@ const DateItem = ({
const toInput = useMemo(
() => (
<DateInputUpd
defaultValue={parser.fromJD(startDateJd)}
defaultValue={parser.fromJD(endJD)}
onChange={(newValue, newStringValue) => {
setDirty((prev) => ({
...prev,
end: newValue || '',
endContext: newStringValue || ''
end: newValue || prev.end,
endContext: newStringValue || prev.endContext
}))
}}
errorMessage={endError}
placeholder={placeholder}
stringForm={calendar === CALENDAR_STRING_ID}
stringFormDefaultValue=""
stringFormDefaultValue={endContext}
/>
),
[calendar, placeholder, parser, startDateJd, endError, setDirty]
Expand Down
1 change: 0 additions & 1 deletion src/stores/authStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ export default class AuthStore {
}

setInivitationData(payload: User) {
console.log(payload, 'invalidation')
this.invitationData = payload
}

Expand Down
1 change: 0 additions & 1 deletion src/stores/cardStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ export default class CardStore {

runInAction(() => {
this.userRole = userRole

if (this.userRole === USER_ROLES.admin && !this.cardInfo.id) {
this.organizationOption = organization
this.ownerOption = id
Expand Down
32 changes: 25 additions & 7 deletions src/stores/propertiesStore.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -41,11 +41,11 @@ type PropertyTemplate = {

// todo: distribute into components and make necessary abstractions (all have showHelp/onChange/value)
export type DateItemData = {
jd: number | null // startJD:
startContext?: string
startJD: number | null
startContext: string
calendar: number
endJD: number | null
endContext?: string
endContext: string
}

export type GeoPropertyProps = {
Expand Down Expand Up @@ -89,8 +89,26 @@ const TYPES: { [key: string]: any } = {
},
JULIAN_DATE: {
renderForm: (props: DatePropertyProps) => <DateProperty {...props} />,
formatToApi: ({ value, calendar }: { value: number; calendar: number }) => [{ jd: value, calendar }],
defaultData: [{ jd: null, calendar: CALENDAR_GREGORIAN_ID }],
formatToApi: (value: DateItemData) => {
return [
{
startJD: value.startJD,
endJD: value.endJD,
startContext: value.startContext,
endContext: value.endContext,
calendar: value.calendar
}
]
},
defaultData: [
{
startJD: null,
endJD: null,
startContext: undefined,
endContext: undefined,
calendar: CALENDAR_GREGORIAN_ID
}
],
hasHelp: true
},
GEO_POINT: {
Expand Down Expand Up @@ -311,11 +329,11 @@ export default class PropertiesStore {
}

async getProperties() {
CardsAPI.getCardsProperties().then((backendData) => this.setPropertiesFromBackend(backendData.data))
await CardsAPI.getCardsProperties().then((backendData) => this.setPropertiesFromBackend(backendData.data))
}

async fetchTemplates() {
TemplatesAPI.getTemplates().then(({ data: backendData }) => this.setTemplatesFromBacked(backendData))
await TemplatesAPI.getTemplates().then(({ data: backendData }) => this.setTemplatesFromBacked(backendData))
}

setTemplatesFromBacked(backendData: PropertyTemplate[]) {
Expand Down
4 changes: 2 additions & 2 deletions src/utils/dates/gregorian.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import GregorianDate from 'ilib/lib/GregorianDate'
// @ts-ignore
import DateFmt from 'ilib/lib/DateFmt'
import { CalendarGeneral } from './types'
class Calendar implements CalendarGeneral {
class Calendar implements CalendarGeneral {
dateFormat = new DateFmt({ template: 'dd.MM.yyyy', timezone: 'GMT' })

validate(dateString: string) {
Expand Down Expand Up @@ -49,5 +49,5 @@ import { CalendarGeneral } from './types'

const calendar = new Calendar()

export {Calendar as GregorianCalendar}
export { Calendar as GregorianCalendar }
export default calendar
1 change: 1 addition & 0 deletions src/views/dashboard/CardPage.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ const CardPage = observer(() => {
CardsAPI.getCardByid(id).then((res) => {
cardStore.setOriginNameOfCard(res.data.name)
cardStore.setCardInfo(res.data)
cardStore.setOrganiztionAndOwner()
})
}
if (authStore.currentUser) {
Expand Down

0 comments on commit 08b460d

Please sign in to comment.